Skip to content

Commit 80604f8

Browse files
shiroginabranhe
authored andcommitted
Binarry Tree
-Create Random Tree -Add Values -find some values
1 parent 1ba8b51 commit 80604f8

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Classes/Tree.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// JavaScript implementation of Binary Search
2+
3+
// Author: Youcef Madadi
4+
5+
// Binary Tree
6+
export default class BinaryTree{
7+
left=null;
8+
right=null;
9+
value;
10+
11+
constructor(value,left,right) {
12+
this.value = value;
13+
if(left instanceof BinaryTree)this.left = left;
14+
if(right instanceof BinaryTree)this.right= right;
15+
}
16+
AddValue(value){
17+
if(value>this.value){
18+
if(this.right) return this.right.AddValue(value);
19+
this.right=new BinaryTree(value)
20+
}else if(value<this.value){
21+
if(this.left) return this.left.AddValue(value);
22+
this.left=new BinaryTree(value)
23+
}else return;
24+
}
25+
Print(){
26+
if(this.left) this.left.Print();
27+
console.log(this.value)
28+
if(this.right) this.right.Print();
29+
}
30+
PrintLRM(){
31+
if(this.left) this.left.Print();
32+
if(this.right) this.right.Print();
33+
console.log(this.value)
34+
}
35+
PrintRML(){
36+
if(this.right) this.right.Print();
37+
console.log(this.value)
38+
if(this.left) this.left.Print();
39+
}
40+
findValue(value){
41+
if( this.value > value ) {
42+
if(this.left)return this.left.findValue(value);
43+
}
44+
else if( this.value < value ) {
45+
if(this.right) return this.right.findValue(value);
46+
}
47+
else return true
48+
return false;
49+
}
50+
static CreateRandomTree(){
51+
let root=new BinaryTree(Math.floor(Math.random() * 100)),
52+
deep= Math.floor(Math.random() * 50);
53+
for( let i=0 ; i<deep; i++){
54+
root.AddValue(Math.floor(Math.random() * 100))
55+
}
56+
return root;
57+
}
58+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy