Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.16 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 (false)
  168.             {
  169.  
  170.             }
  171.  
  172.             // Next, we figure out which of the cases we're in
  173.  
  174.             // Case 1: The target node has no children
  175.             if (cur.Left == null && cur.Right == null)
  176.             {
  177.  
  178.  
  179.                 return;
  180.             }
  181.             // Case 2: The target node has 1 child
  182.             // (You may want to split out the left vs. right child thing)
  183.  
  184.  
  185.             // Case 3: The target node has 1 child
  186.  
  187.  
  188.  
  189.  
  190.  
  191.         }
  192.  
  193.         private void RemoveRootNode(int target)
  194.         {
  195.             // If we're here, it's because we're removing the top-most node (the 'root' node)
  196.  
  197.             // Case 1: Root has no children
  198.             if (m_top.Left == null && m_top.Right == null)
  199.             {
  200.                 m_top = null;            // Therefore, the tree is now empty
  201.                 return;
  202.             }
  203.             // Case 2: Root has 1 child
  204.             else if (m_top.Left == null)
  205.             {
  206.                 // 1 (right) child, OR zero children (right may also be null)
  207.                 m_top = m_top.Right; // Right is null or another node - either way is correct
  208.                 return;
  209.             }
  210.             else if (m_top.Right == null)
  211.             {
  212.                 // If we're here, Left is not null, so there MUST be one (Left) Child
  213.                 m_top = m_top.Left;
  214.                 return;
  215.             }
  216.             // Case 3: Root has two children - this is where it gets interesting :)
  217.             else
  218.             {
  219.                 // 2 children - find (and remove) next smaller value
  220.                 // use that data to overwrite the current data.
  221.                 BSTNode removee = FindAndRemoveNextSmallerValue(target, m_top);
  222.                 m_top.Data = removee.Data;
  223.                 return;
  224.             }
  225.         }
  226.  
  227.         /// <summary>
  228.         /// This method takes 1 step to the left, then walks as far to the right
  229.         /// as possible.  Once that right-most node is found, it's removed & returned.
  230.         /// Note that the node MAY be immediately to the left of the <B>startHere</B>
  231.         /// parameter, if startHere.Left.Right == null
  232.         /// </summary>
  233.         /// <param name="smallerThanThis"></param>
  234.         /// <param name="startHere"></param>
  235.         /// <returns></returns>
  236.         private BSTNode FindAndRemoveNextSmallerValue(int smallerThanThis, BSTNode startHere)
  237.         {
  238.             BSTNode parent = startHere;
  239.             BSTNode child = startHere.Left;
  240.  
  241.             return null;
  242.         }
  243.  
  244.         // Given the value of a node, find (and remove) the predessor node in the tree
  245.         // returns the value of the predecessor node, or Int32.MinValue if no such value was found
  246.         public int TestFindAndRemoveNextSmallest(int sourceNode)
  247.         {
  248.             BSTNode startAt = this.FindNode(sourceNode);
  249.             // sourceNode should == startAt.Data, unless startAt is null)
  250.             BSTNode removed = FindAndRemoveNextSmallerValue(sourceNode, startAt);
  251.             if (removed != null)
  252.                 return removed.Data;
  253.             else
  254.                 return Int32.MinValue;
  255.         }
  256.     }
  257.  
  258.  
  259.     public class SearchingAndSorting
  260.     {
  261.         public int Partition(int[] nums, int left, int right)
  262.         {
  263.             return -1;
  264.         }
  265.         public void QuickSort(int[] A)
  266.         {
  267.             for (int i = 0; i < A.Length; i++)
  268.                 A[i] = Int32.MinValue;
  269.         }
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement