public static void EntryPoint() { BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 11, Left = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 2, Left = new BinaryTreeNode <int>() { Data = 1 } }, Right = new BinaryTreeNode <int>() { Data = 4, Right = new BinaryTreeNode <int>() { Data = 3 } } }, Right = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 7, Right = new BinaryTreeNode <int>() { Data = 6 } }, Right = new BinaryTreeNode <int>() { Data = 9, Left = new BinaryTreeNode <int>() { Data = 8 } } } }; BinaryTreeHelpers <int> .Print(root); var ancestor = AncestorNonRecursive(root, 8, 6, 11); if (ancestor != null) { Console.WriteLine(ancestor.Data); } Console.ReadLine(); }
public static void EntryPoint() { BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 11, Left = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 2, Left = new BinaryTreeNode <int>() { Data = 1 } }, Right = new BinaryTreeNode <int>() { Data = 4, Right = new BinaryTreeNode <int>() { Data = 3 } } }, Right = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 7, Right = new BinaryTreeNode <int>() { Data = 6 } }, Right = new BinaryTreeNode <int>() { Data = 9, Left = new BinaryTreeNode <int>() { Data = 8 } } } }; BinaryTreeHelpers <int> .Print(root); PostOrderNonRecursive(root); Console.ReadLine(); }
public static void EntryPoint() { BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 15, Left = new BinaryTreeNode <int>() { Data = 12, Left = new BinaryTreeNode <int>() { Data = 1231 }, Right = new BinaryTreeNode <int>() { Data = 1252, Right = new BinaryTreeNode <int>() { Data = 1293, } } }, Right = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 123 }, Right = new BinaryTreeNode <int>() { Data = 125, Right = new BinaryTreeNode <int>() { Data = 129, } } } }, Right = new BinaryTreeNode <int>() { Data = 9, Left = new BinaryTreeNode <int>() { Data = 8 }, Right = new BinaryTreeNode <int>() { Data = 13, Left = new BinaryTreeNode <int>() { Data = 89 }, Right = new BinaryTreeNode <int>() { Data = 456, Left = new BinaryTreeNode <int>() { Data = 657 }, Right = new BinaryTreeNode <int>() { Data = 976 } } } } }; BinaryTreeHelpers <int> .Print(root); BinaryTreeHelpers <int> .Print(MirrorNonRecursive(root)); Console.ReadLine(); }
public static void EntryPoint() { BinaryTreeNodeHeight <int> root = new BinaryTreeNodeHeight <int>() { Data = 11, Left = new BinaryTreeNodeHeight <int>() { Data = 5, Left = new BinaryTreeNodeHeight <int>() { Data = 2, Left = new BinaryTreeNodeHeight <int>() { Data = 1, Left = new BinaryTreeNodeHeight <int>() { Data = 12, Right = new BinaryTreeNodeHeight <int>() { Data = 15 } } } }, Right = new BinaryTreeNodeHeight <int>() { Data = 4, Right = new BinaryTreeNodeHeight <int>() { Data = 3, Right = new BinaryTreeNodeHeight <int>() { Data = 121, Left = new BinaryTreeNodeHeight <int>() { Data = 151, Left = new BinaryTreeNodeHeight <int>() { Data = 153 } } } } } }, Right = new BinaryTreeNodeHeight <int>() { Data = 10, Left = new BinaryTreeNodeHeight <int>() { Data = 7 }, Right = new BinaryTreeNodeHeight <int>() { Data = 9 } } }; BinaryTreeHelpers <int> .Print(root); int diameter = 0; int height = DiameterRecursive(root, ref diameter); Console.WriteLine("Height is : {0}, Diameter: {1}", height, diameter + 1); diameter = DiameterNonRecursive(root); Console.WriteLine("Diameter is : {0}", diameter + 1); Console.ReadLine(); }
public static void EntryPoint() { BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 3, Left = new BinaryTreeNode <int>() { Data = 1, Right = new BinaryTreeNode <int>() { Data = 2 } }, Right = new BinaryTreeNode <int>() { Data = 4 } }, Right = new BinaryTreeNode <int>() { Data = 8, Left = new BinaryTreeNode <int>() { Data = 6, Right = new BinaryTreeNode <int>() { Data = 7 } }, Right = new BinaryTreeNode <int>() { Data = 9 } } }, Right = new BinaryTreeNode <int>() { Data = 20, Left = new BinaryTreeNode <int>() { Data = 15, Left = new BinaryTreeNode <int>() { Data = 13 }, Right = new BinaryTreeNode <int>() { Data = 18 } }, Right = new BinaryTreeNode <int>() { Data = 28, Left = new BinaryTreeNode <int>() { Data = 26 }, Right = new BinaryTreeNode <int>() { Data = 29 } } } }; BinaryTreeHelpers <int> .Print(root); BinaryTreeNode <int> pred = null; BinaryTreeNode <int> succ = null; var isTrue = BSTPredesseorNon(root, 28, ref pred, ref succ); if (isTrue) { if (pred != null) { Console.WriteLine("predecessor: {0}", pred.Data); } if (succ != null) { Console.WriteLine("Successor: {0}", succ.Data); } } Console.ReadLine(); }
public static void EntryPoint() { BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 15, Left = new BinaryTreeNode <int>() { Data = 12, Left = new BinaryTreeNode <int>() { Data = 1231 }, Right = new BinaryTreeNode <int>() { Data = 1252, Right = new BinaryTreeNode <int>() { Data = 1293, } } }, Right = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 123 }, Right = new BinaryTreeNode <int>() { Data = 125, Right = new BinaryTreeNode <int>() { Data = 129, } } } }, Right = new BinaryTreeNode <int>() { Data = 9, Left = new BinaryTreeNode <int>() { Data = 8 }, Right = new BinaryTreeNode <int>() { Data = 13, Left = new BinaryTreeNode <int>() { Data = 89 }, Right = new BinaryTreeNode <int>() { Data = 456, Left = new BinaryTreeNode <int>() { Data = 657 }, Right = new BinaryTreeNode <int>() { Data = 976 } } } } }; BinaryTreeHelpers <int> .Print(root); bool result = PrintPathAncestorsNonRecursiveRef(root, 657, 17); Console.WriteLine(result); Console.ReadLine(); }
public static void EntryPoint() { BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 3, Left = new BinaryTreeNode <int>() { Data = 1, Right = new BinaryTreeNode <int>() { Data = 2 } }, Right = new BinaryTreeNode <int>() { Data = 4 } }, Right = new BinaryTreeNode <int>() { Data = 8, Left = new BinaryTreeNode <int>() { Data = 6, Right = new BinaryTreeNode <int>() { Data = 7 } }, Right = new BinaryTreeNode <int>() { Data = 9 } } }, Right = new BinaryTreeNode <int>() { Data = 20, Left = new BinaryTreeNode <int>() { Data = 15, Left = new BinaryTreeNode <int>() { Data = 13 }, Right = new BinaryTreeNode <int>() { Data = 18 } }, Right = new BinaryTreeNode <int>() { Data = 28, Left = new BinaryTreeNode <int>() { Data = 26 }, Right = new BinaryTreeNode <int>() { Data = 29 } } } }; BinaryTreeHelpers <int> .Print(root); Console.WriteLine("Is BST : {0}", IsBSTNonRecursive(root)); Console.ReadLine(); }
public static void EntryPoint() { ////int[] myInts = Array.ConvertAll(Console.ReadLine().Split(' '), s => int.Parse(s)); ////Console.WriteLine(string.Join(", ", myInts)); BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 15, Left = new BinaryTreeNode <int>() { Data = 12, Left = new BinaryTreeNode <int>() { Data = 1231 }, Right = new BinaryTreeNode <int>() { Data = 1252, Right = new BinaryTreeNode <int>() { Data = 1293, } } }, Right = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 123 }, Right = new BinaryTreeNode <int>() { Data = 125, Right = new BinaryTreeNode <int>() { Data = 129, } } } }, Right = new BinaryTreeNode <int>() { Data = 9, Left = new BinaryTreeNode <int>() { Data = 8 }, Right = new BinaryTreeNode <int>() { Data = 13, Left = new BinaryTreeNode <int>() { Data = 89 }, Right = new BinaryTreeNode <int>() { Data = 456, Left = new BinaryTreeNode <int>() { Data = 657 }, Right = new BinaryTreeNode <int>() { Data = 976 } } } } }; BinaryTreeHelpers <int> .Print(root); bool result = PrintSumPathNonRecursiveRef(root, 153, 17); Console.WriteLine(result); Console.ReadLine(); }
public static void EntryPoint() { var depthNode = new BinaryTreeNode <int>() { Data = 976, Right = new BinaryTreeNode <int>() { Data = 776 } }; BinaryTreeNode <int> root = new BinaryTreeNode <int>() { Data = 10, Left = new BinaryTreeNode <int>() { Data = 15, Left = new BinaryTreeNode <int>() { Data = 12, Left = new BinaryTreeNode <int>() { Data = 1231 }, Right = new BinaryTreeNode <int>() { Data = 1252, Right = new BinaryTreeNode <int>() { Data = 1293, } } }, Right = new BinaryTreeNode <int>() { Data = 5, Left = new BinaryTreeNode <int>() { Data = 123 }, Right = new BinaryTreeNode <int>() { Data = 125, Right = new BinaryTreeNode <int>() { Data = 129, } } } }, Right = new BinaryTreeNode <int>() { Data = 9, Left = new BinaryTreeNode <int>() { Data = 8 }, Right = new BinaryTreeNode <int>() { Data = 13, Left = new BinaryTreeNode <int>() { Data = 89 }, Right = new BinaryTreeNode <int>() { Data = 456, Left = new BinaryTreeNode <int>() { Data = 657 }, Right = depthNode } } } }; BinaryTreeHelpers <int> .Print(root); //bool isFound = false; int depth = DepthOfANodeNonRecursive(root, 123); Console.WriteLine("Depth is : {0}", depth); Console.ReadLine(); }