class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private Node InsertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = InsertRec(root.left, val); else root.right = InsertRec(root.right, val); return root; } public void InOrder(Node node) { if (node == null) return; InOrder(node.left); System.Console.Write(node.val + " "); InOrder(node.right); } } var tree = new BST(); tree.Insert(10); tree.Insert(5); tree.Insert(15); tree.Insert(3); tree.Insert(7); tree.InOrder(tree.root);
In-order traversal visits left subtree, root, then right subtree. After inserting 10, 5, 15, 3, 7, the BST in-order traversal prints sorted values: 3 5 7 10 15.
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private Node InsertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = InsertRec(root.left, val); else root.right = InsertRec(root.right, val); return root; } public void PreOrder(Node node) { if (node == null) return; System.Console.Write(node.val + " "); PreOrder(node.left); PreOrder(node.right); } } var tree = new BST(); tree.Insert(20); tree.Insert(10); tree.Insert(30); tree.Insert(25); tree.Insert(35); tree.PreOrder(tree.root);
Pre-order traversal prints root first, then left subtree, then right subtree. After insertion, the root is 20, left child 10, right child 30 with children 25 and 35.
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private Node InsertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = InsertRec(root.left, val); else if (val > root.val) root.right = InsertRec(root.right, val); return root; } } var tree = new BST(); tree.Insert(10); tree.Insert(10);
The code does not handle the case when val equals root.val, so it neither inserts left nor right. This causes infinite recursion on the same node, leading to StackOverflowException.
In the worst case, the BST becomes a linked list (all nodes on one side). Inserting requires traversing all nodes, so time complexity is O(n).
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private Node InsertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = InsertRec(root.left, val); else root.right = InsertRec(root.right, val); return root; } public void PostOrder(Node node) { if (node == null) return; PostOrder(node.left); PostOrder(node.right); System.Console.Write(node.val + " "); } } var tree = new BST(); int[] values = {50, 30, 70, 20, 40, 60, 80}; foreach (var v in values) tree.Insert(v); tree.PostOrder(tree.root);
Post-order traversal prints left subtree nodes first, then right subtree nodes, then the root. For the given insertions, the output is 20 40 30 60 80 70 50.
