-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionInTreeUsingArray.cpp
More file actions
59 lines (59 loc) · 1.22 KB
/
insertionInTreeUsingArray.cpp
File metadata and controls
59 lines (59 loc) · 1.22 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
// Insertion in trees using array representation
// implementation of tree using array numbering starting from 0 to n-1.
#include <bits/stdc++.h>
using namespace std;
char tree[10]; // declare tree array
// create root
int root(char key)
{
if (tree[0] != '\0')
cout << "Tree already had root";
else
tree[0] = key;
return 0;
}
// left child of node
int set_left(char key, int parent)
{
if (tree[parent] == '\0')
cout << "\nCan't set child at "
<< (parent * 2) + 1
<< " , no parent found";
else
tree[(parent * 2) + 1] = key;
return 0;
}
// right child of node
int set_right(char key, int parent)
{
if (tree[parent] == '\0')
cout << "\nCan't set child at "
<< (parent * 2) + 2
<< " , no parent found";
else
tree[(parent * 2) + 2] = key;
return 0;
}
int print_tree()
{
cout << "\n";
for (int i = 0; i < 10; i++)
{
if (tree[i] != '\0')
cout << tree[i];
else
cout << " - ";
}
return 0;
}
int main()
{
root('A');
set_right('C', 0);
set_left('D', 1);
set_right('E', 1);
set_right('F', 2);
print_tree();
cout << endl;
return 0;
}