-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.cpp
78 lines (66 loc) · 1.21 KB
/
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
#include<iostream>
#include<queue>
using namespace std;
struct node{
int data;
node *left;
node *right;
};
node* createnode(int data){
node* newnode=new node();
/* if(! newnode){
cout<<"memory error";
}*/
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
node* insert(node* root,int data){
if (root=NULL){
createnode(data);
return root;
}
queue<node*>q;
q.push(root);
while(!q.empty()){
node *temp=q.front();
q.pop();
if(temp->left != NULL){
q.push(temp->left);
}
else{
temp->left=createnode(data);
return root;
}
if(temp->right != NULL){
q.push(temp->right);}
else{
temp->right=createnode(data);
return root;
}
}
}
void inorder(node *temp){
if(temp==NULL){
return ;
}
inorder(temp->left);
cout<<temp->data;
inorder(temp->right);
}
int main(){
node *root=createnode(10);
root->left=createnode(12);
root->right=createnode(15);
root->left->left=createnode(34);
root->left->right=createnode(18);
cout<<"inorder before";
inorder(root);
cout<<"inorder after insertion";
int k=12;
insert(root,k);
inorder(root);
return 0;
//getch();
}