-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary_Tree.cpp
More file actions
79 lines (68 loc) · 1.63 KB
/
Binary_Tree.cpp
File metadata and controls
79 lines (68 loc) · 1.63 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
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node *newnode(int key){
struct node *a = (struct node*)malloc(sizeof(struct node));
a->data=key;
a->left=NULL;
a->right=NULL;
return a;
}
struct node *Insertright(struct node *root, int key){
if(root == NULL) return newnode(key);
root->right = Insertright(root->right, key);
return root;
}
struct node *Insertleft(struct node *root, int key){
if(root == NULL) return newnode(key);
root->left = Insertleft(root->left, key);
return root;
}
void inorder(struct node* root)
{
if (!root) return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
void preorder(struct node* root) {
if(root == NULL) return;
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root) {
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
bool Search(struct node* root, int key)
{
if (root == NULL)
return false;
if (root->data == key)
return true;
bool l = Search(root->left, key);
bool r = Search(root->right, key);
return l || r;
}
int main(){
struct node *root = NULL;
root = Insertleft(root, 84);
root = Insertright(root, 78);
root = Insertright(root, 74);
root = Insertright(root, 46);
root = Insertright(root, 57);
inorder(root);
cout<<endl;
preorder(root);
cout<<endl;
postorder(root);
cout<<endl;
cout<<Search(root, 84);
}