Guest User

Untitled

a guest
May 11th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Xml.Serialization;
  10. using System.Runtime.Serialization.Formatters.Soap;
  11.  
  12. namespace ConsoleApplication20
  13. {
  14.     [Serializable]
  15.     public class BinarySearchTree<T>
  16.     {
  17.         [Serializable]
  18.         public class BinarySearchTreeNode<T>
  19.         {
  20.             T _data;
  21.             BinarySearchTreeNode<T> _left;
  22.             BinarySearchTreeNode<T> _right;
  23.  
  24.             public BinarySearchTreeNode()
  25.             {
  26.                 _data = default(T);
  27.                 _left = null;
  28.                 _right = null;
  29.             }
  30.  
  31.             public BinarySearchTreeNode(T data)
  32.             {
  33.                 _data = data;
  34.                 _left = null;
  35.                 _right = null;
  36.             }
  37.  
  38.             public T Data
  39.             {
  40.                 get
  41.                 {
  42.                     return _data;
  43.                 }
  44.  
  45.                 set
  46.                 {
  47.                     _data = value;
  48.                 }
  49.             }
  50.  
  51.             public BinarySearchTreeNode<T> Left
  52.             {
  53.                 get
  54.                 {
  55.                     return _left;
  56.                 }
  57.  
  58.                 set
  59.                 {
  60.                     _left = value;
  61.                 }
  62.             }
  63.  
  64.             public BinarySearchTreeNode<T> Right
  65.             {
  66.                 get
  67.                 {
  68.                     return _right;
  69.                 }
  70.  
  71.                 set
  72.                 {
  73.                     _right = value;
  74.                 }
  75.             }
  76.  
  77.         }
  78.  
  79.         //public delegate int Compare(T a, T b);
  80.         //Compare _cmp;
  81.  
  82.         BinarySearchTreeNode<T> _root;
  83.  
  84.         Comparison<T> _cmp;
  85.  
  86.         public BinarySearchTreeNode<T> Root
  87.         {
  88.             get
  89.             {
  90.                 return _root;
  91.             }
  92.         }
  93.  
  94.         public BinarySearchTree(Comparison<T> cmp)
  95.         {
  96.             _root = null;
  97.             _cmp = cmp;
  98.         }
  99.  
  100.         public BinarySearchTree()
  101.         {
  102.             _root = null;
  103.             _cmp = Comparer<T>.Default.Compare;
  104.         }
  105.  
  106.         public void TraverseInOrder(BinarySearchTreeNode<T> start)
  107.         {
  108.             if (start != null)
  109.             {
  110.                 Console.WriteLine(start.Data.ToString());
  111.                 TraverseInOrder(start.Left);
  112.                 TraverseInOrder(start.Right);
  113.             }
  114.         }
  115.  
  116.         public BinarySearchTreeNode<T> Find(T data)
  117.         {
  118.             if (_root == null)
  119.             {
  120.                 return null;
  121.             }
  122.  
  123.             BinarySearchTreeNode<T> current = _root;
  124.  
  125.             while (current != null)
  126.             {
  127.                 if (_cmp(data, current.Data) == 0)
  128.                 {
  129.                     return current;
  130.                 }
  131.  
  132.                 if (_cmp(data, current.Data) < 0)
  133.                 {
  134.                     current = current.Left;
  135.                 }
  136.                 else if (_cmp(data, current.Data) > 0)
  137.                 {
  138.                     current = current.Right;
  139.                 }
  140.             }
  141.  
  142.             return null;
  143.         }
  144.  
  145.         public bool Remove(T data, BinarySearchTreeNode<T> start = null, BinarySearchTreeNode<T> prev = null)
  146.         {
  147.             if (_root == null)
  148.             {
  149.                 return false;
  150.             }
  151.  
  152.             BinarySearchTreeNode<T> curr = start;
  153.  
  154.             if (start == null)
  155.             {
  156.                 curr = _root;
  157.             }
  158.  
  159.             if (_cmp(data, curr.Data) < 0)
  160.             {
  161.                 return Remove(data, curr.Left, curr);
  162.             }
  163.             else if (_cmp(data, curr.Data) > 0)
  164.             {
  165.                 return Remove(data, curr.Right, curr);
  166.             }
  167.  
  168.             if (_cmp(data, curr.Data) == 0)
  169.             {
  170.                 if (curr.Left == null && curr.Right == null)
  171.                 {
  172.                     if (prev == null)
  173.                     {
  174.                         _root = null;
  175.                         return true;
  176.                     }
  177.                     else if (_cmp(curr.Data, prev.Data) < 0)
  178.                     {
  179.                         prev.Left = null;
  180.                     }
  181.                     else if (_cmp(curr.Data, prev.Data) > 0)
  182.                     {
  183.                         prev.Right = null;
  184.                     }
  185.                 }
  186.                 else if (curr.Left != null && curr.Right == null)
  187.                 {
  188.                     if (prev == null)
  189.                     {
  190.                         _root = _root.Left;
  191.                     }
  192.                     else if (_cmp(curr.Data, prev.Data) < 0)
  193.                     {
  194.                         prev.Left = curr.Left;
  195.                     }
  196.                     else if (_cmp(curr.Data, prev.Data) > 0)
  197.                     {
  198.                         prev.Right = curr.Left;
  199.                     }
  200.                 }
  201.                 else if (curr.Left == null && curr.Right != null)
  202.                 {
  203.                     if (prev == null)
  204.                     {
  205.                         _root = _root.Right;
  206.                     }
  207.                     else if (_cmp(curr.Data, prev.Data) < 0)
  208.                     {
  209.                         prev.Left = curr.Right;
  210.                     }
  211.                     else if (_cmp(curr.Data, prev.Data) > 0)
  212.                     {
  213.                         prev.Right = curr.Right;
  214.                     }
  215.                 }
  216.                 else
  217.                 {
  218.  
  219.                     BinarySearchTreeNode<T> successor = curr.Right;
  220.                     BinarySearchTreeNode<T> successorPrev = curr.Right;
  221.  
  222.                     while (successor.Left != null)
  223.                     {
  224.                         successorPrev = successor;
  225.                         successor = successor.Left;
  226.                     }
  227.  
  228.                     if (successor != successorPrev)
  229.                     {
  230.                         successorPrev.Left = successor.Right;
  231.                         successor.Right = curr.Right;
  232.                     }
  233.                     else
  234.                     {
  235.                         curr.Right = successor.Right;
  236.                     }
  237.  
  238.                     successor.Left = curr.Left;
  239.  
  240.                     if (prev == null)
  241.                     {
  242.                         _root = successor;
  243.                     }
  244.                     else
  245.                     {
  246.                         if (_cmp(curr.Data, prev.Data) < 0)
  247.                         {
  248.                             prev.Left = successor;
  249.                         }
  250.                         else if (_cmp(curr.Data, prev.Data) > 0)
  251.                         {
  252.                             prev.Right = successor;
  253.                         }
  254.                     }
  255.                 }
  256.  
  257.                 return true;
  258.             }
  259.  
  260.             return false;
  261.         }
  262.  
  263.         public void Add(T data)
  264.         {
  265.             BinarySearchTreeNode<T> node = new BinarySearchTreeNode<T>(data);
  266.  
  267.             if (_root == null)
  268.             {
  269.                 _root = node;
  270.                 return;
  271.             }
  272.  
  273.             BinarySearchTreeNode<T> curr = _root;
  274.             BinarySearchTreeNode<T> prev = null;
  275.  
  276.             while (curr != null)
  277.             {
  278.                 prev = curr;
  279.                 if (_cmp(node.Data, curr.Data) < 0)
  280.                 {
  281.                     curr = curr.Left;
  282.                 }
  283.                 else
  284.                 {
  285.                     curr = curr.Right;
  286.                 }
  287.             }
  288.  
  289.             if (_cmp(node.Data, prev.Data) < 0)
  290.             {
  291.                 prev.Left = node;
  292.             }
  293.             else
  294.             {
  295.                 prev.Right = node;
  296.             }
  297.         }  
  298.  
  299.     }
  300.  
  301.     class Program
  302.     {
  303.         public static void SerializeToXml(string filename, object obj)
  304.         {
  305.             XmlSerializer serializer = new XmlSerializer(obj.GetType());
  306.  
  307.             using (Stream fs = new FileStream(filename, FileMode.Create))
  308.             {
  309.                 serializer.Serialize(fs, obj);
  310.             }
  311.         }
  312.  
  313.         public static object DeserializeFromXml(string filename, Type type)
  314.         {
  315.             XmlSerializer serializer = new XmlSerializer(type);
  316.  
  317.             object result = null;
  318.  
  319.             using (Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
  320.             {
  321.                 result = serializer.Deserialize(fs);
  322.             }
  323.  
  324.             return result;
  325.         }
  326.  
  327.         public static object DeserializeFromBinary(string filename)
  328.         {
  329.             object obj = null;
  330.  
  331.             using (Stream s = new FileStream(filename, FileMode.Open, FileAccess.Read))
  332.             {
  333.                 BinaryFormatter bf = new BinaryFormatter();
  334.                 obj = bf.Deserialize(s);
  335.             }
  336.  
  337.             return obj;
  338.         }
  339.  
  340.         public static void SerializeToBinary(string filename, object obj)
  341.         {
  342.             using (Stream s = new FileStream(filename, FileMode.Create))
  343.             {
  344.                 BinaryFormatter bf = new BinaryFormatter();
  345.                 bf.Serialize(s, obj);
  346.             }
  347.         }
  348.  
  349.         public static void TestBinarySearchTreeClass()
  350.         {
  351.             Comparison<int> cmp = Comparer<int>.Default.Compare;
  352.             BinarySearchTree<int> btree = new BinarySearchTree<int>(cmp);
  353.  
  354.             Console.WriteLine("Testing BinarySearchTree class Add method...");
  355.             btree.Add(8);
  356.             btree.Add(10);
  357.             btree.Add(14);
  358.             btree.Add(13);
  359.             btree.Add(3);
  360.             btree.Add(1);
  361.             btree.Add(6);
  362.             btree.Add(7);
  363.             btree.Add(4);
  364.             btree.Add(15);
  365.             btree.Add(16);
  366.  
  367.             Console.WriteLine("The constructed binary search tree:");
  368.             btree.TraverseInOrder(btree.Root);
  369.  
  370.             Console.WriteLine("Testing BinarySearchTree class Remove method (removing a leaf node)...");
  371.             btree.Remove(16);
  372.  
  373.             Console.WriteLine("The tree after removing node \"16\": ");
  374.             btree.TraverseInOrder(btree.Root);
  375.  
  376.             Console.WriteLine("Testing BinarySearchTree class Remove method (removing a node with the only child)...");
  377.             btree.Remove(14);
  378.  
  379.             Console.WriteLine("The tree after removing node \"14\": ");
  380.             btree.TraverseInOrder(btree.Root);
  381.  
  382.             Console.WriteLine("Testing BinarySearchTree class Remove method (removing a node with two children)...");
  383.             btree.Remove(3);
  384.  
  385.             Console.WriteLine("The tree after removing node \"3\": ");
  386.             btree.TraverseInOrder(btree.Root);
  387.  
  388.             Console.WriteLine("Testing BinarySearchTree class Remove method (removing the root)...");
  389.             btree.Remove(8);
  390.  
  391.             Console.WriteLine("The tree after removing node \"8\": ");
  392.             btree.TraverseInOrder(btree.Root);
  393.  
  394.             string filename = @"C:\Users\DEFAULTUSER\Desktop\SavedBST111.xml";
  395.  
  396.             Console.WriteLine("Testing BinarySearchTree class binary serialization...");
  397.  
  398.             SerializeToXml(filename, btree);
  399.  
  400.             Console.WriteLine("Testing BinarySearchTree class binary deserializtion...");
  401.             BinarySearchTree<int> btree2 = null;
  402.  
  403.             btree2 = (BinarySearchTree<int>)DeserializeFromXml(filename, typeof(BinarySearchTree<int>));
  404.  
  405.             Console.WriteLine("Deserialized tree: ");
  406.             btree2.TraverseInOrder(btree2.Root);
  407.         }
  408.  
  409.         static void Main(string[] args)
  410.         {
  411.             TestBinarySearchTreeClass();
  412.  
  413.             Console.ReadKey();
  414.         }
  415.     }
  416. }
Add Comment
Please, Sign In to add comment