Advertisement
myname0

AVLTree

Apr 8th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.60 KB | None | 0 0
  1. namespace AVL
  2. {  
  3.     public class AVLTree
  4.     {
  5.         public class Node
  6.         {
  7.             private string name;
  8.             private string phone;
  9.             private string tariff;
  10.             private Node Left;
  11.             private Node Right;
  12.             private int height;
  13.  
  14.             public Node(string name_, string phone_, string tariff_)
  15.             {
  16.                 name = name_;
  17.                 phone = phone_;
  18.                 tariff = tariff_;
  19.                 Left = null;
  20.                 Right = null;
  21.                 height = 1;
  22.             }
  23.  
  24.             private static int Height(Node AVL)
  25.             {
  26.                 return (AVL != null) ? AVL.height : 0;
  27.             }
  28.  
  29.             private static int Bfactor (Node AVL)
  30.             {
  31.                 return (Height(AVL.Right) - Height(AVL.Left));
  32.             }
  33.  
  34.             private static void Fixheight(Node AVL)
  35.             {
  36.                 int hl = Height(AVL.Left);
  37.                 int hr = Height(AVL.Right);
  38.                 AVL.height = (hl > hr ? hl : hr) + 1;
  39.             }
  40.  
  41.             private static void RotateRight(ref Node AVL)
  42.             {
  43.                 Node tmp = AVL.Left;
  44.                 AVL.Left = tmp.Right;
  45.                 tmp.Right = AVL;
  46.                 Fixheight(AVL);
  47.                 Fixheight(tmp);
  48.                 AVL = tmp;
  49.             }
  50.  
  51.             private static void RotateLeft(ref Node AVL)
  52.             {
  53.                 Node tmp = AVL.Right;
  54.                 tmp.Right = tmp.Left;
  55.                 tmp.Left = AVL;
  56.                 Fixheight(AVL);
  57.                 Fixheight(tmp);
  58.                 AVL =  tmp;
  59.             }
  60.  
  61.             private static void Balance(ref Node AVL)
  62.             {
  63.                 Fixheight(AVL);
  64.                 if (Bfactor(AVL) == 2)
  65.                 {
  66.                     if (Bfactor(AVL.Right) < 0)
  67.                         RotateRight(ref AVL.Right);
  68.                     RotateLeft(ref AVL);
  69.                 }
  70.                 if (Bfactor(AVL) == -2)
  71.                 {
  72.                     if (Bfactor(AVL.Left) > 0)
  73.                         RotateLeft(ref AVL.Left);
  74.                     RotateRight(ref AVL);
  75.                 }
  76.             }
  77.  
  78.             public static void Add(ref Node AVL, string _name, string _phone, string _tariff)
  79.             {
  80.                 if (AVL == null)  AVL = new Node(_name, _phone, _tariff);
  81.                 else if (((IComparable)(AVL.phone)).CompareTo(_phone) < 0)
  82.                     Add(ref AVL.Left, _name, _phone, _tariff);
  83.                 else    
  84.                     Add(ref AVL.Right, _name, _phone, _tariff);
  85.                 Balance(ref AVL);
  86.             }
  87.  
  88.             private Node FindMin(Node AVL)
  89.             {
  90.                 return (AVL.Left != null) ? FindMin(AVL.Left) : AVL;
  91.             }
  92.  
  93.             public static void RemoveMin(Node AVL, ref Node AVLT)
  94.             {
  95.                 if (AVLT.Right != null)
  96.                     RemoveMin(AVL, ref AVLT.Right);
  97.                 else
  98.                 {
  99.                     AVL.name = AVLT.name;
  100.                     AVLT = AVLT.Left;
  101.                 }
  102.             }
  103.                          
  104.             public static void RemoveByTelephone(ref Node AVL, string number)
  105.             {
  106.                 if(AVL == null) throw new Exception ("This phone number doesn't belongs tree!");
  107.                 if (((IComparable)(AVL.phone)).CompareTo(number) < 0)
  108.                     RemoveByTelephone(ref AVL.Left, number);
  109.                 else if (((IComparable)(AVL.phone)).CompareTo(number) > 0)
  110.                     RemoveByTelephone(ref AVL.Right, number);
  111.                 else
  112.                 {
  113.                     if(AVL.Left == null) AVL = AVL.Right;
  114.                     if(AVL.Right == null) AVL = AVL.Left;
  115.                     else RemoveMin(AVL, ref AVL.Left);
  116.                 }
  117.                 Balance(ref AVL);
  118.             }
  119.  
  120.             public static void FindByPhone (Node AVL, string number, out Node result)
  121.             {
  122.                 if (AVL == null)    result = null;
  123.                 else
  124.                 {
  125.                     if (((IComparable)(AVL.phone)).CompareTo(number) == 0)
  126.                     {
  127.                         result = AVL;
  128.                         Console.WriteLine("{0} - {1} - {2}", AVL.name, AVL.phone, AVL.tariff);
  129.                         Console.WriteLine();
  130.                     }
  131.                     else if (((IComparable)(AVL.phone)).CompareTo(number) < 0)
  132.                         FindByPhone(AVL.Left, number, out result);
  133.                     else FindByPhone(AVL.Right, number, out result);
  134.                 }
  135.             }
  136.  
  137.             public static void Print(Node AVL)
  138.                 {
  139.                     if (AVL != null)
  140.                     {
  141.                         Console.WriteLine("{0} - {1} - {2}", AVL.name, AVL.phone, AVL.tariff);
  142.                         Print(AVL.Left);
  143.                         Print(AVL.Right);
  144.                     }
  145.                 }
  146.  
  147.             public static void GetMostPopularTariff(Node AVL)
  148.             {
  149.                 if (AVL != null)
  150.                 {
  151.                     Dictionary<string, int> allTariffes = new Dictionary<string, int>();
  152.                     CounterOfTariffes(AVL, ref allTariffes);
  153.  
  154.                     int max = 0;
  155.                     foreach(var i in allTariffes)
  156.                         if(i.Value > max)   max = i.Value;
  157.  
  158.                     Console.Write("The most popular tariff is: ");
  159.  
  160.                     foreach(var i in allTariffes)
  161.                         if(i.Value == max)  Console.Write("{0} ", i.Key);
  162.  
  163.                     Console.WriteLine();
  164.                 }
  165.             }
  166.  
  167.             private static void CounterOfTariffes(Node AVL, ref Dictionary <string, int> allTariffes)
  168.             {
  169.                 if(AVL != null)
  170.                 {
  171.                     if (allTariffes.ContainsKey(AVL.tariff))
  172.                         allTariffes[AVL.tariff] += 1;
  173.                     else allTariffes.Add(AVL.tariff, 0);
  174.  
  175.                     CounterOfTariffes(AVL.Left, ref allTariffes);
  176.                     CounterOfTariffes(AVL.Right, ref allTariffes);                    
  177.                 }
  178.             }
  179.                
  180.         }
  181.  
  182.         Node tree;
  183.  
  184.         public AVLTree()
  185.         {
  186.             tree = null;
  187.         }
  188.  
  189.         private AVLTree(Node tmp)
  190.         {
  191.             tree = tmp;
  192.         }
  193.  
  194.         public void Add(string name, string phone, string tariff)
  195.         {
  196.             Node.Add(ref tree, name, phone, tariff);
  197.         }
  198.  
  199.         public void RemoveByTelephone(string number)
  200.         {
  201.             Node.RemoveByTelephone(ref tree, number);
  202.         }
  203.  
  204.         public void Print()
  205.         {
  206.             Node.Print(tree);
  207.             Console.WriteLine();
  208.         }
  209.  
  210.         public void GetMostPopularTariff()
  211.         {
  212.             Node.GetMostPopularTariff(tree);
  213.         }
  214.  
  215.         public AVLTree FindByPhone(string number)
  216.         {
  217.             Node res;
  218.             Node.FindByPhone(tree, number, out res);
  219.             if (res == null) throw new Exception("Object with this phone number is not found!");
  220.             return new AVLTree(res);
  221.         }
  222.  
  223.         public void FormAVLTree(StreamReader fin)
  224.         {
  225.             string name, tariff;
  226.             string[] tmp;
  227.             while (!fin.EndOfStream)
  228.             {
  229.                 tmp = fin.ReadLine().Split(' ');
  230.                 if (tmp.Length == 5)
  231.                 {
  232.                     name = tmp[0] + ' ' + tmp[1] + ' ' + tmp[2];
  233.                     Node.Add(ref tree, name, tmp[3], tmp[4]);
  234.                 }
  235.                 else
  236.                 {
  237.                     name = tmp[0] + ' ' + tmp[1] + ' ' + tmp[2];
  238.                     tariff = tmp[4] + ' ' + tmp[5];
  239.                     Node.Add(ref tree, name, tmp[3], tariff);
  240.                 }
  241.             }
  242.         }
  243.     }
  244.  
  245.     class Program
  246.     {
  247.         static void Main(string[] args)
  248.         {
  249.             AVLTree tree = new AVLTree();
  250.             AVLTree tmp = new AVLTree();
  251.  
  252.             StreamReader fin = new StreamReader("input.txt");
  253.             tree.FormAVLTree(fin);
  254.             fin.Close();
  255.             tree.Print();
  256.  
  257.          
  258.             tree.Add("Иванов Сергей Иванович", "9213456532", "Все включено");
  259.             tree.Add("Гордеев Андрей Владимирович", "9653214333", "Домашний");
  260.             tree.Add("Грачева Антонина Николаевна", "9653214334", "Все включено");
  261.             tree.Add("Андреянова Светлана Георгиевна", "9653214335", "Домашний");
  262.             tree.Add("Соколов Виктор Сергеевич", "9653214336", "Домашний");
  263.             tree.Add("Петров Геннадий Вадимович", "9653214337", "Все включено");
  264.             Console.WriteLine("Tree after adding new elements: ");
  265.             tree.Print();
  266.  
  267.             tree.RemoveByTelephone("9653214336");
  268.             Console.WriteLine("Remove of 9653214336:");
  269.             tree.Print();
  270.  
  271.             Console.WriteLine("Find by number:");
  272.             tmp = tree.FindByPhone("9653214334");
  273.  
  274.             tree.GetMostPopularTariff();
  275.         }
  276.     }
  277. }
  278.  
  279. //input
  280. Иванов Антон Иванович 9213456538 Все включено
  281. Кузьмин Андрей Владимирович 9653214339 Семейный
  282. Лососева Антонина Николаевна 9653214340 Все включено
  283. Кулякина Светлана Георгиевна 9653214341 Домашний
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement