xxxxxxxxxx
from binarytree import Node
def insert(T, val):
parent = search(T, val)
if val == parent.val: #the value exists in the tree
return #do nothing
newnode = Node(val)
if val < parent.value: #the value is less than the parent
parent.left = newnode
else: #the value is greater than the parent
parent.right = newnode
xxxxxxxxxx
# Python program to insert element in binary tree
class newNode():
def __init__(self, data):
self.key = data
self.left = None
self.right = None
""" Inorder traversal of a binary tree"""
def inorder(temp):
if (not temp):
return
inorder(temp.left)
print(temp.key,end = " ")
inorder(temp.right)
"""function to insert element in binary tree """
def insert(temp,key):
if not temp:
root = newNode(key)
return
q = []
q.append(temp)
# Do level order traversal until we find
# an empty place.
while (len(q)):
temp = q[0]
q.pop(0)
if (not temp.left):
temp.left = newNode(key)
break
else:
q.append(temp.left)
if (not temp.right):
temp.right = newNode(key)
break
else:
q.append(temp.right)
# Driver code
if __name__ == '__main__':
root = newNode(10)
root.left = newNode(11)
root.left.left = newNode(7)
root.right = newNode(9)
root.right.left = newNode(15)
root.right.right = newNode(8)
print("Inorder traversal before insertion:", end = " ")
inorder(root)
key = 12
insert(root, key)
print()
print("Inorder traversal after insertion:", end = " ")
inorder(root)
xxxxxxxxxx
# Python program to insert element in binary tree
class newNode():
def __init__(self, data):
self.key = data
self.left = None
self.right = None
""" Inorder traversal of a binary tree"""
def inorder(temp):
if (not temp):
return
inorder(temp.left)
print(temp.key,end = " ")
inorder(temp.right)
"""function to insert element in binary tree """
def insert(temp,key):
if not temp:
root = newNode(key)
return
q = []
q.append(temp)
# Do level order traversal until we find
# an empty place.
while (len(q)):
temp = q[0]
q.pop(0)
if (not temp.left):
temp.left = newNode(key)
break
else:
q.append(temp.left)
if (not temp.right):
temp.right = newNode(key)
break
else:
q.append(temp.right)
# Driver code
if __name__ == '__main__':
root = newNode(10)
root.left = newNode(11)
root.left.left = newNode(7)
root.right = newNode(9)
root.right.left = newNode(15)
root.right.right = newNode(8)
print("Inorder traversal before insertion:", end = " ")
inorder(root)
key = 12
insert(root, key)
print()
print("Inorder traversal after insertion:", end = " ")
inorder(root)
# This code is contributed by DarkDevilor
xxxxxxxxxx
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root==null)
{
TreeNode node = new TreeNode(val);
return node;
}
else if( val< root.val)
{
root.left=insertIntoBST(root.left,val);
}
else if( val> root.val)
{
root.right=insertIntoBST(root.right,val);
}
return root;
}
}
xxxxxxxxxx
#include "stdio.h"
#include "stdlib.h"
struct node{
int data;
struct node *r,*l;
};
//struct node *temp=NULL;
struct node *newnode(int x)
{
struct node *root=(struct node*)malloc(sizeof(struct node));
root->data=x;
root->l=NULL;
root->r=NULL;
return root;
}
struct node *create(struct node *root,int x)
{
if(root==NULL)
return newnode(x);
if(x < root->data)
root->l=create(root->l,x);
else if(x > root->data)
root->r=create(root->r,x);
return root;
}
void inorder(struct node *root)
{
if(root==NULL)
return;
else
{
inorder(root->l);
printf("%d ",root->data);
inorder(root->r);
}
//printf("\n");
}
struct node *MinValNode()
{
}
int main()
{
struct node *temp=NULL;
int n,x,w;
////scanf("%d",&x);
while(w!=-1)
{
scanf("%d",&w);
printf("X:");
scanf("%d",&x);
temp=create(temp,x);
printf("Inserted\n");
if(w==2)
inorder(temp);
}
}