Ramirez_RD

Binray Search C#_Samson

Sep 30th, 2025 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Node
  6. {
  7.     public int Data;
  8.     public Node Left;
  9.     public Node Right;
  10.  
  11.     public Node(int data)
  12.     {
  13.         Data = data;
  14.         Left = null;
  15.         Right = null;
  16.     }
  17. }
  18.  
  19. public class BinarySearchTreeChecker
  20. {
  21.     private const long INT_MIN_LONG = long.MinValue;
  22.     private const long INT_MAX_LONG = long.MaxValue;
  23.  
  24.     public static bool IsBinarySearchTree(Node root)
  25.     {
  26.         return IsBSTUtil(root, INT_MIN_LONG, INT_MAX_LONG);
  27.     }
  28.  
  29.     private static bool IsBSTUtil(Node node, long min_val, long max_val)
  30.     {
  31.         if (node == null)
  32.         {
  33.             return true;
  34.         }
  35.  
  36.         if (node.Data < min_val || node.Data > max_val)
  37.         {
  38.             return false;
  39.         }
  40.  
  41.         bool isLeftValid = IsBSTUtil(node.Left, min_val, (long)node.Data - 1);
  42.         if (!isLeftValid)
  43.         {
  44.             return false;
  45.         }
  46.  
  47.         bool isRightValid = IsBSTUtil(node.Right, (long)node.Data, max_val);
  48.        
  49.         return isRightValid;
  50.     }
  51. }
  52.  
  53. public class Program
  54. {
  55.     public static void Main()
  56.     {
  57.         Console.WriteLine("--- Binary Search Tree Checker ---");
  58.         Console.WriteLine("Enter the data for the 7 nodes separated by spaces:");
  59.        
  60.         Console.Write("Enter 7 numbers: ");
  61.         string inputLine = Console.ReadLine();
  62.        
  63.         int[] elements;
  64.         try
  65.         {
  66.             elements = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  67.                                 .Select(s => int.Parse(s.Trim()))
  68.                                 .ToArray();
  69.         }
  70.         catch (FormatException)
  71.         {
  72.             Console.WriteLine("\nInvalid input. Please ensure all entries are integers separated by spaces. Exiting.");
  73.             return;
  74.         }
  75.  
  76.         if (elements.Length != 7)
  77.         {
  78.             Console.WriteLine($"\nInput error: Expected 7 numbers but received {elements.Length}. Exiting.");
  79.             return;
  80.         }
  81.        
  82.         Node root = new Node(elements[0]);
  83.        
  84.         root.Left = new Node(elements[1]);
  85.        
  86.         root.Right = new Node(elements[2]);
  87.        
  88.         root.Left.Left = new Node(elements[3]);
  89.        
  90.         root.Left.Right = new Node(elements[4]);
  91.        
  92.         root.Right.Left = new Node(elements[5]);
  93.        
  94.         root.Right.Right = new Node(elements[6]);
  95.  
  96.         Console.WriteLine("\n--- Tree Construction Complete. Checking structure... ---");
  97.  
  98.         bool is_bst = BinarySearchTreeChecker.IsBinarySearchTree(root);
  99.  
  100.         Console.Write("\nThe inputted data elements are structured as a: ");
  101.         if (is_bst)
  102.         {
  103.             Console.WriteLine("BINARY SEARCH TREE (BST).");
  104.         }
  105.         else
  106.         {
  107.             Console.WriteLine("NOT a Binary Search Tree (BST).");
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment