AvengersAssemble

Untitled

Dec 24th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 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.         static Random Num = new Random();
  11.  
  12.         static BinTreeNode<int>  BuildT(int x)
  13.         {
  14.             int a;
  15.             if (x < 0) return null;
  16.             if (x==0)  return new BinTreeNode <int>(Num .Next (1,21));
  17.             if (x > 0)
  18.             {
  19.                 a = Num.Next(1, 4);
  20.                 if (a == 1)
  21.                     return new BinTreeNode<int>(null, Num.Next(1, 21), BuildT(x - 1));
  22.                 if (a == 2)
  23.                     return new BinTreeNode<int>(BuildT(x - 1), Num.Next(1, 21), null);
  24.                 if (a == 3)
  25.                     return new BinTreeNode<int>(BuildT(x - 1), Num.Next(1, 21), BuildT(x - 1));
  26.             }
  27.             return null;
  28.         }
  29.  
  30.         static int Count(BinTreeNode<int> t)
  31.         {
  32.             if(t == null)
  33.                 return 0;
  34.             return 1 + Count(t.GetLeft()) + Count(t.GetRight());
  35.         }
  36.        
  37.         static int Count2(BinTreeNode<int> t)
  38.         {
  39.             if(t == null)
  40.                 return 0;
  41.             if(t.GetRight() == t.GetLeft())
  42.                 return 1;
  43.             return Count2(t.GetLeft()) + Count2(t.GetRight());
  44.         }
  45.  
  46.         static int CountRight(BinTreeNode<int> t)
  47.         {
  48.             if(t == null)
  49.                 return 0;
  50.             if(t.GetRight() != null)
  51.                 return 1 + CountRight(t.GetRight()) + CountRight(t.GetLeft());
  52.             return CountRight(t.GetRight()) + CountRight(t.GetLeft());
  53.         }
  54.  
  55.        
  56.         static int CountLeft(BinTreeNode<int> t)
  57.         {
  58.             if(t == null || t.GetLeft() == t.GetRight())
  59.                 return 0;
  60.             if(t.GetLeft() != null && t.GetRight() == null)
  61.                 return 1 + CountLeft(t.GetLeft());
  62.             return CountLeft(t.GetLeft()) + CountLeft(t.GetRight());
  63.         }
  64.  
  65.         static int Sum(BinTreeNode<int> t)
  66.         {
  67.             if(t == null);
  68.             return t.GetLeft().GetInfo() + t.GetRight().GetInfo() + Sum(t);
  69.         }
  70.  
  71.         static int Sum2(BinTreeNode<int> t)
  72.         {
  73.             if(t == null)
  74.                 return 0;
  75.             if(t.GetRight() == t.GetLeft())
  76.                 return t.GetInfo();
  77.             return Sum2(t.GetLeft()) + Sum2(t.GetRight());
  78.         }
  79.  
  80.         static int Count3(BinTreeNode<int> t, int x)
  81.         {
  82.             if(t == null)
  83.                 return 0;
  84.             if(x == 0)
  85.             {
  86.                 return Count3(t
  87.             }
  88.         }
  89.         static void Main(string[] args)
  90.         {
  91.             BinTreeNode<int> p = BuildT(3);
  92.             BinTreeUtils.ShowTree(p);
  93.  
  94.         }
  95.     }
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment