コード例 #1
0
 public static bool IsSubTree(BinaryTreeNode<int> smallerTreeRoot, BinaryTreeNode<int> largerTreeRoot)
 {
     if (smallerTreeRoot == null) return true;
     else if (largerTreeRoot == null) return false;
     if (AreIdentical(smallerTreeRoot, largerTreeRoot)) return true;
     return IsSubTree(smallerTreeRoot, largerTreeRoot.Left) || IsSubTree(smallerTreeRoot, largerTreeRoot.Right);
 }
コード例 #2
0
ファイル: ComputeLCA.cs プロジェクト: mmoroney/Algorithms
        private static BinaryTreeNode<int> BruteForce(BinaryTreeNode<int> root, int a, int b)
        {
            BinaryTreeNode<int> n1 = BinarySearchTree.Find(root, a);
            BinaryTreeNode<int> n2 = BinarySearchTree.Find(root, b);

            int d1 = ComputeLCA.Depth(n1);
            int d2 = ComputeLCA.Depth(n2);

            while (d1 > d2)
            {
                n1 = n1.Parent;
                d1--;
            }

            while (d2 > d1)
            {
                n2 = n2.Parent;
                d2--;
            }

            while (!object.Equals(n1, n2))
            {
                n1 = n1.Parent;
                n2 = n2.Parent;
            }

            return n1;
        }
コード例 #3
0
 public static BinaryTreeNode<int> FindLowestCommonAncestorInBST(BinaryTreeNode<int> root, BinaryTreeNode<int> node1, BinaryTreeNode<int> node2)
 {
     if (root == null || node1 == null || node2 == null) return null;
     if (root.Value > Max(node1.Value, node2.Value)) return FindLowestCommonAncestorInBST(root.Left, node1, node2);
     if (root.Value < Min(node1.Value, node2.Value)) return FindLowestCommonAncestorInBST(root.Right, node1, node2);
     return root;
 }
コード例 #4
0
        public void testNotInTree()
        {
            BinaryTreeNode<Int32> root = new BinaryTreeNode<Int32>(2);
            root.insert(5);
            root.insert(1);
            root.insert(16);
            bool exceptionCaught_v1 = false;
            bool exceptionCaught_v2 = false;
            bool exceptionCaught_v3 = false;

            #pragma warning disable 168
            try {
                Assert.AreEqual(eu.sig.training.ch03.binarytree.v1.BinaryTreeSearch.calculateDepth(root, 17), 0);
            } catch (TreeException e) {
                exceptionCaught_v1 = true;
            }
            try {
                Assert.AreEqual(eu.sig.training.ch03.binarytree.v2.BinaryTreeSearch
                    .calculateDepth(root, 17), 0);
            } catch (TreeException e) {
                exceptionCaught_v2 = true;
            }
            try {
                Assert.AreEqual(eu.sig.training.ch03.binarytree.v3.BinaryTreeSearch
                    .calculateDepth(root, 17), 0);
            } catch (TreeException e) {
                exceptionCaught_v3 = true;
            }
            Assert.IsTrue(exceptionCaught_v1);
            Assert.IsTrue(exceptionCaught_v2);
            Assert.IsTrue(exceptionCaught_v3);
            #pragma warning restore 168
        }
コード例 #5
0
        public void ShouldCorrectlyCreateRootNode()
        {
            var root = new BinaryTreeNode<int>(null);

            Assert.AreEqual(null, root.Parent);
            Assert.IsTrue(root.IsRoot);
        }
コード例 #6
0
        private BinaryTreeNode<Script> UnknowContact()
        {
            var root = new BinaryTreeNode<Script>(new Script
            {
                Question =
                    "Could you please tell me the name of the person who is responsible for the cleaning of your premises?",
                Actions = new Collection<ScriptAction>
                {
                    new UpdateContactName()
                },
            });

            root.Right = KnownContact();
            root.Left = new BinaryTreeNode<Script>(new Script
            {
                Question =
                    "We would like to send the relevant person some information on our services that they could find useful in the future. I do not " +
                    "need to speak to the person. Could you just tell me their name",
                Actions = new Collection<ScriptAction>
                {
                    new UpdateContactName()
                }
            });

            root.Left.Left = End(false);
            root.Left.Right = End(true);

            return root;
        }
