예제 #1
0
 private void DisplayNodeValue(BinaryTreeNode node)
 {
     Console.WriteLine(node.Value);
 }
예제 #2
0
        public void Remove(int value)
        {
            BinaryTreeNode current     = root;
            BinaryTreeNode parent      = root;
            bool           isLeftChild = true;

            while (current.Value != value)
            {
                parent = current;
                if (value < current.Value)
                {
                    isLeftChild = true;
                    current     = current.Left;
                }
                else
                {
                    isLeftChild = false;
                    current     = current.Right;
                }

                if (current.Left == null && current.Right == null)
                {
                    if (current == root)
                    {
                        root = null;
                    }
                    else if (isLeftChild)
                    {
                        parent.Left = null;
                    }
                    else
                    {
                        parent.Right = null;
                    }
                }

                else if (current.Right == null)
                {
                    if (current == root)
                    {
                        root = current.Left;
                    }
                    else if (isLeftChild)
                    {
                        parent.Left = current.Left;
                    }
                    else
                    {
                        parent.Right = current.Left;
                    }
                }

                else if (current.Right == null)
                {
                    if (current == root)
                    {
                        root = current.Right;
                    }
                    else if (isLeftChild)
                    {
                        parent.Left = current.Right;
                    }
                    else
                    {
                        parent.Right = current.Right;
                    }
                }

                else
                {
                    BinaryTreeNode successor = GetSuccessor(current);

                    if (current == root)
                    {
                        root = successor;
                    }
                    else if (isLeftChild)
                    {
                        parent.Left = successor;
                    }
                    else
                    {
                        parent.Right = successor;
                    }

                    successor.Left = current.Left;
                }
            }
        }
예제 #3
0
 public BinaryTree()
 {
     root  = null;
     queue = new Queue <BinaryTreeNode>();
 }