Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.63 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4.  * STUDENTS: Your answers (your code) goes into this file!!!!
  5.  *
  6.   * NOTE: In addition to your answers, this file also contains a 'main' method,
  7.  *      in case you want to run a normal console application.
  8.  *
  9.  * If you want to / have to put something into 'Main' for these PCEs, then put that
  10.  * into the Program.Main method that is located below,
  11.  * then select this project as startup object
  12.  * (Right-click on the project, then select 'Set As Startup Object'), then run it
  13.  * just like any other normal, console app: use the menu item Debug->Start Debugging, or
  14.  * Debug->Start Without Debugging
  15.  *
  16.  */
  17.  
  18. namespace PCE_StarterProject
  19. {
  20.     public class Program
  21.     {
  22.         public static void Main(string[] args)
  23.         {
  24.             Console.WriteLine("Hello, world!");
  25.         }
  26.     }
  27.  
  28.     class DuplicateValueException : Exception
  29.     { public DuplicateValueException(string msg) : base(msg) { } }
  30.  
  31.     public class BST
  32.     {
  33.         private BSTNode m_top;
  34.  
  35.         private class BSTNode
  36.         {
  37.             private int m_data;
  38.             private BSTNode m_left;
  39.             private BSTNode m_right;
  40.  
  41.             public int Data
  42.             {
  43.                 get { return m_data; }
  44.                 set { m_data = value; }
  45.             }
  46.             public BSTNode Left
  47.             {
  48.                 get { return m_left; }
  49.                 set { m_left = value; }
  50.             }
  51.             public BSTNode Right
  52.             {
  53.                 get { return m_right; }
  54.                 set { m_right = value; }
  55.             }
  56.  
  57.             public BSTNode(int data)
  58.             {
  59.                 m_data = data;
  60.             }
  61.         }
  62.  
  63.         public void AddValue(int v)
  64.         {
  65.             if (m_top == null)
  66.             {
  67.                 m_top = new BSTNode(v);
  68.             }
  69.             else
  70.             {
  71.                 BSTNode cur = m_top;
  72.                 while (true)
  73.                 {
  74.                     if (v < cur.Data)
  75.                     {
  76.                         if (cur.Left == null)
  77.                         {
  78.                             cur.Left = new BSTNode(v);
  79.                             return;
  80.                         }
  81.                         else
  82.                             cur = cur.Left;
  83.                     }
  84.                     else if (v > cur.Data)
  85.                     {
  86.                         if (cur.Right == null)
  87.                         {
  88.                             cur.Right = new BSTNode(v);
  89.                             return;
  90.                         }
  91.                         else
  92.                             cur = cur.Right;
  93.                     }
  94.                     else
  95.                         throw new DuplicateValueException("Value " + v + " is already in the tree!");
  96.                 }
  97.             }
  98.         }
  99.  
  100.         public void Print()
  101.         {
  102.             Console.WriteLine("=== Printing the tree ===");
  103.             if (m_top == null)
  104.                 Console.WriteLine("Empty tree!");
  105.             else
  106.                 PrintInternal(m_top);
  107.         }
  108.         private void PrintInternal(BSTNode cur)
  109.         {
  110.             if (cur.Left != null)
  111.                 PrintInternal(cur.Left);
  112.  
  113.             Console.WriteLine(cur.Data);
  114.  
  115.             if (cur.Right != null)
  116.                 PrintInternal(cur.Right);
  117.         }
  118.  
  119.         public bool Find(int target)
  120.         {
  121.             return FindNode(target) != null;
  122.         }
  123.  
  124.         private BSTNode FindNode(int target)
  125.         {
  126.             BSTNode cur = m_top;
  127.             while (cur != null)
  128.             {
  129.                 if (cur.Data == target)
  130.                     return cur;
  131.                 else if (target < cur.Data)
  132.                     cur = cur.Left;
  133.                 else if (target > cur.Data)
  134.                     cur = cur.Right;
  135.             }
  136.  
  137.             return null;
  138.         }
  139.  
  140.  
  141.         public void Remove(int target)
  142.         {
  143.             // if the tree is empty:
  144.             if (m_top == null)
  145.                 return;
  146.             //else
  147.             //    throw new DuplicateValueException("Value " + target + " is already in the tree!");
  148.  
  149.             if (m_top.Data == target)
  150.             {
  151.                 RemoveRootNode(target);
  152.             }
  153.             else
  154.             {
  155.                 RemoveNonrootNode(target);
  156.             }
  157.         }
  158.  
  159.         private void RemoveNonrootNode(int target)
  160.         {
  161.             BSTNode cur = m_top;
  162.             BSTNode parent = null;
  163.  
  164.             //First, find the target node that we need to remove
  165.             // we'll have the 'parent' reference trail the cur pointer down the tree
  166.             // so when we stop, cur is the node to remove, and parent is one above it.
  167.             while (true)
  168.             {
  169.                 if (cur.Data == target)
  170.                 {
  171.                     break;
  172.                 }
  173.                 else if (cur.Data > target)
  174.                 {
  175.                     parent = cur;
  176.                     cur = cur.Left;
  177.                 }
  178.                 else
  179.                 {
  180.                     parent = cur;
  181.                     cur = cur.Right;
  182.                 }
  183.             }
  184.  
  185.             // Next, we figure out which of the cases we're in
  186.  
  187.             // Case 1: The target node has no children
  188.             if (cur.Left == null && cur.Right == null)
  189.             {
  190.                 cur = parent;
  191.                 cur.Left = null;
  192.                 cur.Right = null;
  193.  
  194.                 return;
  195.             }
  196.             // Case 2: The target node has 1 child
  197.             // (You may want to split out the left vs. right child thing)
  198.             else if (cur.Left == null || cur.Right == null)
  199.             {
  200.                 BSTNode temp = (cur.Left == null) ? cur.Right : cur.Left;
  201.                 if (parent.Left == cur)
  202.                     parent.Left = temp;
  203.                 else
  204.                     parent.Right = temp;
  205.             }
  206.  
  207.             // Case 3: The target node has 1 child
  208.             else
  209.             {
  210.                 int temp = TestFindAndRemoveNextSmallest(cur.Data);
  211.                 parent.Data = temp;
  212.             }
  213.         }
  214.  
  215.         private void RemoveRootNode(int target)
  216.         {
  217.             // If we're here, it's because we're removing the top-most node (the 'root' node)
  218.  
  219.             // Case 1: Root has no children
  220.             if (m_top.Left == null && m_top.Right == null)
  221.             {
  222.                 m_top = null;            // Therefore, the tree is now empty
  223.                 return;
  224.             }
  225.             // Case 2: Root has 1 child
  226.             else if (m_top.Left == null)
  227.             {
  228.                 // 1 (right) child, OR zero children (right may also be null)
  229.                 m_top = m_top.Right; // Right is null or another node - either way is correct
  230.                 return;
  231.             }
  232.             else if (m_top.Right == null)
  233.             {
  234.                 // If we're here, Left is not null, so there MUST be one (Left) Child
  235.                 m_top = m_top.Left;
  236.                 return;
  237.             }
  238.             // Case 3: Root has two children - this is where it gets interesting :)
  239.             else
  240.             {
  241.                 // 2 children - find (and remove) next smaller value
  242.                 // use that data to overwrite the current data.
  243.                 BSTNode removee = FindAndRemoveNextSmallerValue(target, m_top);
  244.                 m_top.Data = removee.Data;
  245.                 return;
  246.             }
  247.         }
  248.  
  249.         /// <summary>
  250.         /// This method takes 1 step to the left, then walks as far to the right
  251.         /// as possible.  Once that right-most node is found, it's removed & returned.
  252.         /// Note that the node MAY be immediately to the left of the <B>startHere</B>
  253.         /// parameter, if startHere.Left.Right == null
  254.         /// </summary>
  255.         /// <param name="smallerThanThis"></param>
  256.         /// <param name="startHere"></param>
  257.         /// <returns></returns>
  258.         private BSTNode FindAndRemoveNextSmallerValue(int smallerThanThis, BSTNode startHere)
  259.         {
  260.             BSTNode parent = startHere;
  261.             BSTNode child = startHere.Left;
  262.  
  263.             return null;
  264.         }
  265.  
  266.         // Given the value of a node, find (and remove) the predessor node in the tree
  267.         // returns the value of the predecessor node, or Int32.MinValue if no such value was found
  268.         public int TestFindAndRemoveNextSmallest(int sourceNode)
  269.         {
  270.             BSTNode startAt = this.FindNode(sourceNode);
  271.             // sourceNode should == startAt.Data, unless startAt is null)
  272.             BSTNode removed = FindAndRemoveNextSmallerValue(sourceNode, startAt);
  273.             if (removed != null)
  274.                 return removed.Data;
  275.             else
  276.                 return Int32.MinValue;
  277.         }
  278.     }
  279.  
  280.  
  281.     public class SearchingAndSorting
  282.     {
  283.         // Answer O(nlogn)
  284.         // Answer O(1) space
  285.         public int Partition(int[] arr, int low, int high)
  286.         {
  287.             int pivot = arr[low];
  288.  
  289.             // index of the smaller element
  290.             int i = (low - 1);
  291.             for (int j = low; j < high; j++)
  292.             {
  293.                 if (arr[j] <= pivot)
  294.                 {
  295.                     i++;
  296.                     int temp = arr[i];
  297.                     arr[i] = arr[j];
  298.                     arr[j] = temp;
  299.                 }
  300.             }
  301.  
  302.             int temp1 = arr[i + 1];
  303.  
  304.             arr[i + 1] = arr[low];
  305.  
  306.             arr[low] = temp1;
  307.  
  308.             return i + 1;
  309.         }
  310.  
  311.  
  312.         public void QuickSort(int[] nums)
  313.         {
  314.             QuickSort(nums, 0, nums.Length - 1);          
  315.  
  316.         }
  317.  
  318.         private void QuickSort(int[] nums, int iStart, int iEnd)
  319.         {
  320.             if (iStart < iEnd)
  321.             {
  322.                 int pi = partition(nums, iStart, iEnd);
  323.                 QuickSort(nums, iStart, pi - 1);
  324.                 QuickSort(nums, pi - 1, iEnd);
  325.             }
  326.         }
  327.  
  328.             //finding the parttion index
  329.         private void swap(int a, int b)
  330.         {
  331.             int temp = a;
  332.             a = b;
  333.             b = temp;
  334.         }
  335.         private int partition(int[] arr, int low, int high)
  336.         {
  337.             int pivot = arr[high]; // pivot
  338.             int i = (low - 1); // Index of smaller element
  339.  
  340.         for (int j = low; j <= high - 1; j++)
  341.         {
  342.             // If current element is smaller than the pivot
  343.             if (arr[j] < pivot)
  344.             {
  345.                 i++; // increment index of smaller element
  346.                 swap(arr[i], arr[j]);
  347.             }
  348.         }
  349.         swap(arr[i + 1], arr[high]);
  350.         return (i + 1);
  351.         }
  352.     }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement