Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using Unit4.CollectionsLib;
- namespace ConsoleApplication1
- {
- class Program
- {
- static Random Num = new Random();
- static BinTreeNode<int> BuildT(int x)
- {
- int a;
- if (x < 0) return null;
- if (x==0) return new BinTreeNode <int>(Num .Next (1,21));
- if (x > 0)
- {
- a = Num.Next(1, 4);
- if (a == 1)
- return new BinTreeNode<int>(null, Num.Next(1, 21), BuildT(x - 1));
- if (a == 2)
- return new BinTreeNode<int>(BuildT(x - 1), Num.Next(1, 21), null);
- if (a == 3)
- return new BinTreeNode<int>(BuildT(x - 1), Num.Next(1, 21), BuildT(x - 1));
- }
- return null;
- }
- static int Count(BinTreeNode<int> t)
- {
- if(t == null)
- return 0;
- return 1 + Count(t.GetLeft()) + Count(t.GetRight());
- }
- static int Count2(BinTreeNode<int> t)
- {
- if(t == null)
- return 0;
- if(t.GetRight() == t.GetLeft())
- return 1;
- return Count2(t.GetLeft()) + Count2(t.GetRight());
- }
- static int CountRight(BinTreeNode<int> t)
- {
- if(t == null)
- return 0;
- if(t.GetRight() != null)
- return 1 + CountRight(t.GetRight()) + CountRight(t.GetLeft());
- return CountRight(t.GetRight()) + CountRight(t.GetLeft());
- }
- static int CountLeft(BinTreeNode<int> t)
- {
- if(t == null || t.GetLeft() == t.GetRight())
- return 0;
- if(t.GetLeft() != null && t.GetRight() == null)
- return 1 + CountLeft(t.GetLeft());
- return CountLeft(t.GetLeft()) + CountLeft(t.GetRight());
- }
- static int Sum(BinTreeNode<int> t)
- {
- if(t == null);
- return t.GetLeft().GetInfo() + t.GetRight().GetInfo() + Sum(t);
- }
- static int Sum2(BinTreeNode<int> t)
- {
- if(t == null)
- return 0;
- if(t.GetRight() == t.GetLeft())
- return t.GetInfo();
- return Sum2(t.GetLeft()) + Sum2(t.GetRight());
- }
- static int Count3(BinTreeNode<int> t, int x)
- {
- if(t == null)
- return 0;
- if(x == 0)
- {
- return Count3(t
- }
- }
- static void Main(string[] args)
- {
- BinTreeNode<int> p = BuildT(3);
- BinTreeUtils.ShowTree(p);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment