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