-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP-Tree.js
83 lines (71 loc) · 1.77 KB
/
P-Tree.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
// practice treee
class Node{
constructor(val){
this.value = val;
this.left = null;
this.right = null;
}
}
class BtsTree{
constructor(){
this.root = null // default
}
isEmpty(){ // checking root is empty or not
return this.root === null;
}
// creating a tree
makeTree(val){
let newNode = new Node(val)
console.log("newNode" , newNode)
if(this.root == null){ // if node is empty insert value here
this.root = newNode;
}
else{ // if nor call a function and find better place
this.insertNode(this.root , newNode)
}
}
insertNode(root , newNode){
if(root.value > newNode.value){
if(root.left == null){
root.left = newNode
}
else{
this.insertNode(root.left , newNode)
}
}
else{
if(root.right === null){
root.right = newNode
}
else{
this.insertNode(root.right , newNode)
}
}
}
// searching
search(root , val){
if(root === null){ // checking if root is false
return false;
}
else if(root.value === val){ // checking the root value is equal to value
return true;
}
else if(root.value > val){
return this.search(root.left , val);
}
else{
return this.search(root.right ,val);
}
}
}
let bts = new BtsTree()
bts.makeTree(15)
bts.makeTree(6)
bts.makeTree(55)
bts.makeTree(12)
bts.makeTree(16)
bts.makeTree(77)
bts.makeTree(3)
console.log(bts.search(bts.root , 77))
// console.log(bts.isEmpty())
// console.warn(bts.root) // checking every value