static void Main(string[] args)
        {
            BSTree myTree = new BSTree();

            myTree.AddNode(5);
            myTree.AddNode(2);
            myTree.AddNode(9);
            myTree.AddNode(7);
            myTree.AddNode(1);
            myTree.AddNode(8);
            myTree.AddNode(10);
            Console.WriteLine(myTree.Search(7));
            Console.WriteLine(myTree.Search(4));
            Console.ReadLine();
        }
示例#2
0
        public static void Main1(string[] args)
        {
            BSTree <int> BSTree = new BSTree <int>(45);

            BSTree.Insert(24);
            BSTree.Insert(55);
            BSTree.Insert(12);
            BSTree.Insert(37);
            BSTree.Insert(53);
            BSTree.Insert(60);
            BSTree.Insert(28);
            BSTree.Insert(40);
            BSTree.Insert(70);
            //BSTree.Delete(40);

            Console.Write("先序序列:");
            BSTree.PreOrder();
            Console.WriteLine();

            Console.Write("非递归先序序列:");
            BSTree.PreOrder2();
            Console.WriteLine();

            Console.Write("中序序列:");
            BSTree.InOrder();
            Console.WriteLine();

            Console.Write("非递归中序序列:");
            BSTree.InOrder2();
            Console.WriteLine();

            Console.Write("后序序列:");
            BSTree.PostOrder();
            Console.WriteLine();

            Console.Write("非递归后序序列:");
            BSTree.PostOrder2();
            Console.WriteLine();

            Console.Write("层次序列:");
            BSTree.LevelOrder();
            Console.WriteLine();
        }
示例#3
0
        // 层次遍历
        public void LevelOrder()
        {
            Queue <BSTree <T> > queue = new Queue <BSTree <T> >();
            BSTree <T>          p     = null;

            queue.Enqueue(this);
            while (queue.Count != 0)
            {
                p = queue.Dequeue();
                Console.Write(p.data + " ");
                if (p.lchild != null)
                {
                    queue.Enqueue(p.lchild);
                }
                if (p.rchild != null)
                {
                    queue.Enqueue(p.rchild);
                }
            }
        }
示例#4
0
 // 增加结点
 public void Insert(T data)
 {
     if (this.data.CompareTo(data) >= 0)
     {
         if (this.lchild == null)
         {
             this.lchild = new BSTree <T>(data);
             return;
         }
         this.lchild.Insert(data);
     }
     else
     {
         if (this.rchild == null)
         {
             this.rchild = new BSTree <T>(data);
             return;
         }
         this.rchild.Insert(data);
     }
 }
示例#5
0
        // 非递归先序遍历
        public void PreOrder2()
        {
            Stack <BSTree <T> > stack = new Stack <BSTree <T> >();
            // 初始化current为当前根结点
            BSTree <T> current = this;

            // current != null是为了保证先将根结点和左孩子结点入栈
            // stack.Count != 0是为了保证依次出栈根结点和左孩子结点,并寻找它们的右兄弟
            while (current != null || stack.Count != 0)
            {
                // 先输出根结点和根节点的左孩子结点
                while (current != null)
                {
                    Console.Write(current.data + " ");
                    stack.Push(current);
                    current = current.lchild;
                }
                // 依次寻找左孩子结点的右兄弟
                current = stack.Pop();
                current = current.rchild;
            }
        }
示例#6
0
        // 非递归中序遍历
        public void InOrder2()
        {
            Stack <BSTree <T> > stack = new Stack <BSTree <T> >();
            // 初始化current为当前根结点
            BSTree <T> current = this;

            // 和非递归先序遍历类似,都是先将根结点和根节点的左孩子结点入栈,只不过输出时左孩子先输出。
            while (current != null || stack.Count != 0)
            {
                // 先将根结点和根节点的左孩子结点入栈
                while (current != null)
                {
                    stack.Push(current);
                    current = current.lchild;
                }

                // 出栈输出时先输出左孩子再输出左孩子的父结点
                current = stack.Pop();
                Console.Write(current.data + " ");
                // 寻找右孩子结点
                current = current.rchild;
            }
        }
示例#7
0
 public BSTree(T data)
 {
     this.data   = data;
     this.lchild = null;
     this.rchild = null;
 }