Skip to content

Commit 822bc79

Browse files
BarklimBarklim
authored andcommitted
add bintree implementation
1 parent 5f4d920 commit 822bc79

File tree

4 files changed

+338
-9
lines changed

4 files changed

+338
-9
lines changed

0104-maximum-depth-of-binary-tree.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ var maxDepth = function(root) {
2828
const [left, right] = [root.left, root.right].map(maxDepth);
2929
return 1 + Math.max(left, right);
3030
};
31+
32+
// var maxDepth = function(root) {
33+
// if (!root) return 0;
34+
// left = maxDepth(root.left)
35+
// right = maxDepth(root.right)
36+
// return 1 + Math.max(left, right);
37+
// };

dStructure/binTree.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
class TreeNode {
2+
constructor(value) {
3+
this.value = value;
4+
this.left = null;
5+
this.right = null
6+
}
7+
}
8+
9+
class BinaryTree {
10+
constructor() {
11+
this.root = null;
12+
}
13+
14+
add(value) {
15+
const newNode = new TreeNode(value)
16+
if (!this.root) {
17+
this.root = newNode;
18+
return;
19+
}
20+
21+
let currentNode = this.root;
22+
23+
while(currentNode) {
24+
if (newNode.value < currentNode.value) {
25+
if (!currentNode.left) {
26+
currentNode.left = newNode;
27+
return;
28+
}
29+
30+
currentNode = currentNode.left
31+
} else {
32+
if (!currentNode.right) {
33+
currentNode.right = newNode;
34+
return;
35+
}
36+
37+
currentNode = currentNode.right
38+
}
39+
40+
}
41+
}
42+
43+
search(value) {
44+
let currentNode = this.root;
45+
while (currentNode) {
46+
if (value === currentNode.value) {
47+
return currentNode;
48+
}
49+
if (value < currentNode.value) {
50+
currentNode = currentNode.left;
51+
} else {
52+
currentNode = currentNode.right;
53+
}
54+
}
55+
return null;
56+
}
57+
58+
print(root = this.root) {
59+
if (!root) return true
60+
console.log(root.value)
61+
this.print(root.left)
62+
this.print(root.right)
63+
}
64+
}
65+
66+
function createTreeFromArray(arr) {
67+
if (!arr.length || arr[0] === null) return null;
68+
69+
let root = new TreeNode(arr[0]);
70+
let queue = [root];
71+
let i = 1;
72+
73+
while (i < arr.length) {
74+
let current = queue.shift();
75+
76+
if (arr[i] !== null) {
77+
current.left = new TreeNode(arr[i]);
78+
queue.push(current.left);
79+
}
80+
i++;
81+
82+
if (i < arr.length && arr[i] !== null) {
83+
current.right = new TreeNode(arr[i]);
84+
queue.push(current.right);
85+
}
86+
i++;
87+
}
88+
89+
return root;
90+
}
91+
92+
const myTree = new BinaryTree();
93+
const treeFromArray = createTreeFromArray([3,9,20,null,null,15,7]);
94+
var myTreeStringify = JSON.stringify(treeFromArray, null, 2);
95+
96+
const foundNode = myTree.search(10);
97+
98+
console.log('!!! myTree');
99+
// console.log(myTree);
100+
// console.log(myTree.print());
101+
console.log(myTreeStringify);
102+
// console.log(foundNode);
103+
console.log('!!!');

