This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary-depth-search.js
159 lines (142 loc) · 3.6 KB
/
binary-depth-search.js
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
/**
* A binary tree is a data structure meant to contain a tree-like relationship between a value and one of two children
*
* {@link https://en.wikipedia.org/wiki/Binary_tree}
*
* The "_find" method is what's known as a "depth-first" search
*
* {@link https://en.wikipedia.org/wiki/Depth-first_search}
*/
// Copilot was given "class Node"
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Copilot suggested all of this without any help whatsoever
class BinarySearchTree {
constructor() {
this.root = null;
}
insert(data) {
if (!this.root) {
this.root = new Node(data);
} else {
this._insert(this.root, data);
}
}
_insert(node, data) {
if (data <= node.data) {
if (node.left) {
this._insert(node.left, data);
} else {
node.left = new Node(data);
}
} else {
if (node.right) {
this._insert(node.right, data);
} else {
node.right = new Node(data);
}
}
}
find(data) {
if (!this.root) {
return null;
} else {
return this._find(this.root, data);
}
}
_find(node, data) {
if (node.data === data) {
return node;
} else if (data <= node.data) {
if (node.left) {
return this._find(node.left, data);
} else {
return null;
}
} else {
if (node.right) {
return this._find(node.right, data);
} else {
return null;
}
}
}
delete(data) {
if (!this.root) {
return;
} else {
this._delete(this.root, data);
}
}
_delete(node, data) {
if (node.data === data) {
if (!node.left && !node.right) {
node = null;
} else if (!node.left) {
node = node.right;
}
else if (!node.right) {
node = node.left;
}
else {
let parent = node;
let child = node.right;
while (child.left) {
parent = child;
child = child.left;
}
node.data = child.data;
if (child.right) {
child.right.left = child.left;
}
if (child.left) {
child.left.right = child.right;
}
if (parent === node) {
parent.left = child.right;
} else {
parent.right = child.right;
}
}
} else if (data <= node.data) {
if (node.left) {
this._delete(node.left, data);
}
} else {
if (node.right) {
this._delete(node.right, data);
}
}
}
print() {
if (!this.root) {
return;
} else {
this._print(this.root);
}
}
_print(node) {
if (node) {
this._print(node.left);
console.log(node.data);
this._print(node.right);
}
}
}
// Copilot was given "var bst"
var bst = new BinarySearchTree();
// Copilot suggested these on its own
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(1);
bst.insert(4);
bst.insert(6);
bst.insert(8);
bst.insert(2);
bst.print();