Advertisement
AvengersAssemble

Lab 21.1

Jan 21st, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Unit4.CollectionsLib;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         private static Random num = new Random();
  11.  
  12.         //טענת כניסה: מקבלת מספר כלשהו איקס
  13.         //טענת יציאה: מחזירה עץ בינארי רנדומלי בגובה איקס
  14.         static BinTreeNode<int> BuildT(int x)
  15.         {
  16.             int a;
  17.             if (x < 0) return null;
  18.             if (x == 0) return new BinTreeNode<int>(num.Next(1, 21));
  19.             if (x > 0)
  20.             {
  21.                 a = num.Next(1, 4);
  22.                 if (a == 1)
  23.                     return new BinTreeNode<int>(null, num.Next(1, 21), BuildT(x - 1));
  24.                 if (a == 2)
  25.                     return new BinTreeNode<int>(BuildT(x - 1), num.Next(1, 21), null);
  26.                 if (a == 3)
  27.                     return new BinTreeNode<int>(BuildT(x - 1), num.Next(1, 21), BuildT(x - 1));
  28.             }
  29.             return null;
  30.         }
  31.  
  32.         //טענת כניסה: מקבלת עץ בינארי
  33.         //טענת יציאה: מחזירה את מספר הרמות בעץ
  34.         //סיבוכיות O(n)
  35.         public static int Height(BinTreeNode<int> bt)
  36.         {
  37.             if (bt == null) //O(1)
  38.                 return -1; //O(1)
  39.             return 1 + Math.Max(Height(bt.GetLeft()), Height(bt.GetRight())); //O(n)
  40.         }
  41.  
  42.         //טענת כניסה: מקבלת עץ בינארי
  43.         //טענת יציאה: מחזירה אמת אם העץ הוא עץ בינארי מאוזן אחרת מחזירה שקר
  44.         //סיבוכיות O(n^2)
  45.         public static bool Balanced(BinTreeNode<int> t)
  46.         {
  47.             if (t == null) //O(1)
  48.                 return true; //O(1)
  49.             if (t.GetLeft() == t.GetRight()) //O(1)
  50.                 return true; //O(1)
  51.             if (Math.Abs(Height(t.GetLeft()) - Height(t.GetRight())) > 1) //O(n) כי מזומנות פעולות שהסיבוכיות שלהן היא O(n)
  52.                 return false; //O(1)
  53.             return Balanced(t.GetLeft()) && Balanced(t.GetRight()); //O(n^2)
  54.         }
  55.  
  56.         static void Main(string[] args)
  57.         {
  58.             BinTreeNode<int> t = BuildT(3);
  59.             Console.WriteLine(Balanced(t));
  60.             BinTreeUtils.ShowTree(t);
  61.         }
  62.     }
  63. }
  64.  
  65. ///
  66.  
  67. using System;
  68. using System.Linq;
  69. using System.Text;
  70. using Unit4.CollectionsLib;
  71.  
  72. namespace ConsoleApplication1
  73. {
  74.     public class Collec
  75.     {
  76.         private Stack<int> s;
  77.         private int tail;
  78.  
  79.  
  80.         public Collec()
  81.         {
  82.             this.s = new Stack<int>();
  83.         }
  84.  
  85.         public Collec(int n)
  86.         {
  87.             this.s = new Stack<int>();
  88.             this.s.Push(n);
  89.             this.tail = n;
  90.         }
  91.  
  92.         //טענת כניסה: מקבלת מספר n
  93.         //טענת יציאה: בודקת אם אפשר להוסיף אותו לאוסף, מחזירה אמת אם אפשר אחרת מחזירה שקר. אם אפשר להוסיף אותו לאוסף היא מוסיפה
  94.         //סיבוכיות O(1)
  95.         public bool Add(int n)
  96.         {
  97.             if (this.s.IsEmpty()) //O(1)
  98.             {
  99.                 this.tail = n; //O(1)
  100.                 this.s.Push(n); //O(1)
  101.                 return true; //O(1)
  102.             }
  103.             if (this.s.Top() > n) //O(1)
  104.                 return false; //O(1)
  105.             this.s.Push(n); //O(1)
  106.             return true; //O(1)
  107.         }
  108.  
  109.         //טענת יציאה: מחזירה את האיבר הקטן ביותר באוסף
  110.         //סיבוכיות O(1)
  111.         public int Small()
  112.         {
  113.             return this.tail; //O(1)
  114.         }
  115.  
  116.         //טענת כניסה: מקבלת עוד אוסף מסוג קולכ
  117.         //טענת יציאה: מחזירה את האיבר הקטן ביותר בשני האוספים
  118.         //סיבוכיות O(1)
  119.         public int Smallest(Collec c)
  120.         {
  121.             return Math.Min(c.Small(), this.tail); //O(1) כי מזומנות פעולות שהסיבוכיות שלהן היא גם O(1)
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement