-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.cpp
160 lines (133 loc) · 2.96 KB
/
binarytree.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "iostream"
#include "stack"
#include "queue"
#include "iomanip"
#include "cstdlib"
#define REP(i, n) for(int i = 0; i < n; i++)
#define MIN -100
#define MAX 100
using namespace std;
struct node {
int data;
node *l, *r;
node(int x, node *left, node *right) {
data = x;
l = left;
r = right;
};
};
typedef node *link;
void prettyPrint(link p, int indent=4)
{
if(p != NULL) {
if(p->r) {
prettyPrint(p->r, indent+4);
}
if (indent) {
cout << setw(indent) << ' ';
}
if (p->r) cout<<" /\n" << setw(indent) << ' ';
cout<< p->data << "\n ";
if(p->l) {
cout << setw(indent) << ' ' <<" \\\n";
prettyPrint(p->l, indent+4);
}
}
}
void traverse(link h) {
if (h == NULL) return;
traverse(h->l);
cout << h->data << ' ';
traverse(h->r);
}
// traverse using stack
void traverse2(link h) {
if (h == NULL) return;
stack<link> s;
s.push(h);
while(!s.empty()) {
h = s.top();
cout << h->data << ' ';
s.pop();
if (h->r != NULL) s.push(h->r);
if (h->l != NULL) s.push(h->l);
}
}
void levelTraverse(link h) {
if (h == NULL) return;
queue<link> q;
q.push(h);
while(!q.empty()) {
h = q.front();
cout << h->data << ' ';
q.pop();
if (h->l != NULL) q.push(h->l);
if (h->r != NULL) q.push(h->r);
}
}
int countNodes(link h) {
if (h == NULL) return 0;
return 1 + countNodes(h->l) + countNodes(h->r);
}
int getHeight(link h) {
if (h == NULL) return -1;
int u = getHeight(h->l);
int v = getHeight(h->r);
if (u > v) return u + 1;
else return v + 1;
}
int countLeaves(link h) {
if (h == NULL) return 0;
if (h->l == NULL && h->r == NULL) return 1;
return countLeaves(h->l) + countLeaves(h->r);
}
bool isBST(link h, int min, int max) {
if (h == NULL) return true;
if (h->data < min || h->data > max) return false;
return isBST(h->l, min, h->data - 1) && isBST(h->r, h->data + 1, max);
}
// selects random node from the tree using reservoir sampling
link selectRandomNode(link h) {
int nodeCount = 1;
link selected = NULL;
if (h == NULL) return h;
stack<link> s;
s.push(h);
while(!s.empty()) {
h = s.top();
if (rand() % nodeCount == nodeCount - 1) selected = h;
nodeCount++;
s.pop();
if (h->r != NULL) s.push(h->r);
if (h->l != NULL) s.push(h->l);
}
return selected;
}
int main()
{
/*
3
/ \
1 5
/ \
0 7
*/
// create the above tree
link root = new node(3, NULL, NULL);
root->l = new node(1, NULL, NULL);
root->r = new node(5, NULL, NULL);
root->l->l = new node(0, NULL, NULL);
root->l->r = new node(7, NULL, NULL);
levelTraverse(root);
cout << '\n';
cout << "Number of nodes : " << countNodes(root) << '\n';
cout << "Height of tree : " << getHeight(root) << '\n';
cout << "Number of leaves : " << countLeaves(root) << '\n';
cout << '\n';
prettyPrint(root);
cout << isBST(root, MIN, MAX) << '\n';
// generate random seed
srand(time(0));
cout << selectRandomNode(root)->data << '\n';
return 0;
}