コード例 #7
0
ファイル: CC4_8.cs プロジェクト: Sanqiang/Algorithm-Win
        public static void findSumExDepth(BinaryTreeNode root, double threshold, int level = 0,
            System.Collections.Generic.List<BinaryTreeNode> list = null)
        {
            if (list == null)
            {
                list = new System.Collections.Generic.List<BinaryTreeNode>();
                list.Add(root);
                if (root.Data == threshold)
                {
                    printPath(list);
                }
            }
            else
            {
                list.Add(root);
                double tmp = threshold;
                for (int i = level; i >= 0; i--)
                {
                    tmp -= list[i].Data;
                    if (tmp == 0)
                    {
                        printPath(list, i);
                    }
                }
            }

            if (root.LeftNode != null)
            {
                findSumExDepth(root.LeftNode, threshold, level + 1, clone(list));
            }
            if (root.RightNode != null)
            {
                findSumExDepth(root.RightNode, threshold, level + 1, clone(list));
            }
        }
コード例 #8
0
            public void Must_construct_root_node()
            {
                var systemUnderTest = new BinaryTreeNode();

                Assert.That(systemUnderTest.Ancestor, Is.Null);
                Assert.That(systemUnderTest.IsRoot, Is.True);
            }
コード例 #9
0
        /// <summary>
        /// Initializes the BinaryTree with an array of integers. NOTE: All duplicated integers will be removed during the initialization and the tree will contain Nodes with unique values.
        /// </summary>
        /// <param name="integers">An array of integers.</param>
        public BinaryTree(int[] integers)
        {
            this.nodeValues = integers.Distinct().ToArray();

            this.rootNode = this.buildNodeConnections(this.nodeValues);
            this.distinguishedNode = this.rootNode;
        }
コード例 #10
0
            public void Must_have_no_descendants()
            {
                var systemUnderTest = new BinaryTreeNode();

                Assert.That(systemUnderTest.LeftDescendant, Is.Null);
                Assert.That(systemUnderTest.RightDescendant, Is.Null);
            }
コード例 #11
0
 public static BinaryTreeNode<int> FindNode(BinaryTreeNode<int> node, int searchValue)
 {
     if (node != null)
     {
         if (node.Value == searchValue)
         {
             CalcStack.Push(node.InitialValue.ToString());
             return node;
         }
         else
         {
             var right = FindNode(node.Right, searchValue);
             if (right != null)
             {
                 CalcStack.Push("*");
                 CalcStack.Push(node.InitialValue.ToString());
                 return right;
             }
             else
             {
                 var left = FindNode(node.Left, searchValue);
                 if (left != null)
                 {
                     CalcStack.Push("+");
                     CalcStack.Push(node.InitialValue.ToString());
                     return left;
                 }
             }
         }
     }
     return null;
 }
コード例 #12
0
ファイル: CC4_5.cs プロジェクト: Sanqiang/Algorithm-Win
 public static BinaryTreeNode inorderSucc(BinaryTreeNode orignal)
 {
     if (orignal.ParentNode == null && orignal.RightNode != null)
     {
         BinaryTreeNode btn = orignal.RightNode;
         while (btn != null)
         {
             if (btn.LeftNode != null)
             {
                 return btn.LeftNode;
             }
             btn = btn.RightNode;
         }
     }
     else
     {
         BinaryTreeNode btn = orignal;
         while (btn.ParentNode != null)
         {
             if (btn.ParentNode.LeftNode == btn)
             {
                 return btn.ParentNode;
             }
             btn = btn.ParentNode;
         }
         return btn;
     }
     return null;
 }
コード例 #13
0
 // tag::calculateDepth[]
 public static int CalculateDepth(BinaryTreeNode<int> t, int n)
 {
     int depth = 0;
     if (t.Value == n)
     {
         return depth;
     }
     else
     {
         if (n < t.Value)
         {
             BinaryTreeNode<int> left = t.Left;
             if (left == null)
             {
                 throw new TreeException("Value not found in tree!");
             }
             else
             {
                 return 1 + CalculateDepth(left, n);
             }
         }
         else
         {
             BinaryTreeNode<int> right = t.Right;
             if (right == null)
             {
                 throw new TreeException("Value not found in tree!");
             }
             else
             {
                 return 1 + CalculateDepth(right, n);
             }
         }
     }
 }
コード例 #14
0
        ////Algo
        //      5
        //   3     7
        // 1   4  6  8

        int VisitNode(BinaryTreeNode<int> treeNode)
        {
            int leftVal = 0, rightVal = 0;
            if(treeNode.Left == null)
            {
                leftVal = -1;
            }
            else
            {
                leftVal = VisitNode(treeNode.Left);
            }
            if(treeNode.Right == null)
            {
                rightVal = -1;
            }
            else
            {
                rightVal = VisitNode(treeNode.Right);
            }

            if(Math.Abs(leftVal - rightVal) > 1)
            {
                Console.WriteLine("Tree is not balanced");
            }

            return (leftVal > rightVal ? leftVal : rightVal) + 1;
        }
コード例 #15
0
        public void ShouldNotHaveChildNodesInitialy()
        {
            var root = new BinaryTreeNode<int>(null);

            Assert.IsFalse(root.HasLeftChild);
            Assert.IsFalse(root.HasRightChild);
        }
コード例 #16
0
ファイル: SatisfiesBST.cs プロジェクト: mmoroney/Algorithms
        public void SatisfiesBSTTest()
        {
            Func<BinaryTreeNode<int>, bool>[] functions = new Func<BinaryTreeNode<int>, bool>[]
            {
                SatisfiesBST.BruteForce,
                SatisfiesBST.Recursive,
                SatisfiesBST.InOrderTraversal,
                SatisfiesBST.Queue
            };

            for (int i = 0; i < 10; i++)
            {
                int[] data = ArrayUtilities.CreateRandomArray(10, 0, 20);

                BinaryTreeNode<int> root = new BinaryTreeNode<int>(data[0]);

                for(int j = 1; j < data.Length; j++)
                {
                    BinarySearchTree.Insert(root, data[j]);
                    Tests.TestFunctions(root, functions);
                }

                root = new BinaryTreeNode<int>(data[0]);

                for (int j = 1; j < data.Length; j++)
                {
                    BinaryTreeUtilities.AddRandomNode(root, data[j]);
                    Tests.TestFunctions(root, functions);
                }
            }
        }
コード例 #17
0
 public void NewNodeHasParent()
 {
     BinaryTreeNode<int> node = new BinaryTreeNode<int>(1);
     BinaryTreeNode<int> node2 = new BinaryTreeNode<int>(2);
     node.Left = node2;
     Assert.AreEqual(node2.Parent, node);
 }
コード例 #18
0
 public void CanGetParent()
 {
     BinaryTreeNode<int> left = new BinaryTreeNode<int>(2);
     BinaryTreeNode<int> right = new BinaryTreeNode<int>(3);
     BinaryTreeNode<int> node = new BinaryTreeNode<int>(1, left, right);
     Assert.AreEqual(node.Left.Parent, node);
 }
コード例 #19
0
ファイル: CC4_4.cs プロジェクト: Sanqiang/Algorithm-Win
        public static System.Collections.Generic.Dictionary<System.Int16, System.Collections.Generic.List<BinaryTreeNode>> getLevelLinkedList(BinaryTreeNode head)
        {
            System.Collections.Generic.Dictionary<System.Int16, System.Collections.Generic.List<BinaryTreeNode>> result
                = new System.Collections.Generic.Dictionary<System.Int16, System.Collections.Generic.List<BinaryTreeNode>>();
            short level = 0;
            System.Collections.Generic.List<BinaryTreeNode> list = new System.Collections.Generic.List<BinaryTreeNode>();
            list.Add(head);
            result.Add(level, list);

            while (true)
            {
                System.Collections.Generic.List<BinaryTreeNode> list_loop = result[level];
                list = new System.Collections.Generic.List<BinaryTreeNode>();
                result.Add(++level, list);
                foreach (BinaryTreeNode btn in list_loop)
                {
                    if (btn.LeftNode != null)
                    {
                        list.Add(btn.LeftNode);
                    }
                    if (btn.RightNode != null)
                    {
                        list.Add(btn.RightNode);
                    }
                }
                if (list.Count == 0)
                {
                    break;
                }
            }
            return result;
        }
コード例 #20
0
ファイル: CC4_8.cs プロジェクト: Sanqiang/Algorithm-Win
 public static void findSum(BinaryTreeNode root, double threshold, double count = 0,
     System.Collections.Generic.List<BinaryTreeNode> list = null)
 {
     if (list == null)
     {
         list = new System.Collections.Generic.List<BinaryTreeNode>();
     }
     double Data = root.Data;
     count += Data;
     list.Add(root);
     if (count == threshold)
     {
         printPath(list);
     }
     if (root.LeftNode != null)
     {
         findSum(root.LeftNode, threshold, count, clone(list));
         findSum(root.LeftNode, threshold);
     }
     if (root.RightNode != null)
     {
         findSum(root.RightNode, threshold, count, clone(list));
         findSum(root.RightNode, threshold);
     }
 }
コード例 #21
0
 public void CanGetChildren()
 {
     BinaryTreeNode<int> left = new BinaryTreeNode<int>(2);
     BinaryTreeNode<int> right = new BinaryTreeNode<int>(3);
     BinaryTreeNode<int> node = new BinaryTreeNode<int>(1, left, right);
     BinaryTreeNode<int>[] expected = new BinaryTreeNode<int>[] { left, right };
     CollectionAssert.AreEqual(node.Children, expected);
 }
コード例 #22
0
ファイル: P10_10.cs プロジェクト: slao-learn/epi
 private static string Print(BinaryTreeNode t)
 {
     if (t == null)
         return "null";
     string result = t.data.ToString();
     result += "\n|\\\n" + Print(t.left) + " " + Print(t.right) + '\n';
     return result;
 }
コード例 #23
0
 public void NewNodeWithChildrenHasChildren()
 {
     BinaryTreeNode<int> left = new BinaryTreeNode<int>(2);
     BinaryTreeNode<int> right = new BinaryTreeNode<int>(3);
     BinaryTreeNode<int> node = new BinaryTreeNode<int>(1, left, right);
     Assert.AreEqual(node.Left.Value, 2);
     Assert.AreEqual(node.Right.Value, 3);
 }
コード例 #24
0
 // tag::calculateDepth[]
 public static int calculateDepth(BinaryTreeNode<int> t, int n)
 {
     int depth = 0;
     if (t.getValue() == n)
         return depth;
     else
         return traverseByValue(t, n);
 }
コード例 #25
0
        public void BinaryTreeNodeConstructorTest()
        {
            BinaryTreeNode<int> node = new BinaryTreeNode<int>(10);

            Assert.AreEqual(10, node.Value);
            Assert.IsNull(node.Left);
            Assert.IsNull(node.Right);
        }
コード例 #26
0
        public void ShouldCreateBinarySearchTreeWithGivenRoot()
        {
            var rootNode = new BinaryTreeNode<int>(null);

            var binarySearchTree = new BinarySearchTree<int>(rootNode);

            Assert.AreEqual(rootNode, binarySearchTree.Root);
        }
コード例 #27
0
 private static int TraverseByValue(BinaryTreeNode<int> t, int n) {
     BinaryTreeNode<int> childNode = GetChildNode(t, n);
     if (childNode == null) {
         throw new TreeException("Value not found in tree!");
     } else {
         return 1 + CalculateDepth(childNode, n);
     }
 }
コード例 #28
0
 // tag::calculateDepth[]
 public static int CalculateDepth(BinaryTreeNode<int> t, int n) {
     int depth = 0;
     if (t.Value == n) {
         return depth;
     } else {
         return TraverseByValue(t, n);
     }
 }
コード例 #29
0
        public void ShouldCreateBinarySearchTreeWithGivenRootAndUpdateTheCounter()
        {
            var rootNode = new BinaryTreeNode<int>(null);

            var binarySearchTree = new BinarySearchTree<int>(rootNode);

            Assert.AreEqual(1, binarySearchTree.Count);
        }
コード例 #30
0
 private static BinaryTreeNode<int> GetChildNode(
     BinaryTreeNode<int> t, int n) {
     if (n < t.Value) {
         return t.Left;
     } else {
         return t.Right;
     }
 }
コード例 #31
0
ファイル: P10_10.cs プロジェクト: slao-learn/epi
 public BinaryTreeNode(char data, BinaryTreeNode left) : this(data, left, null)
 {
 }
