当前位置: 首页>>代码示例>>C#>>正文


C# BinaryTree.FindNode方法代码示例

本文整理汇总了C#中BinaryTree.FindNode方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryTree.FindNode方法的具体用法?C# BinaryTree.FindNode怎么用?C# BinaryTree.FindNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinaryTree的用法示例。


在下文中一共展示了BinaryTree.FindNode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MainTree

        static void MainTree()
        {
            var tree = new BinaryTree<int>();
            tree.Root = new Node<int> { Value = 1};

            var node2 = tree.Root.Left = new Node<int>(2);
            var node3 = tree.Root.Right = new Node<int>(3);

            var node4 = node2.Left = new Node<int>(4);
            var node5 = node2.Right = new Node<int>(5);

            var node6 = node4.Right = new Node<int>(6);
            var node7 = node4.Left = new Node<int>(7);

            var node0 = node3.Right = new Node<int>(0);
            var node8 = node0.Left = new Node<int>(8);

            //        1
            //      /  \
            //     2     3
            //    / \     \
            //   4   5     0
            //  / \       /
            // 7   6     8

            for (int i = 0; i < 12; i++)
                Console.WriteLine("Contains {0}: {1}", i, tree.FindNode(i) != null);

            Console.WriteLine();

            // Depth PreOrder: 1 2 4 7 6 5 3 0 8
            Console.WriteLine("PreOrder: ");
            var preOrder = tree.GetPreOrderEnumerator();
            while (preOrder.MoveNext())
                Console.Write(" {0} ", preOrder.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            // Depth InOrder: 7 4 6 2 5 1 3 8 0
            Console.WriteLine("InOrder: ");
            var inOrder = tree.GetInOrderEnumerator();
            while (inOrder.MoveNext())
                Console.Write(" {0} ", inOrder.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            // Depth LevelOrder: 1 2 3 4 5 0 7 6 8
            Console.WriteLine("LevelOrder: ");
            var levelOrder = tree.GetLevelOrderEnumerator();
            while (levelOrder.MoveNext())
                Console.Write(" {0} ", levelOrder.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("String representation: ");
            var str = BinaryTreeStringSerializer.AsString(tree);
            Console.WriteLine(str);

            var loadedTree = BinaryTreeStringSerializer.FromString(str);
            Console.WriteLine(BinaryTreeStringSerializer.AsString(loadedTree));

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("String representation Compact: ");
            var strCompact = BinaryTreeStringSerializer.AsStringCompact(tree);
            Console.WriteLine(strCompact);

            var loadedTreeCompact = BinaryTreeStringSerializer.FromStringCompact(strCompact);
            Console.WriteLine(BinaryTreeStringSerializer.AsStringCompact(loadedTreeCompact));
            Console.WriteLine(BinaryTreeStringSerializer.AsString(loadedTreeCompact));

            var pre = new[] {1, 2, 4, 7, 6, 5, 3, 0, 8};
            var ino = new[] {7, 4, 6, 2, 5, 1, 3, 8, 0};

            var reconstructed = BinaryTreeStringSerializer.Reconstruct(pre, ino);
            Console.WriteLine(BinaryTreeStringSerializer.AsString(reconstructed));

            Console.ReadLine();
        }
开发者ID:saguiitay,项目名称:BackToBasics,代码行数:83,代码来源:Program.cs

示例2: Simple

        public void Simple()
        {
            var binaryTree = new BinaryTree<int>(5);

            var child1 = new BinaryTree<int>(2);
            var child2 = new BinaryTree<int>(3);

            binaryTree.Add(child1);
            binaryTree.Add(child2);

            var child4 = new BinaryTree<int>(9);
            var child5 = new BinaryTree<int>(12);
            var child6 = new BinaryTree<int>(13);

            child1.Add(child4);
            child1.Add(child5);
            child2.Add(child6);

            Assert.AreEqual(binaryTree.FindNode(target => target == 2), child1);

            Assert.AreEqual(binaryTree.FindNode(target => target == 9), child4);

            Assert.AreEqual(binaryTree.FindNode(target => target == 13), child6);

            Assert.AreEqual(binaryTree.FindNode(target => target == 57), null);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:26,代码来源:FindNode.cs


注:本文中的BinaryTree.FindNode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。