//find the node with the given identifier among descendants of parent and parent //uses pre-order traversal //O(log(n)) worst O(n) for unbalanced tree internal static BSTNodeBase <T> Find <T>(this BSTNodeBase <T> current, T value) where T : IComparable { while (true) { if (current == null) { return(null); } var compareResult = current.Value.CompareTo(value); if (compareResult == 0) { return(current); } if (compareResult > 0) { current = current.Left; } else { current = current.Right; } } }
//O(log(n)) worst O(n) for unbalanced tree internal static int GetHeight <T>(this BSTNodeBase <T> node) where T : IComparable { if (node == null) { return(-1); } return(Math.Max(GetHeight(node.Left), GetHeight(node.Right)) + 1); }
internal static int AssignCount <T>(BSTNodeBase <T> node) where T : IComparable { if (node == null) { return(0); } node.Count = AssignCount(node.Left) + AssignCount(node.Right) + 1; return(node.Count); }
internal int assignCount(BSTNodeBase <T> node) { if (node == null) { return(0); } node.Count = assignCount(node.Left) + assignCount(node.Right) + 1; return(node.Count); }
internal static BSTNodeBase <T> FindMin <T>(this BSTNodeBase <T> node) where T : IComparable { if (node == null) { return(null); } while (true) { if (node.Left == null) { return(node); } node = node.Left; } }
//get the kth smallest element under given node internal static BSTNodeBase <T> KthSmallest <T>(this BSTNodeBase <T> node, int k) where T : IComparable { var leftCount = node.Left != null ? node.Left.Count : 0; if (k == leftCount) { return(node); } if (k < leftCount) { return(KthSmallest(node.Left, k)); } return(KthSmallest(node.Right, k - leftCount - 1)); }
internal static BSTNodeBase <T> NextLower <T>(this BSTNodeBase <T> node) where T : IComparable { //root or left child if (node.Parent == null || node.IsLeftChild) { if (node.Left != null) { node = node.Left; while (node.Right != null) { node = node.Right; } return(node); } else { while (node.Parent != null && node.IsLeftChild) { node = node.Parent; } return(node?.Parent); } } //right child else { if (node.Left != null) { node = node.Left; while (node.Right != null) { node = node.Right; } return(node); } else { return(node.Parent); } } }
internal static void UpdateCounts <T>(this BSTNodeBase <T> node, bool spiralUp = false) where T : IComparable { while (node != null) { int leftCount = node.Left?.Count ?? 0; var rightCount = node.Right?.Count ?? 0; node.Count = leftCount + rightCount + 1; node = node.Parent; if (!spiralUp) { break; } } }
public bool MoveNext() { if (root == null) { return(false); } if (current == null) { current = root.FindMin(); return(true); } var next = current.NextHigher(); if (next != null) { current = next; return(true); } return(false); }
//get the sorted order position of given item under given node internal static int Position <T>(this BSTNodeBase <T> node, T item) where T : IComparable { if (node == null) { return(-1); } var leftCount = node.Left != null ? node.Left.Count : 0; if (node.Value.CompareTo(item) == 0) { return(leftCount); } if (item.CompareTo(node.Value) < 0) { return(Position(node.Left, item)); } var position = Position(node.Right, item); return(position < 0 ? position : position + leftCount + 1); }
public void Dispose() { current = null; }
public void Reset() { current = root; }
internal BSTEnumerator(BSTNodeBase <T> root) { this.root = root; }
//find the node with the given identifier among descendants of parent and parent //uses pre-order traversal //O(log(n)) worst O(n) for unbalanced tree internal static (BSTNodeBase <T>, int) Find <T>(this BSTNodeBase <T> current, T value) where T : IComparable
internal BSTEnumerator(BSTNodeBase <T> root, bool asc = true) { this.root = root; this.asc = asc; }