public ChildType GetMyBranchType(TreeNode node) { TreeNode parent = Parent(node); if (parent == null) return ChildType.NONE; if (parent.Left == node) return ChildType.LEFT_CHILD; else return ChildType.RIGHT_CHILD; }
public void Insert(int value) { if (root == null) { root = new TreeNode(value); return; } TreeNode newNode = new TreeNode(value); TreeNode currNode = root; TreeNode nodeInsertAt = null; while (currNode != null) { nodeInsertAt = currNode; if (newNode.Value <= currNode.Value) currNode = currNode.Left; else currNode = currNode.Right; } if (newNode.Value <= nodeInsertAt.Value) nodeInsertAt.Left = newNode; else nodeInsertAt.Right = newNode; }
public void Clear() { this.root = null; }
private TreeNode MinValNode(TreeNode root) { if (root == null) return null; if (root.Left == null) return root; TreeNode curr = root; while (curr.Left != null) curr = curr.Left; return curr; }
private string PreOrderPrintHelper(TreeNode root) { if (root == null) return ""; string msg = ""; msg = msg + " " + root.Value; msg = msg + PreOrderPrintHelper(root.Left); msg = msg + PreOrderPrintHelper(root.Right); return msg; }
//Path counting private int HeightHelper2(TreeNode currNode) { if (currNode == null) return 0; int leftHeight = 0; if (currNode.Left != null) leftHeight = 1 + HeightHelper2(currNode.Left); int rightHeight = 0; if (currNode.Right != null) rightHeight = 1 + HeightHelper2(currNode.Right); return leftHeight > rightHeight ? leftHeight : rightHeight; }
private TreeNode MaxValNode(TreeNode root) { if (root == null) return null; if (root.Right == null) return root; TreeNode curr = root; while (curr.Right != null) curr = curr.Right; return curr; }
//Nodes counting private int HeightHelper(TreeNode currNode) { if (currNode == null) return 0; else return 1 + compareMax(HeightHelper(currNode.Left), HeightHelper(currNode.Right)); }
public BSTClass(int rootValue) { root = new TreeNode(rootValue); }
private void DeleteHelper(TreeNode ToDelete) { TreeNode toSplice; if (ToDelete.Left == null || ToDelete.Right == null) toSplice = ToDelete; else toSplice = SuccessorNode(ToDelete); TreeNode childNode; if (toSplice.Left != null) childNode = toSplice.Left; else childNode = toSplice.Right; TreeNode toSpliceParent = Parent(toSplice); if (toSpliceParent == null) root = childNode; else { ChildType ToSpliceBranch = GetMyBranchType(toSplice); if (ToSpliceBranch == ChildType.LEFT_CHILD) toSpliceParent.Left = childNode; else toSpliceParent.Right = childNode; } if (toSplice != ToDelete) ToDelete.Value = toSplice.Value; }
public TreeNode SuccessorNode(TreeNode node) { if (node == null) return null; if (node.Right != null) return MinValNode(node.Right); else { TreeNode parent = Parent(node); while (parent != null && GetMyBranchType(node) == ChildType.RIGHT_CHILD) { node = parent; parent = Parent(parent); } return parent; } }
public TreeNode Parent(TreeNode node) { if (node == null || node == root) return null; TreeNode parent = root; TreeNode curr; if (node.Value <= parent.Value) curr = parent.Left; else curr = parent.Right; while (curr != null) { if (node.Value == curr.Value) return parent; else if (node.Value <= curr.Value) { parent = curr; curr = curr.Left; } else { parent = curr; curr = curr.Right; } } return null; }