-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
123 lines (83 loc) · 1.99 KB
/
BinaryTree.cpp
File metadata and controls
123 lines (83 loc) · 1.99 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<bits/stdc++.h>
using namespace std;
typedef struct node Node;
struct node
{
int data;
struct node *left;
struct node *right;
};
Node *create_Node(int data)
{
Node *new_node = (struct node*)malloc(sizeof(Node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return (new_node);
}
void add_left_child(Node *node, Node *child)
{
node->left = child;
}
void add_right_child(Node *node, Node *child)
{
node->right = child;
}
Node *create_tree()
{
Node *two = create_Node(2);
Node *seven = create_Node(7);
Node *nine = create_Node(9);
add_left_child(two,seven);
add_right_child(two, nine);
Node *one = create_Node(1);
Node *six = create_Node(6);
add_left_child(seven, one);
add_right_child(seven, six);
Node *five = create_Node(5);
Node *ten = create_Node(10);
add_left_child(six, five);
add_right_child(six, ten);
Node *eight = create_Node(8);
add_right_child(nine, eight);
Node *three = create_Node(3);
Node *four = create_Node(4);
add_left_child(eight, four);
add_right_child(eight, three);
return two;
}
void pre_order(Node *node)
{
printf("%d ", node->data);
if(node->left != NULL)
pre_order(node->left);
if(node->right != NULL)
pre_order(node->right);
}
void post_order(Node *node)
{
if(node->left != NULL)
post_order(node->left);
if(node->right != NULL)
post_order(node->right);
printf("%d ", node->data);
}
void in_order(Node *node)
{
if(node->left != NULL)
in_order(node->left);
printf("%d ", node->data);
if(node->right != NULL)
in_order(node->right);
}
int main()
{
Node *root = create_tree();
pre_order(root);
printf("\n\n");
post_order(root);
printf("\n\n");
in_order(root);
printf("\n\n");
return 0;
}