-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_tree_operations.cpp
138 lines (116 loc) · 2.88 KB
/
basic_tree_operations.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
129
130
131
132
133
134
135
136
137
138
/*
this code is my method of performing basic tree operations..
like:
no.of nodes
no of leafs
*/
#include <iostream>
#include <vector>
typedef struct node{
char data;
node *left;
node *right;
}node;
node *make_tree(char to_push[9]){
node *a = (node*)malloc(sizeof(node));
a->data = to_push[0];
node *b = (node*)malloc(sizeof(node));
b->data = to_push[1];
a->left = b;
node *c = (node*)malloc(sizeof(node));
c->data = to_push[2];
a->right = c;
b->left = NULL;
node *d = (node*)malloc(sizeof(node));
b->right = d;
d->data = to_push[3];
node *e = (node*)malloc(sizeof(node));
node *f = (node*)malloc(sizeof(node));
e->data = to_push[4];
e->left = NULL;
e->right = NULL;
f->data = to_push[5];
f->left = NULL;
f->right = NULL;
d->left = e;
d->right = f;
node *g = (node*)malloc(sizeof(node));
node *h = (node*)malloc(sizeof(node));
g->data = to_push[6];
g->left = NULL;
g->right = NULL;
h->data = to_push[7];
h->left = NULL;
h->right = NULL;
c->left = g;
c->right = h;
return a;
}
int count_nodes(node *root){
if(root!=NULL){
return 1+count_nodes(root->left)+count_nodes(root->right);
}
else{
return 0;
}
}
int count_leafs(node *root){
if(root!=NULL){
if(root->left == NULL && root->right == NULL){
return 1;
}
else{
return count_leafs(root->left)+count_leafs(root->right);
}
}
else{
return 0;
}
}
// Non recursive leaf count
// this idea of mine is based on tree traversal algo that I wrote before. I will write the post
// order non recursive algorithm to get better accustomed to this
//The idea behind non recursive leaf count is that, I simply traverse the tree in a non-recursive
//manner and the point where I print/visit the node, I simply check the children of the node and
//increment the count.
//This is a great approach because, I don't have to come up with a new algorithm for non recursive leaf count
// or any such thing. One traversal algorithm will do it all!
// make a flash card out of this!
int non_recur_leaf_count(node *root){
std::vector<std::pair<node *, int>> stack;
int count = 0;
while(root != NULL){
stack.push_back(std::make_pair(root, 0));
root = root->left;
}
while(!stack.empty()){
std::pair<node *, int> R = stack.back();
stack.pop_back();
root = R.first;
if(R.second == 0){
stack.push_back(std::make_pair(root, 1));
root = root->right;
while(root!=NULL){
stack.push_back(std::make_pair(root, 0));
root = root->left;
}
}
else{
if(root->left == NULL && root->right == NULL){
count++;
}
}
}
return count;
}
int main(){
char to_push[9]= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
node *root = NULL;
root = make_tree(to_push);
// haha works!
std::cout << "no of nodes are: " << count_nodes(root);
//leaf nodes
std::cout << "\nno of leaf nodes are: " << count_leafs(root);
std::cout << "\n(non recurr)no of leaf nodes are: " << non_recur_leaf_count(root);
return 0;
}