-
Notifications
You must be signed in to change notification settings - Fork 0
/
How to create a binary Tree
86 lines (66 loc) · 1.43 KB
/
How to create a binary Tree
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
#include<bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin);
#define write() freopen("output.txt", "w", stdout);
#define ll long long int
struct Node
{
int data;
Node *left;
Node *right;
};
Node *createnode(int item)
{
Node *temp=new Node();
if(temp==NULL) {cout<<" Error!! Could not create a new node"<<endl; exit(1);}
temp->data=item;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void add_left_child(Node *node, Node *child)
{
node->left=child;
}
void add_right_child(Node *node, Node *child)
{
node->right=child;
}
Node *createtree()
{
Node *two=createnode(2);
Node *seven=createnode(7);
Node *nine=createnode(9);
add_left_child(two,seven);
add_right_child(two,nine);
Node *one=createnode(1);
Node *six=createnode(6);
Node *eight=createnode(8);
add_left_child(seven,one);
add_right_child(seven,six);
add_right_child(nine,eight);
Node *five=createnode(5);
Node *ten=createnode(10);
Node *three=createnode(3);
Node *four=createnode(4);
add_left_child(six,five);
add_right_child(six,ten);
add_left_child(eight,three);
add_right_child(eight,four);
return two;
}
int main()
{
#ifdef asiuzzaman
read(); write();
#endif
Node *root=createtree();
cout<<root->data<<endl;
}
2
/ \
7 9
/ \ \
1 6 8
/ \ / \
5 10 3 4