Repository navigation
Expand file tree
/
Copy pathHeapBinaryTree.pas
More file actions
280 lines (235 loc) · 8.14 KB
/
Copy pathHeapBinaryTree.pas
File metadata and controls
280 lines (235 loc) · 8.14 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Created by Matthew Abbott 31/8/22 two days till lord of the rings rings of power. edit on 13/9/22 it really sucked what a let down.
//
{$mode objfpc}
{$M+}
unit HeapBinaryTree;
interface
uses HeapBinaryTreeNode in './HeapBinaryTreeNode.pas';
type
THeapBinaryTree = class
public
constructor create();
procedure insertData(inputData: integer);
procedure printTree();
function deleteNode(key: integer): boolean;
function countNodes(): integer;
function findNodeNumber(key: integer): integer;
private
procedure printPreOrder(p: THeapBinaryTreeNode; indent: integer);
procedure preOrder(localRoot: THeapBinaryTreeNode);
procedure inOrder(localRoot: THeapBinaryTreeNode);
procedure postOrder(localRoot: THeapBinaryTreeNode);
function getSuccessor(delNode: THeapBinaryTreeNode): THeapBinaryTreeNode;
end;
var
root: THeapBinaryTreeNode;
counter, nodeCount: integer;
implementation
constructor THeapBinaryTree.create();
begin
root := nil;
end;
procedure THeapBinaryTree.insertData(inputData: integer);
var
newTreeNode, current, parent, temp: THeapBinaryTreeNode;
begin
newTreeNode := THeapBinaryTreeNode.create;
newTreeNode.setData(inputData);
if root = nil then
begin
root := newTreeNode;
nodeCount := 1;
root.setNodeNumber(1);
end
else
begin
inc(nodeCount);
current := root;
while 1 <> 0 do
begin
parent := current;
if inputData <= current.getData then
begin
current := current.getLeftChild;
if current = nil then
begin
parent.setLeftChild(newTreeNode);
temp := parent.getLeftChild;
temp.setNodeNumber(nodeCount);
break;
end;
end;
if inputData > current.getData then
begin
current := current.getRightChild;
if current = nil then
begin
parent.setRightChild(newTreeNode);
temp := parent.getRightChild;
temp.setNodeNumber(nodeCount);
break;
end;
end;
end;
end;
end;
function THeapBinaryTree.deleteNode(key: integer): boolean;
var
current, parent, successor: THeapBinaryTreeNode;
isLeftChild: boolean;
begin
current := root;
parent := root;
isLeftChild := true;
while current.getData <> key do
begin
writeln(current.getData);
parent := current;
if key < current.getData then
begin
isLeftChild := true;
current := current.getLeftChild
end
else
begin
isLeftChild := false;
current := current.getRightChild;
end;
if current = nil then
begin
deleteNode := false;
end;
end;
if current.getLeftChild = nil then
begin
if current.getRightChild = nil then
begin
if current = root then
root := nil
else if isLeftChild = true then
parent.setLeftChild(nil)
else
parent.setRightChild(nil);
end;
end
else if current.getRightChild = nil then
begin
if current = root then
root := current.getLeftChild
else if isLeftChild = true then
parent.setLeftChild(current.getLeftChild)
else
parent.setRightChild(current.getLeftChild);
end
else if current.getLeftChild = nil then
begin
if current = root then
root := current.getRightChild
else if isLeftChild = true then
parent.setLeftChild(current.getRightChild)
else
parent.setRightChild(current.getRightChild);
end
else
begin
successor := getSuccessor(current);
if current = root then
root := successor
else if isLeftChild = true then
parent.setLeftChild(successor)
else
parent.setRightChild(successor);
successor.setLeftChild(current.getLeftChild);
end;
deleteNode := true;
end;
function THeapBinaryTree.getSuccessor(delNode: THeapBinaryTreeNode): THeapBinaryTreeNode;
var
successorParent, successor, current: THeapBinaryTreeNode;
begin
successorParent := delNode;
successor := delNode;
current := delNode.getRightChild;
while current <> nil do
begin
successorParent := successor;
successor := current;
current := current.getLeftChild;
end;
if successor <> delNode.getRightChild then
begin
successorParent.setLeftChild(successor.getRightChild);
successor.setRightChild(delNode.getRightChild);
end;
getSuccessor := successor;
end;
function THeapBinaryTree.countNodes(): integer;
begin
counter := 0;
preOrder(root);
countNodes := counter;
end;
procedure THeapBinaryTree.preOrder(localRoot: THeapBinaryTreeNode);
begin
if localRoot <> nil then
begin
inc(counter); {replace this line with code for pre order execution on the tree}
preOrder(localRoot.getLeftChild);
preOrder(localRoot.getRightChild);
end;
end;
procedure THeapBinaryTree.inOrder(localRoot: THeapBinaryTreeNode);
begin
inOrder(localRoot.getLeftChild);
{insert code here for inorder execution on the tree}
inOrder(localRoot.getRightChild);
end;
procedure THeapBinaryTree.postOrder(localRoot: THeapBinaryTreeNode);
begin
postOrder(localRoot.getLeftChild);
postOrder(localRoot.getRightChild);
{insert code here for postorder execution on the tree}
end;
function THeapBinaryTree.findNodeNumber(key: integer): integer;
var
current: THeapBinaryTreeNode;
begin
current := root;
while current.getData <> key do
begin
if key < current.getData then
current := current.getLeftChild
else
current := current.getRightChild;
if current = nil then
findNodeNumber := 0;
end;
findNodeNumber := current.getNodeNumber;
end;
procedure THeapBinaryTree.printTree();
begin
printPreOrder(root, 0);
end;
procedure THeapBinaryTree.printPreOrder(p: THeapBinaryTreeNode; indent: integer);
var
i: integer;
begin
if indent <> 0 then
begin
for i := 0 to indent do
begin
write(' ');
end;
end;
writeln(p.getData);
if p.getLeftChild <> nil then
begin
printPreOrder(p.getLeftChild, indent + 4);
end;
if p.getRightChild <> nil then
begin
printPreOrder(p.getRightChild, indent + 4);
end;
end;
end.