-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbst_oprcodechf.cpp
More file actions
84 lines (53 loc) · 1.02 KB
/
Copy pathbst_oprcodechf.cpp
File metadata and controls
84 lines (53 loc) · 1.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct treeNode{
int data;
int p;
treeNode *left;
treeNode *right;
};
treeNode *getnode(int data){
treeNode *temp= new treeNode();
temp->data= data;
temp->left= nullptr;
temp->right= nullptr;
return temp;
}
class binarylst{
public:
treeNode* insert(int data,treeNode* root,int p){
if(root== NULL){
cout<<p;
cout<<"\n";
root= getnode(data);
}
else if(data<= root->data){
root= insert(data,root->left,2*p);
}
else{
root= insert(data,root->right,2*p+1);
}
return root;
}
void preorder(treeNode* root){
if(root== NULL){
return;
}
else{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
};
int main(){
treeNode* root= NULL;
binarylst b;
vector<int> a={1,2,0,5};
for(int i=0;i<a.size();i++)
root= b.insert(a[i],root,1);
cout<<"Preorder\n";
b.preorder(root);
return 0;
}