Created
May 2, 2021 08:49
-
-
Save syedjafer/7c0a08eee56768a64fcf5f4548924666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node{ | |
| constructor(data){ | |
| this.data = data; | |
| this.left; | |
| this.righ; | |
| } | |
| insert(data){ | |
| if (this.data != null){ | |
| if (data < this.data){ | |
| if (this.left == null){ | |
| this.left = new Node(data); | |
| }else{ | |
| console.log(this.left); | |
| this.left.insert(data); | |
| } | |
| } | |
| else if(data > this.data){ | |
| if(this.right == null){ | |
| this.right = new Node(data); | |
| }else{ | |
| this.right.insert(data); | |
| } | |
| } | |
| }else{ | |
| this.data = new Node(data); | |
| } | |
| } | |
| print_tree(){ | |
| if (this.left){ | |
| this.left.print_tree(); | |
| } | |
| console.log(this.data+" ,"); | |
| if (this.right){ | |
| this.right.print_tree(); | |
| } | |
| } | |
| } | |
| root = new Node(27) | |
| root.insert(14) | |
| root.insert(35) | |
| root.insert(31) | |
| root.insert(10) | |
| root.insert(19) | |
| root.print_tree() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment