public BinTree(T val, BinTree <T> parent) { this.val = val; this.parent = parent; }
public bool Remove(T val) { BinTree <T> tree = Search(val); if (tree == null) { return(false); } BinTree <T> curTree; if (tree == this) { if (tree.right != null) { curTree = tree.right; } else { curTree = tree.left; } while (curTree.left != null) { curTree = curTree.left; } T temp = curTree.val; this.Remove(temp); tree.val = temp; return(true); } if (tree.left == null && tree.right == null && tree.parent != null) { if (tree == tree.parent.left) { tree.parent.left = null; } else { tree.parent.right = null; } return(true); } if (tree.left != null && tree.right == null) { tree.left.parent = tree.parent; if (tree == tree.parent.left) { tree.parent.left = tree.left; } else if (tree == tree.parent.right) { tree.parent.right = tree.left; } return(true); } if (tree.left == null && tree.right != null) { tree.right.parent = tree.parent; if (tree == tree.parent.left) { tree.parent.left = tree.right; } else if (tree == tree.parent.right) { tree.parent.right = tree.right; } return(true); } if (tree.right != null && tree.left != null) { curTree = tree.right; while (curTree.left != null) { curTree = curTree.left; } if (curTree.parent == tree) { curTree.left = tree.left; tree.left.parent = curTree; curTree.parent = tree.parent; if (tree == tree.parent.left) { tree.parent.left = curTree; } else if (tree == tree.parent.right) { tree.parent.right = curTree; } return(true); } else { if (curTree.right != null) { curTree.right.parent = curTree.parent; } curTree.parent.left = curTree.right; curTree.right = tree.right; curTree.left = tree.left; tree.left.parent = curTree; tree.right.parent = curTree; curTree.parent = tree.parent; if (tree == tree.parent.left) { tree.parent.left = curTree; } else if (tree == tree.parent.right) { tree.parent.right = curTree; } return(true); } } return(false); }
public bool Pop(T value) // Удаление вершины со значением value { BinTree <T> tree = Search(value); if (tree == null) { return(false); // Если вершины нет в дереве } BinTree <T> curTree; if (tree == this) { if (tree.rightchild != null) { curTree = tree.rightchild; } else { curTree = tree.leftchild; } while (curTree.leftchild != null) { curTree = curTree.leftchild; } T temp = curTree.value; Pop(temp); tree.value = temp; return(true); } if (tree.leftchild == null && tree.rightchild == null && tree.parentnode != null) { if (tree == tree.parentnode.leftchild) { tree.parentnode.leftchild = null; } else { tree.parentnode.rightchild = null; } return(true); } if (tree.leftchild != null && tree.rightchild == null) { tree.leftchild.parentnode = tree.parentnode; if (tree == tree.parentnode.leftchild) { tree.parentnode.leftchild = tree.leftchild; } else if (tree == tree.parentnode.rightchild) { tree.parentnode.rightchild = tree.leftchild; } return(true); } if (tree.leftchild == null && tree.rightchild != null) { tree.rightchild.parentnode = tree.parentnode; if (tree == tree.parentnode.leftchild) { tree.parentnode.leftchild = tree.rightchild; } else if (tree == tree.parentnode.rightchild) { tree.parentnode.rightchild = tree.rightchild; } return(true); } if (tree.rightchild != null && tree.leftchild != null) { curTree = tree.rightchild; while (curTree.leftchild != null) { curTree = curTree.leftchild; } if (curTree.parentnode == tree) { curTree.leftchild = tree.leftchild; tree.leftchild.parentnode = curTree; curTree.parentnode = tree.parentnode; if (tree == tree.parentnode.leftchild) { tree.parentnode.leftchild = curTree; } else if (tree == tree.parentnode.rightchild) { tree.parentnode.rightchild = curTree; } return(true); } else { if (curTree.rightchild != null) { curTree.rightchild.parentnode = curTree.parentnode; } curTree.parentnode.leftchild = curTree.rightchild; curTree.rightchild = tree.rightchild; curTree.leftchild = tree.leftchild; tree.leftchild.parentnode = curTree; tree.rightchild.parentnode = curTree; curTree.parentnode = tree.parentnode; if (tree == tree.parentnode.leftchild) { tree.parentnode.leftchild = curTree; } else if (tree == tree.parentnode.rightchild) { tree.parentnode.rightchild = curTree; } return(true); } } return(false); }