Last active
October 23, 2019 08:08
-
-
Save melvincabatuan/d8cdc92700fd6573da5ffc586315c675 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
// Cobalt-mkc: 10/23/2019 | |
class Node { | |
int data; | |
Node left, right; | |
Node(int d) { | |
data = d; | |
left = right = null; | |
} | |
} | |
class BSTDemo { | |
static Node head; | |
Node insert(Node node, int data) { | |
if (node == null) { | |
return (new Node(data)); | |
} else { | |
if (data <= node.data) { | |
node.left = insert(node.left, data); | |
} else { | |
node.right = insert(node.right, data); | |
} | |
return node; | |
} | |
} | |
int minvalue(Node node) { | |
Node current = node; | |
while (current.left != null) { | |
current = current.left; | |
} | |
return (current.data); | |
} | |
public static void main(String[] args) { | |
BSTDemo tree = new BSTDemo(); | |
Node root = null; | |
root = tree.insert(root, 4); | |
tree.insert(root, 2); | |
tree.insert(root, 1); | |
tree.insert(root, 3); | |
tree.insert(root, 6); | |
tree.insert(root, 5); | |
System.out.println("The minimum value of BST is " + tree.minvalue(root)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment