Exemple #1
0
 /// <summary>
 /// This is an O(n+m) algorithm
 /// Step1: We will do an inorder traversal of mainTree and subTree and check if
 /// inorder traversal of subTree is a substring of inorder traversal of mainTree
 /// Step2: We do a preorder/postorder traversal of both the trees and make sure
 /// subtree's traversal result in contained in mainTree's traversal result
 /// </summary>
 /// <param name="mainTree"></param>
 /// <param name="subTree"></param>
 /// <returns></returns>
 private static bool MatchTreeAlgo2(BinaryTreeNode <int> mainTree, BinaryTreeNode <int> subTree)
 {
     // Step1: We will do an inorder traversal of mainTree and subTree and check if
     // inorder traversal of subTree is a substring of inorder traversal of mainTree
     if (WalkTheTree.InOrderTraversalReturnAsString(mainTree).Contains(WalkTheTree.InOrderTraversalReturnAsString(subTree)))
     {
         // Step2: We do a preorder/postorder traversal of both the trees and make sure
         // subtree's traversal result in contained in mainTree's traversal result
         return(WalkTheTree.PreOrderTraversalReturnAsString(mainTree).Contains(WalkTheTree.PreOrderTraversalReturnAsString(subTree)));
     }
     return(false);
 }
Exemple #2
0
        public static void TestKthSmallestElementFromBST()
        {
            BinarySearchTree <int> bst = new BinarySearchTree <int>();

            bst.Insert(6);
            bst.Insert(3);
            bst.Insert(2);
            bst.Insert(1);
            bst.Insert(0);
            bst.Insert(8);
            bst.Insert(7);
            bst.Insert(9);
            WalkTheTree.InOrderTraversal(bst.Head);
            Console.WriteLine();

            KthSmallestElementFromBST kObj = new KthSmallestElementFromBST();

            kObj.GetKthSmallestElement(bst.Head, 5, 0);
            Console.WriteLine("The {0}th element in the bst is {1}", 5, kObj.KthSmallestElement.Data);
            BinarySearchTreeNode <int> kthSmallestElement = kObj.GetKthSmallestElementIterative(bst.Head, 5);

            Console.WriteLine("Algo2: The {0}th element in the bst is {1}", 5, kthSmallestElement.Data);

            kObj = new KthSmallestElementFromBST();
            kObj.GetKthSmallestElement(bst.Head, 10, 0);
            Console.WriteLine("The {0}th element in the bst is {1}", 10, (kObj.KthSmallestElement == null) ? -1 : kObj.KthSmallestElement.Data);
            kthSmallestElement = kObj.GetKthSmallestElementIterative(bst.Head, 10);
            Console.WriteLine("Algo2: The {0}th element in the bst is {1}", 10, (kthSmallestElement == null) ? -1 : kthSmallestElement.Data);

            kObj = new KthSmallestElementFromBST();
            kObj.GetKthSmallestElement(bst.Head, 0, 0);
            Console.WriteLine("The {0}th element in the bst is {1}", 0, (kObj.KthSmallestElement == null) ? -1 : kObj.KthSmallestElement.Data);
            kthSmallestElement = kObj.GetKthSmallestElementIterative(bst.Head, 0);
            Console.WriteLine("Algo2: The {0}th element in the bst is {1}", 0, (kthSmallestElement == null) ? -1 : kthSmallestElement.Data);

            kObj = new KthSmallestElementFromBST();
            kObj.GetKthSmallestElement(bst.Head, 7, 0);
            Console.WriteLine("The {0}th element in the bst is {1}", 7, (kObj.KthSmallestElement == null) ? -1 : kObj.KthSmallestElement.Data);
            kthSmallestElement = kObj.GetKthSmallestElementIterative(bst.Head, 7);
            Console.WriteLine("Algo2: The {0}th element in the bst is {1}", 7, (kthSmallestElement == null) ? -1 : kthSmallestElement.Data);

            kObj = new KthSmallestElementFromBST();
            kObj.GetKthSmallestElement(bst.Head, 1, 0);
            Console.WriteLine("The {0}th element in the bst is {1}", 1, (kObj.KthSmallestElement == null) ? -1 : kObj.KthSmallestElement.Data);
            kthSmallestElement = kObj.GetKthSmallestElementIterative(bst.Head, 1);
            Console.WriteLine("Algo2: The {0}th element in the bst is {1}", 1, (kthSmallestElement == null) ? -1 : kthSmallestElement.Data);
        }
        public static void TestConvertSkewedBSTToCompleteBST()
        {
            Console.WriteLine("Test conversion from skewed BST to the complete BST");
            BinarySearchTree <int> bst = new BinarySearchTree <int>();

            bst.Insert(6);
            bst.Insert(3);
            bst.Insert(2);
            bst.Insert(1);
            bst.Insert(0);
            bst.Insert(8);
            bst.Insert(7);
            bst.Insert(9);
            BinarySearchTree <int> completeBst = ConvertSkewedBSTToCompleteBST(bst);

            WalkTheTree.InOrderTraversal(completeBst.Head);
            Console.WriteLine();
        }
        public static void TestAncestorOfTwoNodesInBST()
        {
            BinarySearchTree <int> bst = new BinarySearchTree <int>();

            bst.Insert(6);
            bst.Insert(3);
            bst.Insert(2);
            bst.Insert(1);
            bst.Insert(0);
            bst.Insert(8);
            bst.Insert(7);
            bst.Insert(9);
            WalkTheTree.InOrderTraversal(bst.Head);
            Console.WriteLine();

            Console.WriteLine("Ancestor of {0} and {1} from AncestorOfTwoNodesIterative is : {2}", 1, 7, AncestorOfTwoNodesIterative(1, 7, bst).Data);
            Console.WriteLine("Ancestor of {0} and {1} from AncestorOfTwoNodesRecursive is : {2}", 1, 7, AncestorOfTwoNodesRecursive(bst.SearchBSTIterative(1), bst.SearchBSTIterative(7), bst.Head).Data);
            Console.WriteLine("Ancestor of {0} and {1} from AncestorOfTwoNodesIterative is : {2}", 1, 3, AncestorOfTwoNodesIterative(1, 3, bst).Data);
            Console.WriteLine("Ancestor of {0} and {1} from AncestorOfTwoNodesRecursive is : {2}", 1, 3, AncestorOfTwoNodesRecursive(bst.SearchBSTIterative(1), bst.SearchBSTIterative(3), bst.Head).Data);
        }