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