Пример #1
0
 public void Add(int n)
 {
     if (data == 0)
     {
         data = n;
         return;
     }
     if (n > data)
     {
         if (right == null)
         {
             right = new BinaryTree()
             {
                 data = n
             };
             right.parent = this;
         }
         else
         {
             right.Add(n);
         }
     }
     else
     {
         if (left == null)
         {
             left = new BinaryTree()
             {
                 data = n
             };
             left.parent = this;
         }
         else
         {
             left.Add(n);
         }
     }
 }
Пример #2
0
        static void Main(string[] args)
        {
            BinaryTree <Student> tree = new BinaryTree <Student>();

            tree.Add(new Student("A", "A", new DateTime(10, 10, 10), 5));
            tree.Add(new Student("B", "A", new DateTime(11, 10, 10), 7));
            tree.Add(new Student("C", "A", new DateTime(12, 10, 10), 8));
            tree.Add(new Student("D", "A", new DateTime(13, 10, 10), 9));
            tree.Add(new Student("E", "A", new DateTime(14, 10, 10), 10));
            tree.Add(new Student("F", "A", new DateTime(15, 10, 10), 4));
            tree.Remove(new Student("A", "A", new DateTime(10, 10, 10), 5));
            Console.WriteLine();
            tree.Balance();
            tree.Serialize(@"D:\Тихон\Авы\Task5\BinaryTree\BinaryTree\bin\Debug\test.xml");
            BinaryTree <Student> newTree = new BinaryTree <Student>();
            bool res = newTree.Deserialize(@"D:\Тихон\Авы\Task5\BinaryTree\BinaryTree\bin\Debug\test.xml");

            Console.WriteLine(res);
            Console.ReadKey();
        }
Пример #3
0
        static void Main(string[] args)
        {
            MyList <int>    intList    = new MyList <int>();
            MyList <string> stringList = new MyList <string>();

            Console.WriteLine("CREATE LIST");

            intList.Add(1);
            intList.Add(2);
            intList.Add(3);
            intList.Add(4);
            intList.Add(5);

            Console.WriteLine("Add 1, 2, 3, 4, 5 to intList:");

            foreach (var item in intList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            intList.Remove(2);
            Console.WriteLine("Delete 2 from intList:");
            foreach (var item in intList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            bool f = intList.Contains(3);

            Console.WriteLine(f == true ? "List contain 3" : "List doesn't contain 3");

            Console.WriteLine("Add 0 first in intList:");
            intList.AppendFirst(0);
            foreach (var item in intList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            Console.WriteLine("Reverse 0 1 3 4 5:");
            foreach (var item in intList.Reverse())
            {
                Console.Write(item + " ");
            }

            Console.WriteLine();

            intList.Clear();
            Console.ReadKey();
            Console.Clear();


            Console.WriteLine("CREATE BINARY TREE:");


            var binaryTree = new BinaryTree <int>();

            binaryTree.Add(8);
            binaryTree.Add(3);
            binaryTree.Add(10);
            binaryTree.Add(1);
            binaryTree.Add(6);
            binaryTree.Add(4);
            binaryTree.Add(7);
            binaryTree.Add(14);
            binaryTree.Add(18);

            binaryTree.PrintTree();

            Console.WriteLine(new string('-', 40));
            Console.WriteLine("Delete 3 from tree:");
            binaryTree.Remove(3);
            binaryTree.PrintTree();

            Console.WriteLine(new string('-', 40));
            Console.WriteLine("Delete 8 from tree:");
            binaryTree.Remove(8);
            binaryTree.PrintTree();

            Console.WriteLine(new string('-', 40));

            if (binaryTree.FindNode(10) == null)
            {
                Console.WriteLine("DON'T FIND 10 in tree");
            }
            else
            {
                Console.WriteLine("FIND 10 in tree");
            }

            Console.ReadKey();
            Console.Clear();


            Console.WriteLine("SORT:");
            foreach (var item in MyInsertionSort.MySort(new[] { -5, 25, 25, 5, 0, -1, 0, -50 }))
            {
                Console.Write(item + " ");
            }

            Console.ReadKey();
        }
Пример #4
0
        static void Main(string[] args)
        {
            #region 数组
            //ArrayList num = new ArrayList();
            //num.Add("a");
            //num.Add("b");
            //num.Add("c");
            //foreach(var item in num)
            //{
            //    Console.WriteLine(num.IndexOf(item) + ":" + item);
            //}
            //Console.WriteLine(num.Capacity);
            //Console.WriteLine(num.Count);
            //num.Add("d");
            //num.Add("e");
            //num.Add("f");
            //foreach (var item in num)
            //{
            //    Console.WriteLine(num.IndexOf(item) + ":" + item);
            //}
            //Console.WriteLine(num.Capacity);
            //Console.WriteLine(num.Count);
            //num.Remove("f");
            //num.Remove("e");
            //num.Remove("d");
            //Console.WriteLine(num.Capacity);
            //Console.WriteLine(num.Count);
            #endregion

            #region 双向链表
            //LinkedList<string> linked = new LinkedList<string>();
            //linked.AddFirst("a");
            //linked.AddFirst("b");
            //linked.AddFirst("c");
            //linked.AddLast("d");

            //foreach(var item in linked)
            //{
            //    Console.WriteLine(item);
            //}

            //DoubleLink<int> dlink = new DoubleLink<int>();// 创建双向链表
            //Console.WriteLine("将 20 插入到表头之后");
            //dlink.Append(0, 10);
            //dlink.ShowAll();
            //Console.WriteLine("将 40 插入到表头之后");
            //dlink.Append(1, 30);
            //dlink.ShowAll();
            //Console.WriteLine("将 10 插入到表头之前");
            //dlink.Insert(0, 40);
            //dlink.ShowAll();
            //Console.WriteLine("将 30 插入到第一个位置之前");
            //dlink.Insert(1, 20);
            //dlink.ShowAll();

            //dlink.Insert(2, 25);
            //dlink.ShowAll();
            //Console.WriteLine("展示第一个:" + dlink.GetFirst());
            //Console.WriteLine("删除第一个");
            //dlink.DelFirst();
            //Console.WriteLine("展示第一个:" + dlink.GetFirst());
            //Console.WriteLine("展示最后一个:" + dlink.GetLast());
            //Console.WriteLine("删除最后一个");
            //dlink.DelLast();
            //Console.WriteLine("展示最后一个:" + dlink.GetLast());
            //dlink.ShowAll();
            //Console.ReadKey();
            #endregion

            #region 二叉查询树
            BinaryTree binaryTree = new BinaryTree();
            //for (var i = 0; i < 5; i++)
            //{
            //    Random random = new Random();
            //    int num = random.Next(0, 100);
            //    binaryTree.Add(num);
            //}
            binaryTree.Add(15);
            binaryTree.Add(20);
            binaryTree.Add(10);
            binaryTree.Add(5);
            binaryTree.Add(18);
            binaryTree.Add(6);
            binaryTree.Add(19);
            binaryTree.Add(17);
            binaryTree.Add(4);
            binaryTree.Add(12);
            //binaryTree.Add(11);
            binaryTree.Add(13);
            binaryTree.Preorder();


            Tree tree = binaryTree.Search(4);
            //if (tree != null)
            //{
            //    Console.WriteLine(tree.Data);
            //}
            //binaryTree.Delete(4);
            //binaryTree.Delete(12);
            binaryTree.Delete(20);
            #endregion

            #region Hash一致性
            //int Replicas = 100;
            //KetamaNodeLocator.AddNode("127.0.0.1:6379", Replicas);
            //KetamaNodeLocator.AddNode("127.0.0.1:6380", Replicas);
            //KetamaNodeLocator.AddNode("127.0.0.1:6381", Replicas);
            //List<string> nodes = new List<string>();
            //for (int i = 0; i < 100; i++)
            //{
            //    nodes.Add(KetamaNodeLocator.GetTargetNode(i + "test" + (char)i));
            //}
            //var counts = nodes.GroupBy(n => n, n => n.Count()).ToList();
            //counts.ForEach(index => Console.WriteLine(index.Key + "-" + index.Count()));

            //Console.ReadLine();
            #endregion

            //Cat.sid = 100;
            //Cat mimi = new Cat("mimi");
            //Cat pipi = new Cat("pipi");
            //bool bol = true;
            //Int32 i = new Int32();
            //int j = 1;
            //int k = bol ? i : j;
            //Console.WriteLine(k);

            Console.ReadLine();
        }