コード例 #1
0
 //空构造函数
 public MyAvlTree()
 {
     this.Root = null;
 }
コード例 #2
0
 /// <summary>
 /// 树的清空操作
 /// </summary>
 public void ClearTree()
 {
     this.ClearTree(this.Root);
     this.Root = null;
 }
コード例 #3
0
        /// <summary>
        /// 树形打印一棵树
        /// </summary>
        /// <param name="tree"></param>
        public void PrintTree4(AvlTree tree)
        {
            if (this.Root == null)
            {
                //Console.WriteLine("null");
                return;
            }
            Queue <AvlTree> queue = new Queue <AvlTree>();
            AvlTree         str   = new AvlTree()
            {
                StrData = "*"
            };

            queue.Enqueue(tree);
            int Level = this.NodeDepth(this.Root);

            AvlTree temp;
            AvlTree NowLast    = this.Root;
            AvlTree NextLast   = null;
            bool    LayerFirst = true;


            while (queue.Count > 0)
            {
                temp = queue.Dequeue();
                double First = this.BackFirst(Level);
                double After = this.BackAfter(Level);
                if (LayerFirst == true)
                {
                    this.PrintSpace(First);
                }

                if (temp.Data == 0)
                {
                    Console.Write(temp.StrData);
                    LayerFirst = false;
                }
                else
                {
                    //Console.Write(temp.Data);
                    Console.Write(temp.Data);
                    LayerFirst = false;
                }
                if (LayerFirst == false)
                {
                    this.PrintSpace(After);
                }

                if (temp.LeftChild == null)
                {
                    queue.Enqueue(str);
                }
                else
                {
                    queue.Enqueue(temp.LeftChild);
                    NextLast = temp.LeftChild;
                }


                if (temp.RightChild == null)
                {
                    queue.Enqueue(str);
                }
                else
                {
                    queue.Enqueue(temp.RightChild);
                    NextLast = temp.RightChild;
                }


                if (temp == NowLast)
                {
                    //if (temp != this.Root && temp.ParentNode.RightChild == null) {
                    //    //After = this.BackAfter(Level);
                    //    //this.PrintSpace(After);
                    //    temp = queue.Dequeue();
                    //    Console.Write(temp.StrData);

                    //}
                    Console.WriteLine();
                    LayerFirst = true;
                    Level--;
                    NowLast = NextLast;
                }
                if (Level == 0)
                {
                    return;
                }
            }
        }
コード例 #4
0
 public void InsertNode(int item)
 {
     Root = InsertNode(Root, item);
 }
コード例 #5
0
 /// <summary>
 /// 右左双结构
 ///
 ///      4                  4                       6
 ///    /   \              /  \                    /   \
 ///   2      7      ==   2    6       ==         4      7
 ///        /   \            /   \              /   \     \
 ///       6     8          5      7           2     5     8
 ///      /                         \
 ///     5                           8
 ///
 /// </summary>
 /// <param name="A"></param>
 /// <returns></returns>
 private AvlTree RightLeftRotation(AvlTree node)
 {
     node.RightChild = LeftLeftRotation(node.RightChild);
     return(RightRightRotation(node));
 }
コード例 #6
0
 /// <summary>
 /// 左右双结构,当左边太高时
 ///
 ///           8                 8
 ///         /   \             /   \                  6
 ///        5     12  ==      6     12    ==        /   \
 ///      /   \             /   \                  5      8
 ///     3     6           5     7                /      /  \
 ///            \         /                      3      7   12
 ///             7       3
 ///
 /// </summary>
 /// <param name="A"></param>
 private AvlTree LeftRightRotation(AvlTree node)
 {
     node.LeftChild = RightRightRotation(node.LeftChild);
     return(LeftLeftRotation(node));
 }