Exemplo n.º 1
0
        static void CallTheTree()
        {
            BinaryTree <int> integerTree = new BinaryTree <int>();

            Random rand = new Random();

            for (int i = 0; i < 20; i++)
            {
                int value = rand.Next(100);
                Console.WriteLine("Adding {0}", value);
                integerTree.Add(value);
            }

            Console.WriteLine("Number of nodes is {0}", integerTree.Count);
            Console.WriteLine("Max value is {0}", integerTree.MaxValue);
            Console.WriteLine("Min value is {0}", integerTree.MinValue);
            Console.WriteLine("Preorder traversal:");
            Console.WriteLine(string.Join(" ", integerTree.Preorder()));
            Console.WriteLine("Inorder traversal:");
            Console.WriteLine(string.Join(" ", integerTree.Inorder()));
            Console.WriteLine("Postorder traversal:");
            Console.WriteLine(string.Join(" ", integerTree.Postorder()));
            Console.WriteLine("Levelorder traversal:");
            Console.WriteLine(string.Join(" ", integerTree.Levelorder()));
            Console.WriteLine("Default traversal (inorder):");
            foreach (int n in integerTree)
            {
                Console.Write("{0} ", n);
            }
            Console.WriteLine();
            Console.ReadKey(true);
        }
Exemplo n.º 2
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();
        }