-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreation_of_binary_tree.cpp
128 lines (102 loc) · 2.78 KB
/
creation_of_binary_tree.cpp
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
124
125
126
127
128
#include <iostream>
using namespace std;
/* If you don't understand the flow of recursion then comment all the other function and run only the buildTree function and see the value of right and left and you
will get the flow of recursion. */
/*Steps to create the binary tree
1. make the node class
2. make the build function of binary tree
*/
/* In this code we will see the two methods of the level order traversal of the binary tree.
Second method will be in the "Breadth first search file" */
class Node
{
public:
int data;
Node *left;
Node *right;
// making the constructor
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
Node *buildTree(Node *root)
{
// enter the data of the root
printf("enter data: ");
int num;
cin >> num;
root = new Node(num);
// base case :
// cout<<"we consider -1 as NULL. To terminate the loop."<<endl;
if (num == -1)
return NULL;
// now filling the left and right part of the tree.
// Left Part
cout << "enter the data to fill on left of -> " << num << endl;
// recursive call for the left part of the tree
root->left = buildTree(root->left);
// Right Part
cout << "enter the data to fill on right -> " << num << endl;
// recursive call for the left part of the tree
root->right = buildTree(root->right);
// finally return the root
return root;
}
// finding the height of the binary tree.
int height(Node *root)
{
if (root == NULL)
return 0;
else
{
// compute the left height.
int Lheight = height(root->left);
int Rheight = height(root->right);
// NOW return the larger height
Lheight += 1;
Rheight += 1;
return max(Lheight, Rheight);
}
}
// ***************** 1st method of the level of iteration without using the queue instead that using the "simple recursion" *********************
// printing the current level.
void currentLevel(Node *root, int level)
{
if (root == NULL)
return;
else if (level == 1)
{
cout << root->data << " ";
return;
}
else if (level > 1)
{
currentLevel(root->left, level - 1);
currentLevel(root->right, level - 1);
}
}
// iterating the levels of the binary tree.
void LevelOrder(Node *root)
{
int h = height(root);
for (int i = 1; i <= h; i++)
{
currentLevel(root, i);
cout << endl;
}
}
int main()
{
// 4 2 1 -1 -1 3 -1 -1 5 6 -1 -1 7 -1 -1
Node *root = NULL;
root = buildTree(root);
int depth = height(root);
cout << "The height of the binary tree is -> " << depth << endl;
// printing the binary tree in level order traversal.
LevelOrder(root);
cout << endl;
return 0;
}