-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapTree.cpp
More file actions
182 lines (172 loc) · 4.11 KB
/
Copy pathHeapTree.cpp
File metadata and controls
182 lines (172 loc) · 4.11 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;
struct Node
{
string data;
Node* LChild = NULL;
Node* RChild = NULL;
Node(string str){
data = str;
}
} *root = NULL;
void insert(vector<Node*>&v,int index){
int parentIndex = (index - 1)/2;
if (v[parentIndex]->LChild == NULL)
{
v[parentIndex]->LChild = v[v.size()-1];
}
else if (v[parentIndex]->RChild == NULL)
{
v[parentIndex]->RChild = v[v.size()-1];
}
}
void mirror(Node* node){
if (node == NULL || node->LChild == NULL || node->RChild == NULL)
{
return;
}
Node* temp = node->RChild;
node->RChild = node->LChild;
node->LChild = temp;
mirror(node->LChild);
mirror(node->RChild);
}
void preOrder(Node* node){
if (node != NULL)
{
cout<<node->data<<" ";
preOrder(node->LChild);
preOrder(node->RChild);
}
}
// void Predecessors(vector<Node*> v,int index){
// int parentIndex = (index - 1)/2;
// while (parentIndex >= 0 && v[parentIndex] != NULL)
// {
// cout<< v[parentIndex]->data;
// index = parentIndex;
// parentIndex = (index - 1)/2;
// }
// }
//Predecssor: the node with the largest value less than x
//successor: the node with the smallest value greater than x
void findPreSuc(Node* node, Node*& pre, Node*& suc, string key) {
if (node == NULL) return;
if (node->data == key) {
// Find predecessor: RChildmost in left subtree
if (node->LChild != NULL) {
Node* temp = node->LChild;
while (temp->RChild)
temp = temp->RChild;
pre = temp;
}
// Find successor: LChildmost in right subtree
if (node->RChild != NULL) {
Node* temp = node->RChild;
while (temp->LChild)
temp = temp->LChild;
suc = temp;
}
}
else if (node->data > key) {
// Current node might be successor
suc = node;
findPreSuc(node->LChild, pre, suc, key);
}
else {
// Current node might be predecessor
pre = node;
findPreSuc(node->RChild, pre, suc, key);
}
}
int height(Node* node,int h,string data){
int lh,rh;
if (node == NULL || node->data == data)
{
return h;
}
h++;
lh = height(node->LChild,h,data);
rh = height(node->RChild,h,data);
if (lh > rh)
{
h = lh;
}
else{
h = rh;
}
return h;
}
int BFS(Node* node){
queue<Node*> q;
int depth = 0;
q.push(node);
while (!q.empty())
{
int levelsize = q.size();
for (size_t i = 0; i < levelsize; i++)
{
Node* temp = q.front();
q.pop();
if (temp->LChild != NULL)
{
q.push(temp->LChild);
}
if (temp->RChild != NULL)
{
q.push(temp->RChild);
}
}
depth++;
}
return depth - 1;
}
int BSTsize(Node* node){
int Lcount = 0;
int Rcount = 0;
if (node == NULL)
{
return 0;
}
Lcount = BSTsize(node->LChild);
Rcount = BSTsize(node->RChild);
return Lcount + Rcount + 1;
}
int main(int argc, char const *argv[])
{
vector<Node*>v;
string data;
for (size_t i = 0; i < 6; i++)
{
cout<<"enter data: ";
cin>> data;
v.push_back(new Node(data));
if (i == 0)
{
root = v[0];
}
else{
insert(v,i);
}
}
preOrder(root);
cout<<endl;
//cout<<height(root,-1,"B"); //height of a tree
mirror(root);
preOrder(root);
Node* pre = NULL;
Node* suc = NULL;
findPreSuc(root,pre,suc,v[0]->data);
if (pre != NULL) cout << "Predecessor: " << pre->data << endl;
else cout << "No Predecessor" << endl;
if (suc != NULL) cout << "Successor: " << suc->data << endl;
else cout << "No Successor" << endl;
//BFS to find the height of the tree
int h = BFS(root);
cout<<"Height using BFS: "<<h;
cout<<BSTsize(root);
return 0;
}