-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary tree.cpp
110 lines (96 loc) · 1.61 KB
/
binary tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
struct node *root=NULL;
struct node *insert(struct node *root)
{
struct node *new_node,*temp;
int num,opt,n;
printf("\n Enter the number of elements:");
scanf("%d",&n);
while(n!=0)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->left=NULL;
new_node->right=NULL;
printf("\n Enter the value of data:");
scanf("%d",&num);
new_node->data=num;
if(root==NULL)
{
root=new_node;
}
else
{
temp=root;
int flag=0;
while(flag!=1)
{
fflush(stdin);
printf("\n Enter 1.left or 2.right:");
scanf("%d",&opt);
if(opt==1)
{
if(temp->left==NULL)
{
temp->left=new_node;
temp=temp->left;
flag=1;
}
else
flag=0;
printf("\n %",root->left);
}
else if(opt==2)
{
if(temp->right==NULL)
{
temp->right=new_node;
temp=temp->right;
flag=1;
}
else
flag=0;
}
else
printf("\n invalid option...");
}
}
n--;
}
printf("\n Elements inserted sucessfully..");
return root;
}
struct node *del(struct node *root)
{
int key;
printf("Enter the value of node to be searched:");
scanf("%d",&key);
struct node *ptr;
ptr=root;
}
void inorder(struct node *pos)
{
if(pos!=NULL)
{
inorder(pos->left);
printf(" \t %d",pos->data);
inorder(pos->right);
}
}
void display(struct node *root)
{
struct node *ptr;
ptr=root;
printf("\n Elements in tree..\n");
inorder(ptr);
}
int main()
{
root=insert(root);
display(root);
}