public static void Insert(int number) { if (RootSubTree == null) { RootSubTree = new SubTree {Key = number}; } else { Insert(RootSubTree, number); } }
public static SubTree Maximum(SubTree subTree) { if (subTree.RightSubTree != null) { return Maximum(subTree.RightSubTree); } return subTree; }
private static void PrintTree(SubTree subTree) { if (subTree.LeftSubTree != null) { PrintTree(subTree.LeftSubTree); } Console.Write(" " + subTree.Key + " "); if (subTree.RightSubTree != null) { PrintTree(subTree.RightSubTree); } }
private static SubTree Minimum(SubTree subTree) { if (subTree.LeftSubTree != null) { return Minimum(subTree.LeftSubTree); } return subTree; }
private static void Insert(SubTree subtree, int number) { if (Find(number) == null) { if (number < subtree.Key) { if (subtree.LeftSubTree != null) { Insert(subtree.LeftSubTree, number); } else { subtree.LeftSubTree = new SubTree {Key = number}; } } else { if (subtree.RightSubTree != null) { Insert(subtree.RightSubTree, number); } else { subtree.RightSubTree = new SubTree {Key = number}; } } } }
private static SubTree Find(SubTree subTree, int key) { if (subTree.Key == key) { return subTree; } if (key < subTree.Key) { if (subTree.LeftSubTree != null) { return Find(subTree.LeftSubTree, key); } } else { if (subTree.RightSubTree != null) { return Find(subTree.RightSubTree, key); } } return null; }
private static void DeleteSubTree(SubTree subTree, SubTree lastTree, int key) { if (subTree.Key == key) { // No Children if (subTree.LeftSubTree == null && subTree.RightSubTree == null) { if (key < lastTree.Key) { lastTree.LeftSubTree = null; } else { lastTree.RightSubTree = null; } } // Two Children else if (subTree.LeftSubTree != null && subTree.RightSubTree != null) { SubTree minSubTree = Minimum(subTree.LeftSubTree); minSubTree.RightSubTree = subTree.RightSubTree; if (key < lastTree.Key) { lastTree.LeftSubTree = minSubTree; } else { lastTree.RightSubTree = minSubTree; } } // One Child else { if (subTree.LeftSubTree != null) { if (key < lastTree.Key) { lastTree.LeftSubTree = subTree.LeftSubTree; } else { lastTree.RightSubTree = subTree.LeftSubTree; } } else { if (key < lastTree.Key) { lastTree.LeftSubTree = subTree.RightSubTree; } else { lastTree.RightSubTree = subTree.RightSubTree; } } } } else if (key < subTree.Key) { if (subTree.LeftSubTree != null) { DeleteSubTree(subTree.LeftSubTree, subTree, key); } } else { if (subTree.RightSubTree != null) { DeleteSubTree(subTree.RightSubTree, subTree, key); } } }