Exemplo n.º 1
0
        private AvlNode InsertWithoutBalancingForTest(AvlNode root, int value)
        {
            if (root == null)
            {
                return(new AvlNode(value));
            }

            if (value < root.Value)
            {
                root.LeftChild = this.InsertWithoutBalancingForTest(root.LeftChild, value);
            }
            else
            {
                root.RightChild = this.InsertWithoutBalancingForTest(root.RightChild, value);
            }

            this.SetNodeHeight(root);
            return(root);
        }
Exemplo n.º 2
0
        private AvlNode Balance(AvlNode root)
        {
            if (this.IsNodeLeftHeavy(root))
            {
                if (this.GetBalanceFactor(root.LeftChild) < 0)
                {
                    root.LeftChild = this.RotateLeft(root.LeftChild);
                }

                return(this.RotateRight(root));
            }

            if (this.IsNodeRightHeavy(root))
            {
                if (this.GetBalanceFactor(root.RightChild) > 0)
                {
                    root.RightChild = this.RotateRight(root.RightChild);
                }

                return(this.RotateLeft(root));
            }

            return(root);
        }
Exemplo n.º 3
0
 public void InsertWithoutBalancingForTest(int value)
 {
     this._root = this.InsertWithoutBalancingForTest(this._root, value);
 }
Exemplo n.º 4
0
 private int GetNodeHeight(AvlNode node)
 {
     return(node?.Height ?? -1);
 }
Exemplo n.º 5
0
 private void SetNodeHeight(AvlNode node)
 {
     node.Height = 1 + Math.Max(this.GetNodeHeight(node.LeftChild), this.GetNodeHeight(node.RightChild));
 }
Exemplo n.º 6
0
 private bool IsNodeRightHeavy(AvlNode node)
 {
     return(this.GetBalanceFactor(node) < -1);
 }
Exemplo n.º 7
0
 private bool IsNodeLeftHeavy(AvlNode node)
 {
     return(this.GetBalanceFactor(node) > 1);
 }
Exemplo n.º 8
0
 public void Insert(int value)
 {
     this._root = this.Insert(this._root, value);
 }