Repository navigation
Expand file tree
/
Copy pathBST.cpp
More file actions
165 lines (165 loc) · 3.5 KB
/
Copy pathBST.cpp
File metadata and controls
165 lines (165 loc) · 3.5 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
#include "BST.h"
#include <iostream>
using namespace std;
BST::BST() {
root = NULL;
}
BST::BST(BST &bst) {
root = NULL;
Node** stack = new Node*[bst.count];
unsigned int stack_size = 0;
stack[stack_size++] = bst.root;
while (stack_size > 0) {
Node* p = stack[--stack_size];
insert(p->value);
if (p->right != NULL)
stack[stack_size++] = p->right;
if (p->left != NULL)
stack[stack_size++] = p->left;
}
delete[] stack;
count = bst.count;
}
void BST::insert(int x) {
root = insert(x, root);
count++;
}
Node* BST::insert(int x, Node* node) {
if (node == NULL) {
node = new Node;
node->value = x;
node->left = NULL;
node->right = NULL;
}
else if (x < node->value) {
node->left = insert(x, node->left);
}
else if (x > node->value) {
node->right = insert(x, node->right);
}
return node;
}
Node* BST::remove(int x, Node* node) {
Node* temp;
if (node == NULL)
return NULL;
else if (x < node->value)
node->left = remove(x, node->left);
else if (x > node->value)
node->right = remove(x, node->right);
else if (node->left && node->right) {
temp = findMin(node->right);
node->value = temp->value;
node->right = remove(node->value, node->right);
}
else {
temp = node;
if (node->left == NULL)
node = node->right;
else if (node->right == NULL)
node = node->left;
delete temp;
}
return node;
}
Node* BST::findMin(Node* node) {
if (node == NULL)
return NULL;
else if (node->left == NULL)
return node;
else
return findMin(node->left);
}
void BST::remove(int x) {
root = remove(x, root);
count--;
}
void printTree(BST bst) {
if (bst.root == NULL)
return;
Node** stack = new Node*[bst.count];
unsigned int stack_size = 0;
Node* p = bst.root;
while (p != NULL || stack_size > 0) {
if (p != NULL) {
stack[stack_size++] = p;
p = p->left;
}
else {
p = stack[--stack_size];
cout << p->value << " ";
p = p->right;
}
}
delete[] stack;
}
int* BST::preorder(BST bst) {
if (bst.root == NULL)
return NULL;
Node** stack = new Node*[bst.count];
int* vector = new int[bst.count];
int vector_size = 0;
unsigned int stack_size = 0;
stack[stack_size++] = bst.root;
while (stack_size > 0) {
Node* p = stack[--stack_size];
vector[vector_size++] = p->value;
if (p->right != NULL)
stack[stack_size++] = p->right;
if (p->left != NULL)
stack[stack_size++] = p->left;
}
delete[] stack;
return vector;
}
int* BST::postorder(BST bst) {
if (bst.root == NULL)
return NULL;
Node** stack = new Node*[bst.count];
int* vector = new int[bst.count];
int vector_size = 0;
unsigned int stack_size = 0;
Node* p = bst.root;
while (stack_size > 0) {
while (p != NULL) {
if (p->right != NULL)
stack[stack_size++] = p->right;
stack[stack_size++] = p;
p = p->left;
}
p = stack[--stack_size];
if (p->right != NULL && stack[stack_size - 1] == p->right) {
stack[stack_size - 1] = p;
p = p->right;
}
else {
vector[vector_size++] = p->value;
p = NULL;
}
}
delete[] stack;
return vector;
}
void BST::print(BST bst, int x) {
int* v;
if (x == 0)
v = preorder(bst);
else
v = postorder(bst);
int i;
for (i = 0; i < bst.count; i++)
cout << v[i] << " ";
delete[] v;
}
unsigned int BST::depth(Node* node) {
if (node == NULL)
return 0;
int r = depth(node->right);
int l = depth(node->left);
if (l > r)
return l + 1;
return r + 1;
}
int BST::height() {
return depth(root);
}