-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49-binarytree.cpp
More file actions
70 lines (64 loc) · 1.31 KB
/
49-binarytree.cpp
File metadata and controls
70 lines (64 loc) · 1.31 KB
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
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
class bt
{
public:
node* insert(int item,node *root)
{
char direction[10];
int i;
node *temp=new node;
node *cur=new node;
node *prev=new node;
temp->info=item;
temp->rlink=temp->llink=NULL;
if(root==NULL)
return temp;
cout<<"give direction\n";
cin>>direction;
prev=NULL;
cur=root;
for(i=0; i<strlen(direction)&&(cur!=NULL); i++)
{
prev=cur;
if(direction[i]=='L')
cur=cur->llink;
else
cur=cur->rlink;
}
if(direction[i-1]=='L')
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
void preorder(node *root)
{
if(root!=NULL)
{
cout<<root->info<<endl;
preorder(root->llink);
preorder(root->rlink);
}
}
};
int main()
{
bt obj;
node *root=NULL;
root=obj.insert(10,root);
root=obj.insert(5,root);
root=obj.insert(20,root);
root=obj.insert(15,root);
root=obj.insert(30,root);
obj.preorder(root);
}