// Добавления значений public void Add(int value) { if (value >= data) { if (rightUnit == null) { rightUnit = new MyStitchedTree(value); } else { rightUnit.Add(value); } } else { if (leftUnit == null) { leftUnit = new MyStitchedTree(value); } else { leftUnit.Add(value); } } }
private MyStitchedTree GetSuccessor(MyStitchedTree node) { MyStitchedTree parentOfSuccessor = node; MyStitchedTree successor = node; MyStitchedTree current = node.RightUnit; while (current != null) { parentOfSuccessor = successor; successor = current; current = current.LeftUnit; } if (successor != node.RightUnit) { parentOfSuccessor.LeftUnit = successor.RightUnit; successor.RightUnit = node.RightUnit; } successor.LeftUnit = node.LeftUnit; return(successor); }
public static int getHeight(MyStitchedTree main) { if (main == null) { return(0); } return(Math.Max(getHeight(main.LeftUnit), getHeight(main.RightUnit)) + 1); }
public void SoftDelete(int data) { MyStitchedTree toDelete = Search(data); if (toDelete != null) { toDelete.Delete(); } }
// Вставки public void Add(int data) { if (main != null) { main.Add(data); } else { main = new MyStitchedTree(data); } }
public static bool isBalanced(MyStitchedTree main) { if (main == null) { return(true); } int heightDiff = getHeight(main.LeftUnit) - getHeight(main.RightUnit); if (Math.Abs(heightDiff) > 1) { return(false); } else { return(isBalanced(main.LeftUnit) && isBalanced(main.RightUnit)); } }
// Поиск прямой public MyStitchedTree Search(int value) { MyStitchedTree thisEl = this; while (thisEl != null) { if (value == this.data && isDeleted == false) { return(thisEl); } else if (value > thisEl.data) { thisEl = thisEl.rightUnit; } else { thisEl = thisEl.leftUnit; } } return(null); }
// Удаление public void Remove(int data) { MyStitchedTree current = main; MyStitchedTree parent = main; bool isLeftChild = false; // Если элемент пустой, то пустой return if (current == null) { return; } // Цикл пока не найдем совпадений while (current != null && current.Data != data) { // Устанавливаем текущую в настоящую parent = current; if (data < current.Data) { // Устанавливаем в левый блок current = current.LeftUnit; isLeftChild = true; } else { // Устанавливаем в правый блок current = current.RightUnit; isLeftChild = false; } } if (current == null) { return; } // Если правая и левая ветки пустые if (current.RightUnit == null && current.LeftUnit == null) { // Если текущая равна основной if (current == main) { main = null; } else { // Если это левая ветка if (isLeftChild) { // Удаляем левый блок parent.LeftUnit = null; } else { // Удаляем правый блок parent.RightUnit = null; } } } else if (current.RightUnit == null) { // Если текущее значение равно основному if (current == main) { // Основному присваивается левая ветвь main = current.LeftUnit; } else { if (isLeftChild) { // Если это левый parent.LeftUnit = current.LeftUnit; } else { // Присваиваем правму блоку левую ветвь parent.RightUnit = current.LeftUnit; } } } else if (current.LeftUnit == null) { if (current == main) { main = current.RightUnit; } else { if (isLeftChild) { parent.LeftUnit = current.LeftUnit; } else { parent.RightUnit = current.RightUnit; } } } else { MyStitchedTree successor = GetSuccessor(current); if (current == main) { main = successor; } else if (isLeftChild) { parent.LeftUnit = successor; } else { parent.RightUnit = successor; } } }