mausha

BST_Exercise

Feb 1st, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class BinarySearchTree<T> : IBinarySearchTree<T> where T : IComparable
  5. {
  6.     private Node root;
  7.  
  8.     private Node FindElement(T element)
  9.     {
  10.         Node current = this.root;
  11.  
  12.         while (current != null)
  13.         {
  14.             if (current.Value.CompareTo(element) > 0)
  15.             {
  16.                 current = current.Left;
  17.             }
  18.             else if (current.Value.CompareTo(element) < 0)
  19.             {
  20.                 current = current.Right;
  21.             }
  22.             else
  23.             {
  24.                 break;
  25.             }
  26.         }
  27.  
  28.         return current;
  29.     }
  30.  
  31.     private void PreOrderCopy(Node node)
  32.     {
  33.         if (node == null)
  34.         {
  35.             return;
  36.         }
  37.  
  38.         this.Insert(node.Value);
  39.         this.PreOrderCopy(node.Left);
  40.         this.PreOrderCopy(node.Right);
  41.     }
  42.  
  43.     private Node Insert(T element, Node node)
  44.     {
  45.         if (node == null)
  46.         {
  47.             node = new Node(element);
  48.         }
  49.         else if (element.CompareTo(node.Value) < 0)
  50.         {
  51.             node.Left = this.Insert(element, node.Left);
  52.         }
  53.         else if (element.CompareTo(node.Value) > 0)
  54.         {
  55.             node.Right = this.Insert(element, node.Right);
  56.         }
  57.  
  58.         return node;
  59.     }
  60.  
  61.     private void Range(Node node, Queue<T> queue, T startRange, T endRange)
  62.     {
  63.         if (node == null)
  64.         {
  65.             return;
  66.         }
  67.  
  68.         int nodeInLowerRange = startRange.CompareTo(node.Value);
  69.         int nodeInHigherRange = endRange.CompareTo(node.Value);
  70.  
  71.         if (nodeInLowerRange < 0)
  72.         {
  73.             this.Range(node.Left, queue, startRange, endRange);
  74.         }
  75.         if (nodeInLowerRange <= 0 && nodeInHigherRange >= 0)
  76.         {
  77.             queue.Enqueue(node.Value);
  78.         }
  79.         if (nodeInHigherRange > 0)
  80.         {
  81.             this.Range(node.Right, queue, startRange, endRange);
  82.         }
  83.     }
  84.  
  85.     private void EachInOrder(Node node, Action<T> action)
  86.     {
  87.         if (node == null)
  88.         {
  89.             return;
  90.         }
  91.  
  92.         this.EachInOrder(node.Left, action);
  93.         action(node.Value);
  94.         this.EachInOrder(node.Right, action);
  95.     }
  96.  
  97.     private BinarySearchTree(Node node)
  98.     {
  99.         this.PreOrderCopy(node);
  100.     }
  101.  
  102.     public BinarySearchTree()
  103.     {
  104.     }
  105.  
  106.     public void Insert(T element)
  107.     {
  108.         this.root = this.Insert(element, this.root);
  109.     }
  110.  
  111.     public bool Contains(T element)
  112.     {
  113.         Node current = this.FindElement(element);
  114.  
  115.         return current != null;
  116.     }
  117.  
  118.     public void EachInOrder(Action<T> action)
  119.     {
  120.         this.EachInOrder(this.root, action);
  121.     }
  122.  
  123.     public BinarySearchTree<T> Search(T element)
  124.     {
  125.         Node current = this.FindElement(element);
  126.  
  127.         return new BinarySearchTree<T>(current);
  128.     }
  129.  
  130.     public void DeleteMin()
  131.     {
  132.         if (this.root == null)
  133.         {
  134.             throw new InvalidOperationException();
  135.         }
  136.  
  137.         Node current = this.root;
  138.         Node parent = null;
  139.         while (current.Left != null)
  140.         {
  141.             parent = current;
  142.             current = current.Left;
  143.         }
  144.  
  145.         if (parent == null)
  146.         {
  147.             this.root = this.root.Right;
  148.         }
  149.         else
  150.         {
  151.             parent.Left = current.Right;
  152.         }
  153.     }
  154.  
  155.     public IEnumerable<T> Range(T startRange, T endRange)
  156.     {
  157.         Queue<T> queue = new Queue<T>();
  158.  
  159.         this.Range(this.root, queue, startRange, endRange);
  160.  
  161.         return queue;
  162.     }
  163.  
  164.     public void Delete(T element)
  165.     {
  166.         if (this.root == null || !this.Contains(element))
  167.         {
  168.             throw new InvalidOperationException();
  169.         }
  170.  
  171.         this.root = this.Delete(this.root, element);
  172.     }
  173.  
  174.     private Node Delete(Node node, T element)
  175.     {
  176.         if (node == null)
  177.         {
  178.             return null;
  179.         }
  180.  
  181.         var compare = node.Value.CompareTo(element);
  182.         if (compare > 0)
  183.         {
  184.             node.Left = this.Delete(node.Left, element);
  185.         }
  186.         else if (compare < 0)
  187.         {
  188.             node.Right = this.Delete(node.Right, element);
  189.         }
  190.         else
  191.         {
  192.             if (node.Left == null)
  193.             {
  194.                 return node.Right;
  195.             }
  196.  
  197.             if (node.Right == null)
  198.             {
  199.                 return node.Left;
  200.             }
  201.  
  202.             var leftMostNode = this.LeftMost(node.Right);
  203.             node.Value = leftMostNode.Value;
  204.             node.Right = this.Delete(node.Right, leftMostNode.Value);
  205.         }
  206.  
  207.         return node;
  208.     }
  209.  
  210.     private Node LeftMost(Node node)
  211.     {
  212.         var currentNode = node;
  213.         while (currentNode.Left != null)
  214.         {
  215.             currentNode = currentNode.Left;
  216.         }
  217.  
  218.         return currentNode;
  219.     }
  220.  
  221.     public void DeleteMax()
  222.     {
  223.         if (this.root == null)
  224.         {
  225.             throw new InvalidOperationException();
  226.         }
  227.  
  228.         this.root = this.DeleteMax(this.root);
  229.     }
  230.  
  231.     private Node DeleteMax(Node node)
  232.     {
  233.         if (node.Right == null)
  234.         {
  235.             return node.Left;
  236.         }
  237.  
  238.         node.Right = this.DeleteMax(node.Right);
  239.  
  240.         return node;
  241.     }
  242.  
  243.     public int Count()
  244.     {
  245.         return this.Count(this.root);
  246.     }
  247.  
  248.     private int Count(Node node)
  249.     {
  250.         var count = 0;
  251.         if (node == null)
  252.         {
  253.             return count;
  254.         }
  255.  
  256.         count += this.Count(node.Left);
  257.         count++;
  258.         count += this.Count(node.Right);
  259.  
  260.         return count;
  261.     }
  262.  
  263.     public int Rank(T element)
  264.     {
  265.         return this.Rank(this.root, element);
  266.     }
  267.  
  268.     private int Rank(Node node, T element)
  269.     {
  270.         if (node == null)
  271.         {
  272.             return 0;
  273.         }
  274.  
  275.         var compare = node.Value.CompareTo(element);
  276.         if (compare > 0)
  277.         {
  278.             return this.Rank(node.Left, element);
  279.         }
  280.  
  281.         if (compare < 0)
  282.         {
  283.             return 1 + this.Count(node.Left) + this.Rank(node.Right, element);
  284.         }
  285.  
  286.         return this.Count(node.Left);
  287.     }
  288.  
  289.     public T Select(int rank)
  290.     {
  291.         if (this.root == null)
  292.         {
  293.             throw new InvalidOperationException();
  294.         }
  295.  
  296.         return this.Select(this.root, rank);
  297.     }
  298.  
  299.     private T Select(Node node, int number)
  300.     {
  301.         if (node == null)
  302.         {
  303.             return default(T);
  304.         }
  305.  
  306.         var count = this.Count(node.Left);
  307.         if (count > number)
  308.         {
  309.             return this.Select(node.Left, number);
  310.         }
  311.  
  312.         if (count < number)
  313.         {
  314.             return this.Select(node.Right, number - (count + 1));
  315.         }
  316.  
  317.         return node.Value;
  318.     }
  319.  
  320.     public T Ceiling(T element)
  321.     {
  322.         if (this.root == null)
  323.         {
  324.             throw new InvalidOperationException();
  325.         }
  326.  
  327.         return this.Select(this.Rank(element) + 1);
  328.     }
  329.  
  330.     public T Floor(T element)
  331.     {
  332.         if (this.root == null)
  333.         {
  334.             throw new InvalidOperationException();
  335.         }
  336.  
  337.         return this.Select(this.Rank(element) - 1);
  338.     }
  339.  
  340.     private class Node
  341.     {
  342.         public Node(T value)
  343.         {
  344.             this.Value = value;
  345.         }
  346.  
  347.         public T Value { get; set; }
  348.         public Node Left { get; set; }
  349.         public Node Right { get; set; }
  350.     }
  351. }
  352.  
  353. public class Launcher
  354. {
  355.     public static void Main(string[] args)
  356.     {
  357.         BinarySearchTree<int> bst = new BinarySearchTree<int>();
  358.  
  359.         bst.Insert(10);
  360.         bst.Insert(5);
  361.         bst.Insert(3);
  362.         bst.Insert(1);
  363.         bst.Insert(4);
  364.         bst.Insert(8);
  365.         bst.Insert(9);
  366.         bst.Insert(37);
  367.         bst.Insert(39);
  368.         bst.Insert(45);
  369.  
  370.         Console.WriteLine(bst.Count());
  371.  
  372.         var result = new List<int>();
  373.         bst.EachInOrder(result.Add);
  374.         Console.WriteLine(string.Join(" ", result));
  375.     }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment