diff --git a/Miscellaneous/Binary_Search_Tree.java b/Miscellaneous/Binary_Search_Tree.java new file mode 100644 index 0000000..f9131b0 --- /dev/null +++ b/Miscellaneous/Binary_Search_Tree.java @@ -0,0 +1,58 @@ +public class Binary_Search_Tree { + + private static class Node { + int data; + Node left; + Node right; + + public Node(int data) { + this.data = data; + } + } + + public static Node insert(Node root, int val) { + + if (root == null) { + root = new Node(val); + return root; + } + + if (root.data > val) { + root.left = insert(root.left, val); + } else { + root.right = insert(root.right, val); + } + + return root; + } + + public static void inorder(Node root) { + + if(root == null) + return; + + inorder(root.left); + + System.out.println(root.data + " "); + + inorder(root.right); + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + int value[] = {3, 4, 6, 2, 7, 9, 5, 8}; + + Node root = null; + + for(int i = 0; i < value.length; i++) { + root = insert(root, value[i]); + } + + inorder(root); + } + +} + + +// Source: https://www.youtube.com/shorts/PVazOskZlfY \ No newline at end of file