コード例 #32
0
 public int CompareTo(BinaryTreeNode <T> other)
 {
     return(this.value.CompareTo(other.value));
 }
コード例 #33
0
 public BinaryTreeNode(int value)
 {
     this.value = value;
     this.left  = null;
     this.right = null;
 }
コード例 #34
0
 public BinaryTreeNode insertRight(int rightValue)
 {
     this.right = new BinaryTreeNode(rightValue);
     return(this.right);
 }
コード例 #35
0
 public BinaryTreeNode(int n)
 {
     this.n = n;
     left   = null;
     right  = null;
 }
コード例 #36
0
ファイル: CC4_4.cs プロジェクト: Sanqiang/Algorithm-Win
        public static System.Collections.Generic.Dictionary <System.Int16, System.Collections.Generic.List <BinaryTreeNode> > getLevelLinkedList(BinaryTreeNode head)
        {
            System.Collections.Generic.Dictionary <System.Int16, System.Collections.Generic.List <BinaryTreeNode> > result
                = new System.Collections.Generic.Dictionary <System.Int16, System.Collections.Generic.List <BinaryTreeNode> >();
            short level = 0;

            System.Collections.Generic.List <BinaryTreeNode> list = new System.Collections.Generic.List <BinaryTreeNode>();
            list.Add(head);
            result.Add(level, list);

            while (true)
            {
                System.Collections.Generic.List <BinaryTreeNode> list_loop = result[level];
                list = new System.Collections.Generic.List <BinaryTreeNode>();
                result.Add(++level, list);
                foreach (BinaryTreeNode btn in list_loop)
                {
                    if (btn.LeftNode != null)
                    {
                        list.Add(btn.LeftNode);
                    }
                    if (btn.RightNode != null)
                    {
                        list.Add(btn.RightNode);
                    }
                }
                if (list.Count == 0)
                {
                    break;
                }
            }
            return(result);
        }
コード例 #37
0
        public static string Print(BinaryTreeNode <int> root)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root), "The root of binary tree can't be null");
            }

            // 设立两个堆栈,其中一个在消耗元素的同时,往另一个中添加元素
            // 一个堆栈消耗完毕,开始消耗另一个堆栈,同时如果有新元素需要添加,就添加到之前已经消耗完毕的堆栈中,直到另一个堆栈消耗完毕,再调转回来
            Stack <BinaryTreeNode <int> > stack1 = new Stack <BinaryTreeNode <int> >();
            Stack <BinaryTreeNode <int> > stack2 = new Stack <BinaryTreeNode <int> >();

            StringBuilder sb          = new StringBuilder();
            bool          leftToRight = true;

            stack1.Push(root);

            // 当两个堆栈全部清空的时候,循环才能结束
            while (stack1.Count != 0 || stack2.Count != 0)
            {
                if (leftToRight)
                {
                    var currentNode = stack1.Pop();
                    sb.Append(currentNode.Value).Append(",");

                    // 当前层从左到右遍历时,要考虑到下一层是从右往左遍历的,所以要先将左子节点压入堆栈,再压入右子节点
                    if (currentNode.Left != null)
                    {
                        stack2.Push(currentNode.Left);
                    }
                    if (currentNode.Right != null)
                    {
                        stack2.Push(currentNode.Right);
                    }

                    // 当前堆栈清空以后,说明当前层级遍历结束,修改标志,改变遍历方向
                    // 如果是实际输出到显示屏上,可以在此加入换行符号
                    if (stack1.Count == 0)
                    {
                        leftToRight = false;
                    }
                }
                else
                {
                    var currentNode = stack2.Pop();
                    sb.Append(currentNode.Value).Append(",");

                    if (currentNode.Right != null)
                    {
                        stack1.Push(currentNode.Right);
                    }
                    if (currentNode.Left != null)
                    {
                        stack1.Push(currentNode.Left);
                    }

                    if (stack2.Count == 0)
                    {
                        leftToRight = true;
                    }
                }
            }

            return(sb.ToString(0, sb.Length - 1));// 去掉最后一个逗号
        }
コード例 #38
0
 private static bool BruteForce(BinaryTreeNode <int> root)
 {
     return(IsHeightBalanced.BalancedHeightBruteForce(root));
 }
