-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNodeDOM.js
67 lines (62 loc) · 2.73 KB
/
NodeDOM.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
class NodeDOM{
constructor(val){
this.value = val;
this.element = document.createElement("div");
this.element.className = "node";
this.element.innerHTML = ` <p><span class="value">${this.value}</span></p>
<div class="horizontal-line-center"></div>
<div class="left-right-container">
<div class="left">
</div>
<div class="right">
</div>
</div>`
this.$left = this.element.getElementsByClassName(`left`)[0];
this.$right = this.element.getElementsByClassName(`right`)[0];
const nodeHTML = ` <div class="node">
<p><span class="value">${this.value}</span></p>
<div class="horizontal-line-center"></div>
<div class="left-right-container">
<div class="left">
</div>
<div class="right">
</div>
</div>
</div>`
}
insert(val){
if (val < this.value){
if(!this.$left.children.length > 0){
this.left = new NodeDOM(val);
this.$left.appendChild (this.left.element);
return this.left;
}else{
return this.left.insert(val);
}
}else if(val > this.value){
if(!this.$right.children.length > 0){
this.right = new NodeDOM(val);
this.$right.appendChild (this.right.element);
return this.right;
}else{
return this.right.insert(val);
}
}
return 0;
}
search(val){
if(this.value === val){
console.log("OK")
return this;
}else if(this.value > val){
console.log("going left");
return this.left ? this.left.search(val) : false;
}else if(this.value < val){
console.log("going right");
return this.right ? this.right.search(val) : false;
}
}
traverse(){
return `${this.left? this.left.traverse() : ''}${this.value}\n${this.right? this.right.traverse() : ''}`
}
}