dStructure/binTree2.js

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
class TreeNode {
2+
constructor(value) {
3+
this.value = value;
4+
this.left = null;
5+
this.right = null
6+
}
7+
}
8+
9+
class BinaryTree {
10+
constructor() {
11+
this.root = null;
12+
}
13+
14+
add(value) {
15+
const newNode = new TreeNode(value)
16+
if (!this.root) {
17+
this.root = newNode;
18+
return;
19+
}
20+
21+
let currentNode = this.root;
22+
23+
while(currentNode) {
24+
if (newNode.value < currentNode.value) {
25+
if (!currentNode.left) {
26+
currentNode.left = newNode;
27+
return;
28+
}
29+
30+
currentNode = currentNode.left
31+
} else {
32+
if (!currentNode.right) {
33+
currentNode.right = newNode;
34+
return;
35+
}
36+
37+
currentNode = currentNode.right
38+
}
39+
40+
}
41+
}
42+
43+
preOrder(node, callback) {
44+
if (!node) {
45+
return;
46+
}
47+
48+
if (callback) {
49+
callback(node)
50+
}
51+
52+
this.preOrder(node.left, callback);
53+
this.preOrder(node.right, callback);
54+
}
55+
56+
inOrder(node, callback) {
57+
if (!node) {
58+
return;
59+
}
60+
61+
this.inOrder(node.left, callback);
62+
if (callback) {
63+
callback(node)
64+
}
65+
this.inOrder(node.right, callback);
66+
}
67+
68+
postOrder(node, callback) {
69+
if (!node) {
70+
return;
71+
}
72+
73+
this.postOrder(node.left, callback);
74+
this.postOrder(node.right, callback);
75+
if (callback) {
76+
callback(node)
77+
}
78+
}
79+
80+
traverseDFS(callback, method) {
81+
if (method === 'preOrder') {
82+
return this.preOrder(this.root, callback);
83+
}
84+
85+
if (method === 'inOrder') {
86+
return this.inOrder(this.root, callback);
87+
}
88+
89+
if (method === 'postOrder') {
90+
return this.postOrder(this.root, callback);
91+
}
92+
93+
}
94+
95+
traverseBFS(callback) {
96+
const queue = [this.root];
97+
98+
while(queue.length) {
99+
const node = queue.shift();
100+
callback(node);
101+
102+
if (node.left) {
103+
queue.push(node.left);
104+
}
105+
106+
if (node.right) {
107+
queue.push(node.right);
108+
}
109+
}
110+
}
111+
112+
search(value) {
113+
let currentNode = this.root;
114+
while (currentNode) {
115+
if (value === currentNode.value) {
116+
return currentNode;
117+
}
118+
if (value < currentNode.value) {
119+
currentNode = currentNode.left;
120+
} else {
121+
currentNode = currentNode.right;
122+
}
123+
}
124+
return null;
125+
}
126+
127+
print(root = this.root) {
128+
if (!root) return true
129+
console.log(root.value)
130+
this.print(root.left)
131+
this.print(root.right)
132+
}
133+
}
134+
135+
function createTreeFromArray(arr) {
136+
if (!arr.length || arr[0] === null) return null;
137+
138+
let root = new TreeNode(arr[0]);
139+
let queue = [root];
140+
let i = 1;
141+
142+
while (i < arr.length) {
143+
let current = queue.shift(); // Берем текущий узел
144+
145+
if (arr[i] !== null) {
146+
current.left = new TreeNode(arr[i]);
147+
queue.push(current.left);
148+
}
149+
i++;
150+
151+
if (i < arr.length && arr[i] !== null) {
152+
current.right = new TreeNode(arr[i]);
153+
queue.push(current.right);
154+
}
155+
i++;
156+
}
157+
158+
return root;
159+
}
160+
161+
const myTree = new BinaryTree()
162+
myTree.add(8);
163+
myTree.add(7);
164+
myTree.add(9);
165+
myTree.add(5);
166+
myTree.add(10);
167+
myTree.add(20);
168+
myTree.add(6);
169+
myTree.add(2);
170+
myTree.add(11);
171+
172+
/*
173+
8
174+
7 9
175+
5 10
176+
2 6 20
177+
11
178+
*/
179+
var myTreeStringify = JSON.stringify(myTree, null, 2);
180+
181+
// myTree.traverseDFS((node) => {
182+
// console.log(node.value)
183+
// }, 'preOrder')
184+
// 8 7 5 2 6 9 10 20 11
185+
186+
// myTree.traverseDFS((node) => {
187+
// console.log(node.value)
188+
// }, 'inOrder')
189+
// 2 5 6 7 8 9 10 11 20
190+
191+
// myTree.traverseDFS((node) => {
192+
// console.log(node.value)
193+
// }, 'postOrder')
194+
// 2 6 5 7 11 20 10 9 8
195+
196+
// myTree.traverseBFS((node) => {
197+
// console.log(node.value)
198+
// })
199+
// 8 7 9 5 10 2 6 20 11
200+
201+
const foundNode = myTree.search(10);
202+
203+
const myTree2 = new BinaryTree();
204+
const treeFromArray = createTreeFromArray([3,9,20,null,null,15,7]);
205+
var myTreeStringify2 = JSON.stringify(treeFromArray, null, 2);
206+
207+
// const myTree3 = new BinaryTree();
208+
// [3,9,20,null,null,15,7].forEach(value => myTree3.add(value))
209+
// var myTreeStringify3 = JSON.stringify(myTree3, null, 2);
210+
211+
console.log('!!! myTree');
212+
// console.log(myTree);
213+
// console.log(myTree.print());
214+
// console.log(myTreeStringify);
215+
// console.log(foundNode);
216+
console.log(myTreeStringify2);
217+
// console.log(myTreeStringify3);
218+
console.log('!!!');

example/6.Tree/0104.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
* @return {number}
1212
*/
1313
var maxDepth = function(root) {
14-
14+
1515
};
1616

17-
const example1 = maxDepth(); // [3,9,20,null,null,15,7] // 3
18-
const example2 = maxDepth(); // [1,null,2] // 2
19-
const example3 = maxDepth(); // [1,2,3,null,null,4] // 3
20-
const example4 = maxDepth(); // [] // 0
17+
const testCases = [
18+
{ input: [3,9,20,null,null,15,7], expected: 3 },
19+
{ input: [1,null,2], expected: 2 },
20+
{ input: [1,2,3,null,null,4], expected: 3 },
21+
{ input: [], expected: 0 }
22+
];
23+
24+
const results = testCases.map(({ input }) => maxDepth(createTreeFromArray(input)));
2125

22-
console.log(example1);
23-
console.log(example2);
24-
console.log(example3);
25-
console.log(example4);
26+
console.log(results);

0 commit comments

Comments
 (0)