Beispiel #1
0
 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);
 }
Beispiel #2
0
 public BST(BSTNode <T> node)
 {
     Root   = node;
     _count = Root == null ? 0 : 1;
 }
Beispiel #3
0
        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);
        }
Beispiel #4
0
 public BSTFind()
 {
     Node = null;
 }
Beispiel #5
0
 BSTNode <T> GetNextNode(BSTNode <T> node, bool isRight)
 {
     return(isRight ? node.RightChild : node.LeftChild);
 }