當前位置: 首頁>>代碼示例>>C#>>正文


C# BinarySearchTree.Height方法代碼示例

本文整理匯總了C#中BinarySearchTree.Height方法的典型用法代碼示例。如果您正苦於以下問題:C# BinarySearchTree.Height方法的具體用法?C# BinarySearchTree.Height怎麽用?C# BinarySearchTree.Height使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BinarySearchTree的用法示例。


在下文中一共展示了BinarySearchTree.Height方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: DoTest


//.........這裏部分代碼省略.........
             **           /                        \
             **          6                          7
             **
             **************************************************************************
             */
            avlTree.Insert(6); // insert

            // ASSERT CASE 4
            AssertCase_4(avlTree);

            //
            // CASE #5
            // REMOVE the tree's root: 4.
            //
            /**************************************************************************
             ** UNBALANCED     ===>    TRANSITION (1st R)    ===>    BALANCED (2nd Rt)
             **       null                                              .6..
             **      /   \                                             /    \
             **     2     6    ===>                      ===>         2      7
             **    / \   / \                                         / \    /
             **   1   3 5   7                                       1   3  5
             **
             **************************************************************************
             */
            avlTree.Remove(avlTree.Root.Value); // REMOVE 4

            // ASSERT CASE 5
            AssertCase_5(avlTree);

            //
            // CLEAR THE TREE AND START OVER
            // Compare two binary trees with each other (height-wise) using bulk-inserts

            avlTree = new AVLTree<int>();
            var bsTree = new BinarySearchTree<int>();

            List<int> treeDataList = new List<int>() { 15, 25, 5, 12, 1, 16, 20, 9, 9, 7, 7, -1, 11, 19, 30, 8, 10, 13, 28, 39 };
            avlTree.Insert(treeDataList);
            bsTree.Insert(treeDataList);

            int avlTreeHeight = avlTree.Height();
            int bsTreeHeight = bsTree.Height();

            Debug.Assert(avlTreeHeight < bsTreeHeight, "Wrong heights. AVL Tree must be shorted than BS Tree.");

            //
            // Draw the tree to the console.
            Console.WriteLine(String.Format("************\r\n** BST TREE:\r\n************\r\n"));
            Console.WriteLine(bsTree.DrawTree());

            Console.WriteLine("\r\n\r\n");

            Console.WriteLine(String.Format("************\r\n** AVL TREE:\r\n************\r\n"));
            Console.WriteLine(avlTree.DrawTree());

            //
            // OUTPUT OF AVL TREE DRAWER
            /*****
             ** ************
             ** ** AVL TREE:
             ** ************
             **                    ....15...
             **                   /         \
             **             ...9..      ..20.
             **            /      \    /     \
             **       ..5.      11   16    28
             **      /    \    /  \ / \   /  \
             **    1     7   9   12  19 25  30
             **    /\   / \ / \ / \  /\ /\ / \
             **  -1   7   8  10  13         39
             **  /\   /\ /\  /\  /\         /\
             *
             */

            treeDataList = new List<int>() { 15, 25, 5, 12, 1, 9, 7, -1, 11, 30, 8, 10, 13, 28, 39 };
            avlTree.Clear();
            avlTree.Insert(treeDataList);

            Console.WriteLine(String.Format("************\r\n** AVL TREE:\r\n************\r\n"));
            Console.WriteLine(avlTree.DrawTree());

            //
            // OUTPUT OF AVL TREE DRAWER
            /*****
             **
             **           .......9......
             **          /              \
             **       .5       ....12...
             **      /  \     /         \
             **    1   7    11      .25.
             **    /\ / \   /\     /    \
             **  -1     8  10    15    30
             **  /\    /\  /\    /\   / \
             **                 13   28 39
             **                 /\   /\ /\
             **
             */

            Console.ReadLine();
        }
開發者ID:natanbr,項目名稱:C-Sharp-Algorithms,代碼行數:101,代碼來源:AVLTreeTest.cs


注:本文中的BinarySearchTree.Height方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。