-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeRootAvg.js
More file actions
41 lines (34 loc) · 933 Bytes
/
TreeRootAvg.js
File metadata and controls
41 lines (34 loc) · 933 Bytes
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
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insert(data){
const newNode = new Node(data);
if (!this.root) this.root = newNode;
else this.insertNode(this.root, newNode);
}
insertNode(node, newNode){
if(newNode.data < node.data) {
if(!node.left) return node.left = newNode;
return this.insertNode(node.left, newNode);
} else {
if(!node.right) return node.right = newNode;
return this.insertNode(node.right, newNode);
}
}
avgOnRoot(node){
let data = {};
let result = [];
let index = 0;
if(!node.root) return 'no data';
if(node.left) result.push(node.left.data);
if(node.right) result.push(node.right.data);
}
}