Пример #1
0
        public static void Traverse(AvlNode root)
        {
            Stack <AvlNode> treeStack = new Stack <AvlNode>();

            while (true)
            {
                GoAlongLeftBranch(root, treeStack);
                if (treeStack.Count <= 0)
                {
                    break;
                }
                root = treeStack.Pop();
                Console.Write(root.data + " ");
                root = root.rchild;
            }
        }
Пример #2
0
 public AvlNode(int data, AvlNode parent)
 {
     this.data   = data;
     this.parent = parent;
 }
Пример #3
0
 public void SetParent(AvlNode node)
 {
     parent = node;
 }
Пример #4
0
 public static void UpdateHeight(AvlNode node)
 {
     node.height = Math.Max(height(node.lchild), height(node.rchild)) + 1;
 }
Пример #5
0
        public AvlNode Insert(int data)
        {
            AvlNode node = Search(data);
            AvlNode res  = null;

            //查找失败的时候才插入
            if (node == null)
            {
                node = new AvlNode(data, _hot);
                if (data < _hot.data)
                {
                    _hot.lchild = node;
                }
                else
                {
                    _hot.rchild = node;
                }
                _size++;

                //从下往上找 一旦发现有一个祖先失衡 必将引起多个祖先失衡
                bool isBalanced = true;
                while (node.parent != null)
                {
                    node = node.parent;
                    if (height(node.lchild) - height(node.rchild) == 2)
                    {
                        isBalanced = false;
                        //左左型
                        if (height(node.lchild) - height(node.lchild.lchild) == 1)
                        {
                            res = R_Rotate(node);
                        }
                        //左右型
                        else
                        {
                            res = L_R_Rotate(node);
                        }
                    }
                    else if (height(node.lchild) - height(node.rchild) == -2)
                    {
                        isBalanced = false;
                        //右右型
                        if (height(node.rchild) - height(node.rchild.rchild) == 1)
                        {
                            res = L_Rotate(node);
                        }
                        //右左型
                        else
                        {
                            res = R_L_Rotate(node);
                        }
                    }
                    if (!isBalanced)
                    {
                        break;
                    }
                    else
                    {
                        //平衡性虽然不变 但是高度可能改变
                        UpdateHeight(node);
                    }
                }
            }

            if (node != null)
            {
                _root = node;
                node  = node.parent;
            }

            return(node);
        }
Пример #6
0
 //右左型 先右转 再左转
 public static AvlNode R_L_Rotate(AvlNode node)
 {
     node.rchild = R_Rotate(node.rchild);
     return(L_Rotate(node));
 }
Пример #7
0
 //左右型 先左转 再右转
 public static AvlNode L_R_Rotate(AvlNode node)
 {
     node.lchild = L_Rotate(node.lchild);
     return(R_Rotate(node));
 }