protected BinarySearchTreeNode <T> SearchNode(T value) { BinarySearchTreeNode <T> temp = root; do { if (temp.Value.CompareTo(value) == 0) { return(temp); } if (temp.Value.CompareTo(value) < 0) { temp = temp.Right; } else { temp = temp.Left; } } while (!temp.IsLeaf); return(null); }
private BinarySearchTreeNode <T> GetNodeOfValue(BinarySearchTreeNode <T> tree, T value, ref BinarySearchTreeNode <T> father) { if (tree.Value.CompareTo(value) == 0) { return(tree); } if (tree.Value.CompareTo(value) > 0) { if (tree.Left != null) { father = tree; return(GetNodeOfValue(tree.Left, value, ref father)); } } if (tree.Value.CompareTo(value) < 0) { if (tree.Right != null) { father = tree; return(GetNodeOfValue(tree.Right, value, ref father)); } } return(null); }
private BinarySearchTreeNode <T> GetBigestLeafInTree(BinarySearchTreeNode <T> tree, ref BinarySearchTreeNode <T> father) { if (tree.Left == null && tree.Right == null) { return(tree); } father = tree; if (tree.Right != null) { return(GetBigestLeafInTree(tree.Right, ref father)); } return(GetBigestLeafInTree(tree.Left, ref father)); }
private BinarySearchTreeNode <T> GetSmalestNodeInTree(BinarySearchTreeNode <T> tree, ref BinarySearchTreeNode <T> father) { if (tree.Left == null) { return(tree); } father = tree; return(GetSmalestNodeInTree(tree.Left, ref father)); }
public BSTIterator(BinarySearchTreeNode <T> root, int length) { this.nodesSorted = new T[length]; InOrder(root); }
public bool Remove(T value) { BinarySearchTreeNode <T> toRemoveFather = null; BinarySearchTreeNode <T> toRemove = GetNodeOfValue(root, value, ref toRemoveFather); if (toRemove == null) { return(false); } if (toRemove.IsLeaf) { if (toRemoveFather == null) { root = null; } else if (value.CompareTo(toRemoveFather.Value) < 0) { toRemoveFather.Left = null; } else { toRemoveFather.Right = null; } count--; return(true); } if (toRemove.Left != null && toRemove.Right != null) { BinarySearchTreeNode <T> toSwitchFather = toRemove; BinarySearchTreeNode <T> toSwithc = GetSmalestNodeInTree(toRemove.Right, ref toSwitchFather); toSwitchFather.Left = toSwithc.Right; toSwithc.Left = toRemove.Left; toSwithc.Right = toRemove.Right; if (toRemoveFather == null) { root = toSwithc; return(true); } if (value.CompareTo(toRemoveFather.Value) < 0) { toRemoveFather.Left = toSwithc; } else { toRemoveFather.Right = toSwithc; } count--; return(true); } if (toRemoveFather == null) { if (toRemove.Left != null) { root = toRemove.Left; } else { root = toRemove.Right; } count--; return(true); } if (value.CompareTo(toRemoveFather.Value) < 0) { if (toRemove.Left != null) { toRemoveFather.Left = toRemove.Left; } else { toRemoveFather.Left = toRemove.Right; } count--; return(true); } if (toRemove.Left != null) { toRemoveFather.Right = toRemove.Left; } else { toRemoveFather.Right = toRemove.Right; } count--; return(true); }