コード例 #39
0
ファイル: Test_4_5.CS プロジェクト: jkrez/Interview
        private static void Validate(bool expected, BinaryTreeNode <int> root)
        {
            var result = Question_4_5.IsBST(root);

            Assert.AreEqual(expected, result);
        }
コード例 #40
0
 public void MaxDiffBetweenNodeAndAncestor(BinaryTreeNode root)
 {
 }
コード例 #41
0
 public BinaryTreeNode(T data, BinaryTreeNode <T> leftNode = null, BinaryTreeNode <T> rightNode = null)
 {
     Data      = data;
     LeftNode  = leftNode;
     RightNode = rightNode;
 }
コード例 #42
0
 private static bool Recursive(BinaryTreeNode <int> root)
 {
     return(IsHeightBalanced.BalancedHeightRecursive(root) != null);
 }
コード例 #43
0
 /// <summary>
 /// 二叉树节点
 /// </summary>
 /// <param name="value">节点中的值</param>
 /// <param name="parent">节点的父节点</param>
 public BinaryTreeNode(T value, BinaryTreeNode <T> parent)
 {
     this.Value  = value;
     this.Parent = parent;
 }
コード例 #44
0
 private void RemoveNode(BinaryTreeNode <T> toRemove, bool isLeftChild)
 {
     throw new NotImplementedException();
 }
コード例 #45
0
 private bool RecursiveRemoveFromTree(BinaryTreeNode <T> node, T value)
 {
     throw new NotImplementedException();
 }
コード例 #46
0
 public BinaryTree(T root)
 {
     Root = new BinaryTreeNode <T>(root);
 }
コード例 #47
0
ファイル: BinaryTree.cs プロジェクト: NickRoyer/FileParser
        public virtual void Add(K key, V value)
        {
            BinaryTreeNode newNode = new BinaryTreeNode(key, value);

            InsertNode(newNode);
        }
コード例 #48
0
    public bool isBalanced(BinaryTreeNode treeRoot)
    {
        bool balanced = true;

        //    Start a stack
        Stack <NodeDepthPair> treeStack = new Stack <NodeDepthPair>();

        NodeDepthPair rootNodePair = new NodeDepthPair(treeRoot, 0);

        //    Push in the root
        treeStack.Push(rootNodePair);

        //    Declare depth holder
        List <int> numDepths = new List <int> ();

        while (treeStack.Count > 0)
        {
            NodeDepthPair curNodePair = treeStack.Pop();

            Console.WriteLine("Looking at node {0}", curNodePair.node.value);


            //    Check if this is a leaf node
            if (curNodePair.node.left == null && curNodePair.node.right == null)
            {
                Console.WriteLine("\tIs leaf Node at depth {0}", curNodePair.depth);

                //    Depth is currently not represented
                if (!numDepths.Contains(curNodePair.depth))
                {
                    numDepths.Add(curNodePair.depth);

                    //    Conidtions for having depths that have diff greater than 1
                    //    Number of depths is greater than 2, then we probably
                    if (numDepths.Count > 2)
                    {
                        return(false);
                    }
                    //    There are 2 depths and their diff is greater than 1
                    if (numDepths.Count == 2)
                    {
                        if (Math.Abs(numDepths[0] - numDepths[1]) > 1)
                        {
                            return(false);
                        }
                    }
                }
            }
            //    This node has children
            else
            {
                //    If there is a left node
                if (curNodePair.node.left != null)
                {
                    //    put that on the stack
                    treeStack.Push(new NodeDepthPair(curNodePair.node.left, curNodePair.depth + 1));
                }
                //    If there is a left node
                if (curNodePair.node.right != null)
                {
                    //    put that on the stack
                    treeStack.Push(new NodeDepthPair(curNodePair.node.right, curNodePair.depth + 1));
                }
            }
        }
        return(balanced);
    }
コード例 #49
0
ファイル: Program.cs プロジェクト: vikas3045/oldprep
 private static void ProcessNode(BinaryTreeNode root)
 {
     Console.Write(root.Data + " ");
 }
コード例 #50
0
 public BinarySearchTree()
 {
     this.root = null;
 }
