Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. static void Main(string[] args)
  2.     {
  3.         BTree btr = new BTree();
  4.         btr.Add(6);
  5.         btr.Add(2);
  6.         btr.Add(3);
  7.         btr.Add(11);
  8.         btr.Add(30);
  9.         btr.Add(9);
  10.         btr.Add(13);
  11.         btr.Add(18);
  12.  
  13.         btr.Print();
  14.  
  15.     }
  16.  
  17.     public class BNode
  18.     {
  19.         public int item;
  20.         public BNode right;
  21.         public BNode left;
  22.  
  23.         public BNode(int item)
  24.         {
  25.             this.item = item;
  26.         }
  27.  
  28.         public void PrintPretty(string indent, bool last)
  29.         {
  30.  
  31.             Console.Write(indent);
  32.             if (last)
  33.             {
  34.                 Console.Write("└─");
  35.                 indent += "  ";
  36.             }
  37.             else
  38.             {
  39.                 Console.Write("├─");
  40.                 indent += "| ";
  41.             }
  42.             Console.WriteLine(item);
  43.  
  44.             var children = new List<BNode>();
  45.             if (this.left != null)
  46.                 children.Add(this.left);
  47.             if (this.right != null)
  48.                 children.Add(this.right);
  49.  
  50.             for (int i = 0; i < children.Count; i++)
  51.                 children[i].PrintPretty(indent, i == children.Count - 1);
  52.  
  53.         }
  54.  
  55.     }
  56.  
  57.     public class BTree
  58.     {
  59.         private BNode _root;
  60.         private int _count;
  61.         private IComparer<int> _comparer = Comparer<int>.Default;
  62.  
  63.  
  64.         public BTree()
  65.         {
  66.             _root = null;
  67.             _count = 0;
  68.         }
  69.  
  70.  
  71.         public bool Add(int Item)
  72.         {
  73.             if (_root == null)
  74.             {
  75.                 _root = new BNode(Item);
  76.                 _count++;
  77.                 return true;
  78.             }
  79.             else
  80.             {
  81.                 return Add_Sub(_root, Item);
  82.             }
  83.         }
  84.  
  85.         private bool Add_Sub(BNode Node, int Item)
  86.         {
  87.             if (_comparer.Compare(Node.item, Item) < 0)
  88.             {
  89.                 if (Node.right == null)
  90.                 {
  91.                     Node.right = new BNode(Item);
  92.                     _count++;
  93.                     return true;
  94.                 }
  95.                 else
  96.                 {
  97.                     return Add_Sub(Node.right, Item);
  98.                 }
  99.             }
  100.             else if (_comparer.Compare(Node.item, Item) > 0)
  101.             {
  102.                 if (Node.left == null)
  103.                 {
  104.                     Node.left = new BNode(Item);
  105.                     _count++;
  106.                     return true;
  107.                 }
  108.                 else
  109.                 {
  110.                     return Add_Sub(Node.left, Item);
  111.                 }
  112.             }
  113.             else
  114.             {
  115.                 return false;
  116.             }
  117.         }
  118.  
  119.         public void Print()
  120.         {
  121.             _root.PrintPretty("", true);
  122.         }
  123.  
  124.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement