public void DeepAllNodes(BSTNode <T> currentNode, int iterationType, List <BSTNode <T> > nodes) { DFSStep(currentNode, GetBranch(iterationType, 0), iterationType, nodes); DFSStep(currentNode, GetBranch(iterationType, 1), iterationType, nodes); DFSStep(currentNode, GetBranch(iterationType, 2), iterationType, nodes); }
public BST(BSTNode <T> node) { Root = node; _count = Root == null ? 0 : 1; }
public bool DeleteNodeByKey(int key) { var nodeToDelete = FindNodeByKey(key); if (!nodeToDelete.NodeHasKey) { return(false); } // Мы хотим удалить лист if (nodeToDelete.Node.LeftChild == null && nodeToDelete.Node.RightChild == null) { if (nodeToDelete.Node.Parent == null) { Root = null; } else { BreakNode(nodeToDelete.Node); } } // Мы хотим удалить узел с только левым потомком else if (nodeToDelete.Node.LeftChild != null && nodeToDelete.Node.RightChild == null) { var insertNode = nodeToDelete.Node.LeftChild; if (nodeToDelete.Node.Parent == null) { Root = insertNode; insertNode.Parent = null; } else { BreakNode(nodeToDelete.Node); InsertNode(nodeToDelete.Node.Parent, insertNode, nodeToDelete.Node.NodeKey); } } // Узел с правым потомком else { var insertNode = FinMinMax(nodeToDelete.Node.RightChild, false); BreakNode(insertNode); if (nodeToDelete.Node.Parent == null) { insertNode.LeftChild = nodeToDelete.Node.LeftChild; insertNode.RightChild = nodeToDelete.Node.RightChild; nodeToDelete.Node.LeftChild.Parent = insertNode; nodeToDelete.Node.RightChild.Parent = insertNode; Root = insertNode; insertNode.Parent = null; } else { BreakNode(nodeToDelete.Node); InsertNode(nodeToDelete.Node.Parent, insertNode, nodeToDelete.Node.NodeKey); insertNode.LeftChild = nodeToDelete.Node.LeftChild; insertNode.RightChild = nodeToDelete.Node.RightChild; nodeToDelete.Node.LeftChild.Parent = insertNode; nodeToDelete.Node.RightChild.Parent = insertNode; } } _count--; return(true); }
public BSTFind() { Node = null; }
BSTNode <T> GetNextNode(BSTNode <T> node, bool isRight) { return(isRight ? node.RightChild : node.LeftChild); }