示例#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);
        }
示例#2
0
文件: AVLTree.cs 项目: niko7185/S4.2
        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);
        }
示例#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;
        }
示例#4
0
 public void AddLeftChildTo(BinaryTreeNode <T> parent, T leftChildItem)
 {
     parent.AddLeftChild(leftChildItem);
 }