Exemplo n.º 1
0
 static bool isPathExists(BinarySearchTree.Node node, int sum)
 {
     if (node)
     {
         var isLeaf = isLeafNode(node);
         if (isLeaf && sum - node == 0)
         {
             return(true);
         }
         else if (isLeaf && sum - node != 0)
         {
             return(false);
         }
         var isEx = isPathExists(node.Right, sum - node);
         if (!isEx)
         {
             return(isPathExists(node.Left, sum - node));
         }
         else
         {
             return(true);
         }
         //if (node.Right && node.Right <= sum - node)
         //{
         //    return isPathExists(node.Right, sum - node);
         //}
         //else if (node.Left && node.Left <= sum - node)
         //{
         //    return isPathExists(node.Left, sum - node);
         //}
     }
     return(false);
 }
        static void Main1(string[] args)
        {
            BinarySearchTree bst = new BinarySearchTree();

            bst.Add(10);
            bst.Add(16);
            bst.Add(13);
            bst.Add(6);
            bst.Add(8);
            bst.Add(9);
            bst.Add(1);
            BinarySearchTree.Node lastSmall = new BinarySearchTree.Node(int.MinValue);
            FindClosest(18, bst.Root, ref lastSmall);
        }
Exemplo n.º 3
0
 static void GetLargest(BinarySearchTree.Node node, int n, ref int minNum)
 {
     if (node)
     {
         if (node <= n)
         {
             if (minNum < node)
             {
                 minNum = node;
             }
             GetLargest(node.Right, n, ref minNum);
         }
         else if (node > n)
         {
             GetLargest(node.Left, n, ref minNum);
         }
     }
     return;
 }
 public static void FindClosest(int num, BinarySearchTree.Node node, ref BinarySearchTree.Node lastSmallNode)
 {
     if (node != null)
     {
         if (node.Data == num)
         {
             lastSmallNode = node;
         }
         if (node.Data > num)
         {
             FindClosest(num, node.Left, ref lastSmallNode);
         }
         else
         {
             if (lastSmallNode.Data < node.Data)
             {
                 lastSmallNode = node;
             }
             FindClosest(num, node.Right, ref lastSmallNode);
         }
     }
 }
Exemplo n.º 5
0
 static bool isLeafNode(BinarySearchTree.Node node)
 {
     return(!node.Right && !node.Left);
 }