C#資料結構學習之一:資料結構基礎知識

來源:互聯網
上載者:User

using System;

using System.Collections.Generic;

using System.Collections;

 

   class Program

    {

        static voidMain(string[] args)

        {

 

            Node<string> H = new Node<string>("H",null,null);

            Node<string> I = new Node<string>("I",null,null);

            Node<string> J = new Node<string>("J",null,null);

            Node<string> D = new Node<string>("D",H,I);

            Node<string> E = new Node<string>("E", J,null);

            Node<string> B = new Node<string>("B", D, E);

            Node<string> F = new Node<string>("F", null, null);

            Node<string> G = new Node<string>("G", null, null);

            Node<string> C = new Node<string>("C", F, G);

            Node<string> A = new Node<string>("A", B, C);

            Console.WriteLine("先序遍曆結果集:");

            Node<string>.PreOrder(A);

            Console.WriteLine("中序遍曆結果集:");

            Node<string>.InOrder(A);

            Console.WriteLine("後序遍曆結果集:");

            Node<string>.PostnOrder(A);

            Console.WriteLine("層序遍曆結果集:");

            Node<string>.LevelOrder(A);

 

        }

    }

 

//建立二叉樹的泛型類

public class Node<T>

    {

      private T data;             //資料域

      private Node<T> lChild;     //左孩子

      private Node<T> rChild;     //右孩子

      //構造器

      public Node(T val, Node<T> lp, Node<T> rp)

      {

      data = val;

      lChild = lp;

      rChild = rp;

      }

      //構造器

      public Node(Node<T> lp, Node<T> rp)

      {

    data = default(T);

       lChild = lp;

       rChild = rp;

     } //構造器

public Node(T val)

    {

    data = val;

    lChild = null;

    rChild = null;

     }

 

//構造器

public Node()

    {

    data = default(T);

    lChild = null;

    rChild = null;

    }

 //資料屬性

public T Data

    {

    get

    {

        return data;

    }

    set

    {

        value = data;

    }

    }

 

//左孩子屬性

public Node<T> LChild

    {

    get

        {

            return lChild;

         }

    set

        {     lChild = value;

        }

    }

 

//右孩子屬性

    public Node<T> RChild

    {

    get

        {

            return rChild;

        }

    set

    {

        rChild = value;

    }

    }

    public static void PreOrder(Node<T> root)

    {

        //根結點為空白

        if (root == null)

        {

            return;

        }

        //處理根結點

        Console.WriteLine("{0}", root.Data);

        //先序遍曆左子樹

        PreOrder(root.lChild);

        //先序遍曆右子樹

        PreOrder(root.rChild);

    }

 

 

    public static void InOrder(Node<T> root)

    {

    //根結點為空白

        if (root==null)

        {

            return;

        }

        //中序遍曆左子樹

        InOrder(root.LChild);

        Console.WriteLine("{0}", root.data);

        //中序遍曆右子樹

        InOrder(root.rChild);

    }

    public static void PostnOrder(Node<T> root)

    {

        //根結點為空白

        if (root == null)

        {

            return;

        }

        //後序遍曆左子樹

        PostnOrder(root.LChild);

 

        //後序遍曆右子樹

        PostnOrder(root.rChild);

        Console.WriteLine("{0}", root.data);

    }

 

    public static void LevelOrder(Node<T> root)

    {

        //根結點為空白

        if (root == null)

        {

            return;

        }

 

        //設定一個隊列儲存層序遍曆的結點

        CSeqQueue<Node<T>> sq = new CSeqQueue<Node<T>>(50);

 

        //根結點入隊

        sq.In(root);

        //隊列非空,結點沒有處理完

        while (!sq.IsEmpty())

        {

            //結點出隊

            Node<T> tmp = sq.Out();

            //處理當前結點

            Console.WriteLine("{0}", tmp.data);

    //將當前結點的左孩子結點入隊

     if (tmp.LChild != null)

     {

         sq.In(tmp.LChild);

     }

     //將當前結點的右孩子結點入隊

     if (tmp.RChild != null)

     {

         sq.In(tmp.RChild);

     }

 }

}

}

 

 

//Queue介面

public interface IQueue<T>

{

        int GetLength();          //求隊列的長度

        bool IsEmpty();           //判斷對列是否為空白

        void Clear();             //清空隊列

        void In(T item);          //入隊

        T Out();                  //出隊

        T GetFront();             //取對頭元素

    }

 

//Queue泛型執行個體

public class CSeqQueue<T> : IQueue<T> {

      private int maxsize;       //迴圈順序隊列的容量

      private T[] data;          //數組, 用於儲存迴圈順序隊列中的資料元素

      private int front;          //指示迴圈順序隊列的隊頭

      private int rear;           //指示迴圈順序隊列的隊尾

      //索引器

      public T this[int index]

      {

          get 

          {

              return data[index];

         }

          set 

          {

              data[index] = value;

          }

      }

      //容量屬性

      public int Maxsize

      {

          get

          {

             return maxsize;

            }

            set

            {

                maxsize = value;

            }

}

 //隊頭屬性

public int Front

     {

         get

         {

            return front;

            }

        set

        {

            front = value;

        }

}

 

//隊尾屬性

public int Rear

     {

         get

         {

            return rear;

            }

        set

        {

                rear = value;

        }

       }

     //構造器

     public CSeqQueue(int size)

     { 

         data = new T[size];

         maxsize = size;

         front = rear = -1;

     }

     //求迴圈順序隊列的長度

     public int GetLength()

     {

                 return (rear-front+maxsize) % maxsize;

}

    //清空迴圈順序隊列

    public void Clear()

    {

        front = rear = -1;

    }

    //判斷迴圈順序隊列是否為空白

    public bool IsEmpty()

    {

        if (front == rear)

        {

            return true;

        }

        else 

        {

            return false;

        }

    }

    //判斷迴圈順序隊列是否為滿

    public bool IsFull()

              {

          if ((rear + 1) % maxsize==front)

          {

              return true;

          }

          else

          {

                           return false;

        }

}

     //入隊

     public void In(T item)

     {

         if(IsFull())

         {

             Console.WriteLine("Queue is full");

             return;

        }

        data[++rear] = item;

}

     //出隊

     public T Out()

     {

         T tmp = default(T);

         if (IsEmpty())

         {

             Console.WriteLine("Queue is empty");

             return tmp;

         }

         tmp = data[++front];

         return tmp;

     }

     //擷取隊頭資料元素

     public T GetFront()

     {

         if (IsEmpty())

         {

             Console.WriteLine("Queue is empty!");

             return default(T);

         }

         return data[front + 1];

     }

 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.