コード例 #51
0
ファイル: Program.cs プロジェクト: vikas3045/oldprep
        static void Main(string[] args)
        {
            BinaryTreeNode root = new BinaryTreeNode(1);

            root.Left            = new BinaryTreeNode(2);
            root.Right           = new BinaryTreeNode(3);
            root.Left.Left       = new BinaryTreeNode(4);
            root.Left.Right      = new BinaryTreeNode(10);
            root.Left.Left.Left  = new BinaryTreeNode(7);
            root.Left.Left.Right = new BinaryTreeNode(5);

            root.Right.Left        = new BinaryTreeNode(7);
            root.Right.Right       = new BinaryTreeNode(10);
            root.Right.Left.Left   = new BinaryTreeNode(8);
            root.Right.Left.Right  = new BinaryTreeNode(9);
            root.Right.Right.Right = new BinaryTreeNode(11);

            //BinaryTreeNode root = new BinaryTreeNode(50);
            //var node2 = new BinaryTreeNode(2);
            //var node3 = new BinaryTreeNode(3);
            //var node4 = new BinaryTreeNode(4);
            //var node5 = new BinaryTreeNode(5);
            //var node6 = new BinaryTreeNode(6);
            //var node7 = new BinaryTreeNode(7);

            //root.Left = node2;
            //root.Right = node3;

            //node2.Left = node4;
            //node2.Right = node5;

            //node3.Left = node6;
            //node3.Right = node7;

            //var watch = System.Diagnostics.Stopwatch.StartNew();
            //Console.WriteLine(GetMaximumRecursive(root));
            //watch.Stop();
            //Console.Write(" Time: " + watch.ElapsedMilliseconds + " ms");

            //watch.Restart();
            //Console.WriteLine(GetMaximumIterative(root));
            //watch.Stop();
            //Console.Write(" Time: " + watch.ElapsedMilliseconds + " ms");

            var watch = System.Diagnostics.Stopwatch.StartNew();

            Console.Write("Recursive: ");
            InOrderRecursive(root);

            watch.Stop();
            Console.Write(" Time: " + watch.ElapsedMilliseconds + " ms");

            Console.WriteLine();

            watch.Restart();

            Console.Write("Iterative: ");
            InOrderIterative(root);

            watch.Stop();
            Console.Write(" Time: " + watch.ElapsedMilliseconds + " ms");

            Console.ReadLine();
        }
コード例 #52
0
ファイル: BinaryTreeNode.cs プロジェクト: crdrury/visual-bst
 public void SetChildNode(ChildNode selectedNode, BinaryTreeNode node)
 {
     childNode[(int)selectedNode] = node;
 }
コード例 #53
0
 public NodeDepthPair(BinaryTreeNode node, int depth)
 {
     this.node  = node;
     this.depth = depth;
 }
コード例 #54
0
ファイル: BinaryTreeNode.cs プロジェクト: crdrury/visual-bst
 public int CompareTo(BinaryTreeNode otherNode)
 {
     return(value.CompareTo(otherNode.value));
 }
コード例 #55
0
 public BinaryTreeNode insertLeft(int leftValue)
 {
     this.left = new BinaryTreeNode(leftValue);
     return(this.left);
 }
コード例 #56
0
ファイル: P10_10.cs プロジェクト: slao-learn/epi
 public BinaryTreeNode(char data, BinaryTreeNode left, BinaryTreeNode right)
 {
     this.data  = data;
     this.left  = left;
     this.right = right;
 }
コード例 #57
0
 public void Insert(int n)
 {
     Root = Insert(Root, n);
 }
コード例 #58
0
 public BinaryTreeNode(T value)
     : this(value, null, null)
 {
     parent = null;
 }
コード例 #59
0
 public BinaryTree()
 {
     Root = null;
 }
コード例 #60
0
        //LCA of binary tree
        private static BinaryTreeNode LCA_BT(BinaryTreeNode Root, BinaryTreeNode Node1, BinaryTreeNode Node2)
        {
            if (Root == null || Node1 == null || Node2 == null)
            {
                return(null);
            }
            if (Root == Node1 || Root == Node2)
            {
                return(Root);
            }
            BinaryTreeNode leftLca  = LCA_BT(Root.left, Node1, Node2);
            BinaryTreeNode rightLca = LCA_BT(Root.right, Node1, Node2);

            if (leftLca != null && rightLca != null)
            {
                return(Root);
            }
            if (leftLca != null)
            {
                return(leftLca);
            }
            return(rightLca);
        }