Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78cb213

Browse files
committed
update: implementation of Inorder Traversal
1 parent 34593cb commit 78cb213

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

‎src/_DataStructures_/Trees/BST/index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,37 @@ class BinarySearchTree {
3939
arr = [...arr, ...right];
4040
return arr;
4141
}
42+
43+
inorder(root) {
44+
if (root === null) return [];
45+
let arr = [];
46+
const left = this.inorder(root.leftChild);
47+
arr = [...left, ...arr];
48+
49+
// print root
50+
arr = [...arr, root.value];
51+
52+
const right = this.inorder(root.rightChild);
53+
arr = [...arr, ...right];
54+
return arr;
55+
}
4256
}
4357

44-
// const bst = new BinarySearchTree(10);
58+
// const bst = new BinarySearchTree(6);
4559
// console.log(bst.root);
46-
// bst.insert(bst.root, 12);
60+
// bst.insert(bst.root, 4);
4761
// bst.insert(bst.root, 9);
48-
// bst.insert(bst.root, 19);
49-
// bst.insert(bst.root, 11);
50-
// bst.insert(bst.root, 6);
62+
// bst.insert(bst.root, 2);
63+
// bst.insert(bst.root, 5);
64+
// bst.insert(bst.root, 8);
65+
// bst.insert(bst.root, 12);
5166

5267
// console.log(bst.root);
5368

54-
// const a = bst.preorder(bst.root);
55-
// console.log('arr = ', a);
69+
// const preorder = bst.preorder(bst.root);
70+
// console.log('Preorder Traversal - ', preorder);
71+
72+
// const inorder = bst.inorder(bst.root);
73+
// console.log('Inorder Traversal - ', inorder);
5674

5775
module.exports = BinarySearchTree;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /