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 ef9b43b

Browse files
committed
maximum in BST
1 parent 86b0a1a commit ef9b43b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
class Node {
3+
constructor(data) {
4+
this.data = data;
5+
this.leftNode = this.rightNode = null;
6+
}
7+
}
8+
9+
function insertInBST(root, data) {
10+
if (root == null) return new Node(data);
11+
else if (root.data > data) root.leftNode = insertInBST(root.leftNode, data);
12+
else root.rightNode = insertInBST(root.rightNode, data);
13+
return root;
14+
}
15+
16+
function inorderDisplay(root) {
17+
if (root == null) return;
18+
inorderDisplay(root.leftNode);
19+
console.log(root.data);
20+
inorderDisplay(root.rightNode);
21+
}
22+
23+
function maxInBST(root) {
24+
if (root == null) return null;
25+
if (root.rightNode == null) return root;
26+
return maxInBST(root.rightNode);
27+
}
28+
29+
let tree = null;
30+
tree = insertInBST(tree, 4);
31+
tree = insertInBST(tree, 2);
32+
tree = insertInBST(tree, 3);
33+
tree = insertInBST(tree, 1);
34+
tree = insertInBST(tree, 6);
35+
tree = insertInBST(tree, 5);
36+
tree = insertInBST(tree, 7);
37+
console.log('Maximum In BST', maxInBST(tree).data);

0 commit comments

Comments
(0)

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