Пример #1
0
        public void DeleteNode(BinaryNode node)
        {
            if (Root == null)
            {
                _logger.Log("Tree is empty.");
                return;
            }
            var curNode = Root;
            var parent  = Root;

            while (true)
            {
                if (curNode == null)
                {
                    _logger.Log("Node does not exist in tree.");
                    return;
                }

                if (curNode.Key < node.Key)
                {
                    parent  = curNode;
                    curNode = curNode.Right;
                    continue;
                }

                if (curNode.Key > node.Key)
                {
                    parent  = curNode;
                    curNode = curNode.Left;
                    continue;
                }

                if (curNode == node)
                {
                    if (curNode.Right == null && curNode.Left == null)
                    {
                        if (curNode == Root)
                        {
                            Root = null;
                            return;
                        }

                        if (parent.Right == curNode)
                        {
                            parent.Right = null;
                        }

                        if (parent.Left == curNode)
                        {
                            parent.Left = null;
                        }
                    }

                    if (curNode.Right != null)
                    {
                        parent.Right = curNode.Right;
                    }

                    if (curNode.Left != null)
                    {
                        InsertNode(curNode.Left);
                    }

                    break;
                }
            }
        }
Пример #2
0
 public BinarySearchTree(BinaryNode root, ILogger logger)
 {
     Root    = root;
     _logger = logger;
 }