ITeye博客

数据结构C#实现-二叉查找树的创建,查找,以及各种递归(非递归)遍历算法

bbls · 2009-07-09 09:24 · Markdown

关键字:
        二叉树,查找,(breadth-first algrithm) ---前序遍历,中序遍历,后序遍历,(depth-first algrithm)-层次遍历。
摘要:
        树是一种非常重要的数据结构,Binary Tree则是树型结构中应用最为广泛。本文给出了C#版本的二叉树的建立,查找,以及各种递归和非递归的遍历算法。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DS
{
    public class Tree
    {
        public Tree(int data)
        {
            _data = data;
            leftChild = null;
            rightChild = null;
        }
        private int _data;
        private Tree leftChild;
        private Tree rightChild;

     
       
        public void PreOrder()
        {
            
            if(this != null)
            {
                Console.Write("{0} ", _data);
                if(leftChild!=null)
                  leftChild.PreOrder();
                if(rightChild!=null)
                 rightChild.PreOrder();
            }
        }

        public void InOrder()
        {

            if (this != null)
            {
                if (leftChild != null)
                    leftChild.InOrder();
                
                Console.Write("{0} ", _data);
                           
                if (rightChild != null)
                    rightChild.InOrder();
            }
        }

        public void PostOrder()
        {
            if (this != null)
            {
                if (leftChild != null)
                    leftChild.PostOrder();
                if (rightChild != null)
                    rightChild.PostOrder();
                Console.Write("{0} ", _data);
            }
        }


        public void PostOrderWithoutIretation()
        {
            Stack<Tree> stack = new Stack<Tree>();
            Tree pre=null, temp;
            temp=this;
            while (temp != null || stack.Count != 0)
            {
                while (temp != null)
                {
                    stack.Push(temp);
                    temp = temp.leftChild;
                }

                temp = stack.First<Tree>();
                if (temp.rightChild == null || temp.rightChild==pre)
                {                   
                    temp=stack.Pop();
                    Console.Write("{0} ", temp._data);
                    pre = temp;
                    temp = null;
                }
                else
                {
                    temp = temp.rightChild;
                }

            }
           
        }
        public void InOrderWithoutIretation()
        {
            Stack<Tree> stack = new Stack<Tree>();
            Tree  temp;
            temp = this;
           while (temp!=null || stack.Count != 0)
            {              
                while (temp != null)
                {
                    stack.Push(temp);
                    temp = temp.leftChild;                    
                }

                temp = stack.Pop();
                         
                Console.Write(" {0} ", temp._data);
                temp = temp.rightChild;
            }
        }

        public void LevelOrder()
        {
            Queue<Tree> queue = new Queue<Tree>();
            queue.Enqueue(this);
            Tree  temp;
            while (queue.Count != 0)
            {
                temp=queue.Dequeue();
                Console.Write("{0} ", temp._data);

                if(temp.leftChild!=null)
                    queue.Enqueue(temp.leftChild);
                if(temp.rightChild!=null)
                    queue.Enqueue(temp.rightChild);
               
            }

        }

        public void Search(int data, out Tree p, out Tree q)
        {
            p = this;
            q = null;
            while (p != null)
            {
                q = p;
                if (p._data>data)
                    p = p.leftChild;
                else if (p._data < data)
                    p = p.rightChild;
                else
                    break;
            }               
        }


        public bool InsertNode(int data)
        {
            Tree newNode = new Tree(data);
            Tree p,q;
            Search(data, out p, out q);
            if (p!= null)
                return true;
            else
            {
                if (q._data >data)
                    q.leftChild =newNode;
                else
                    q.rightChild = newNode ;
            }
            return false;
        }
        public void PreOrderWithouIreration()
        {
            Tree temp;
            temp = this;
            Stack<Tree> stack=new Stack<Tree>();
            stack.Push(this);
            while (stack.Count != 0)
            {
                temp = stack.Pop();
                Console.Write("{0} ", temp._data);
                if (temp.rightChild!=null)
                    stack.Push(temp.rightChild);
                if (temp.leftChild!=null)
                    stack.Push(temp.leftChild);
            }

        }
    }

   

    class Program
    {
        static void Main(string[] args)
        {
            Tree Root = new Tree(10);
        
          Root.InsertNode(5);
           Root.InsertNode(14);
           Root.InsertNode(9);
           Root.InsertNode(13);
           Root.InsertNode(8);
            Console.WriteLine("pre order");
            Root.PreOrder();
            Console.WriteLine("pre order without iteration\n");
            Root.PreOrderWithouIreration();
            
            Console.WriteLine("\nIn order");
            Root.InOrder();
            Console.WriteLine("\nInOrder without Iteration");
            Root.InOrderWithoutIretation();

            Console.WriteLine("\nPost Order:");
            Root.PostOrder();
            Console.WriteLine("\nPost Order without Iteration");
            Root.PostOrderWithoutIretation();


            Console.WriteLine("\nlevel order");
            Root.LevelOrder();
            Console.Read();
          
        }
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。