Exemplo n.º 1
0
        private bool InsertItem(T item, int level, BinaryTreeNode <T> root)
        {
            if (root == null || level < 1)
            {
                return(false);
            }

            if (level <= 2)
            {
                if (root.LeftChild == null)
                {
                    root.AddLeftChild(item);
                    UpHeapify(root.LeftChild);
                    return(true);
                }
                else if (root.RightChild == null)
                {
                    root.AddRightChild(item);
                    UpHeapify(root.RightChild);
                    return(true);
                }
            }

            bool inserted = false;

            inserted = InsertItem(item, level - 1, root.LeftChild);

            if (!inserted)
            {
                inserted = InsertItem(item, level - 1, root.RightChild);
            }

            return(inserted);
        }
Exemplo n.º 2
0
        private void RotateRight(BinaryTreeNode <T> node)
        {
            BinaryTreeNode <T> rotatedRoot = new BinaryTreeNode <T>(node.Value);

            rotatedRoot.AddRightChild(node.RightChild);

            rotatedRoot.AddLeftChild(node.LeftChild.RightChild);

            node.Value = node.LeftChild.Value;


            node.AddLeftChild(node.LeftChild.LeftChild);

            node.AddRightChild(rotatedRoot);
        }
Exemplo n.º 3
0
        public void AddRightChild(BinaryTreeNode <T> node)
        {
            if (node == null)
            {
                rightChild = null;
                return;
            }

            BinaryTreeNode <T> treeNode = new BinaryTreeNode <T>(node.Value, this);

            treeNode.AddLeftChild(node.LeftChild);

            treeNode.AddRightChild(node.RightChild);

            rightChild = treeNode;
        }
Exemplo n.º 4
0
 public void AddRightChildTo(BinaryTreeNode <T> parent, T rightChildItem)
 {
     parent.AddRightChild(rightChildItem);
 }