forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Tree Inorder Traversal in JS (jainaman224#2586)
* Create Tree_Inorder_Traversal.js * Update Tree_Inorder_Traversal.js
- Loading branch information
1 parent
225ddd0
commit ed8d9ec
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Program to traverse a tree in Inorder. | ||
*/ | ||
|
||
// Creates a Tree Node with input value | ||
class Node { | ||
constructor(value) { | ||
this.value = value; | ||
this.left = null; | ||
this.right = null; | ||
} | ||
} | ||
|
||
// Function for In Order Tree Transversal | ||
function InOrder(root) { | ||
if (root) { | ||
InOrder(root.left); | ||
console.log(root.value); | ||
InOrder(root.right); | ||
} | ||
} | ||
|
||
// Sample Input | ||
var root = new Node(1); | ||
root.left = new Node(2); | ||
root.right = new Node(3); | ||
root.left.left = new Node(4); | ||
root.left.right = new Node(5); | ||
root.right.left = new Node(6); | ||
root.right.right = new Node(7); | ||
|
||
// Sample Output | ||
console.log("In Order traversal of tree is:-"); | ||
InOrder(root); | ||
|
||
/* Output | ||
In Order traversal of tree is:- 4 2 5 1 6 3 7 | ||
*/ |