Using System;
Using System. Collections. Generic;
Using System. Collections;
Class Program
{
Static void Main (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 ("first traverse result set :");
Node <string>. PreOrder ();
Console. WriteLine ("sequential traversal result set :");
Node <string>. InOrder ();
Console. WriteLine ("result set of post-order traversal :");
Node <string>. PostnOrder ();
Console. WriteLine ("sequence traversal result set :");
Node <string>. LevelOrder ();
}
}
// Create a binary tree generic class
Public class Node <T>
{
Private T data; // data domain
Private Node <T> lChild; // left child
Private Node <T> rChild; // right child
// Constructor
Public Node (T val, Node <T> lp, Node <T> rp)
{
Data = val;
LChild = lp;
RChild = rp;
}
// Constructor
Public Node (Node <T> lp, Node <T> rp)
{
Data = default (T );
LChild = lp;
RChild = rp;
} // Constructor
Public Node (T val)
{
Data = val;
LChild = null;
RChild = null;
}
// Constructor
Public Node ()
{
Data = default (T );
LChild = null;
RChild = null;
}
// Data attributes
Public T Data
{
Get
{
Return data;
}
Set
{
Value = data;
}
}
// Left child attributes
Public Node <T> LChild
{
Get
{
Return lChild;
}
Set
{LChild = value;
}
}
// Right child attributes
Public Node <T> RChild
{
Get
{
Return rChild;
}
Set
{
RChild = value;
}
}
Public static void PreOrder (Node <T> root)
{
// The root node is empty.
If (root = null)
{
Return;
}
// Process the root node
Console. WriteLine ("{0}", root. Data );
// First traverse the left subtree
PreOrder (root. lChild );
// First traverse the right subtree
PreOrder (root. rChild );
}
Public static void InOrder (Node <T> root)
{
// The root node is empty.
If (root = null)
{
Return;
}
// Traverse the left subtree in the middle order
InOrder (root. LChild );
Console. WriteLine ("{0}", root. data );
// Traverse the right subtree in the middle order
InOrder (root. rChild );
}
Public static void PostnOrder (Node <T> root)
{
// The root node is empty.
If (root = null)
{
Return;
}
// Traverse the left subtree in descending order
PostnOrder (root. LChild );
// Traverse the right subtree in descending order
PostnOrder (root. rChild );
Console. WriteLine ("{0}", root. data );
}
Public static void LevelOrder (Node <T> root)
{
// The root node is empty.
If (root = null)
{
Return;
}
// Set a node for storing sequence traversal in a queue
CSeqQueue <Node <T> sq = new CSeqQueue <Node <T> (50 );
// Enter the root node
Sq. In (root );
// The queue is not empty and the node is not processed
While (! Sq. IsEmpty ())
{
// Node team-up
Node <T> tmp = sq. Out ();
// Process the current node
Console. WriteLine ("{0}", tmp. data );
// Enter the left child node of the current node
If (tmp. LChild! = Null)
{
Sq. In (tmp. LChild );
}
// Add the right child node of the current node to the queue
If (tmp. RChild! = Null)
{
Sq. In (tmp. RChild );
}
}
}
}
// Queue Interface
Public interface IQueue <T>
{
Int GetLength (); // evaluate the queue length
Bool IsEmpty (); // determines whether the column is empty.
Void Clear (); // Clear the queue
Void In (T item); // enter the team
T Out (); // leaves the team
T GetFront (); // get the first element
}
// Queue generic instance
Public class CSeqQueue <T>: IQueue <T> {
Private int maxsize; // The capacity of the cyclic sequence queue.
Private T [] data; // array, used to store data elements in the cyclic sequence queue
Private int front; // indicates the head of the cyclic ordered queue.
Private int rear; // indicates the end of the cyclic order queue.
// Indexer
Public T this [int index]
{
Get
{
Return data [index];
}
Set
{
Data [index] = value;
}
}
// Capacity attribute
Public int Maxsize
{
Get
{
Return maxsize;
}
Set
{
Maxsize = value;
}
}
// Queue header attributes
Public int Front
{
Get
{
Return front;
}
Set
{
Front = value;
}
}
// Team-end attributes
Public int Rear
{
Get
{
Return rear;
}
Set
{
Rear = value;
}
}
// Constructor
Public CSeqQueue (int size)
{
Data = new T [size];
Maxsize = size;
Front = rear =-1;
}
// Calculate the length of the cyclic ordered queue
Public int GetLength ()
{
Return (rear-front + maxsize) % maxsize;
}
// Clear the cyclic sequence queue
Public void Clear ()
{
Front = rear =-1;
}
// Determine whether the cyclic sequence queue is empty
Public bool IsEmpty ()
{
If (front = rear)
{
Return true;
}
Else
{
Return false;
}
}
// Determine whether the cyclic sequence queue is full
Public bool IsFull ()
{
If (rear + 1) % maxsize = front)
{
Return true;
}
Else
{
Return false;
}
}
// Enter the team
Public void In (T item)
{
If (IsFull ())
{
Console. WriteLine ("Queue is full ");
Return;
}
Data [++ rear] = item;
}
// Team out
Public T Out ()
{
T tmp = default (T );
If (IsEmpty ())
{
Console. WriteLine ("Queue is empty ");
Return tmp;
}
Tmp = data [++ front];
Return tmp;
}
// Obtain the data element of the queue Header
Public T GetFront ()
{
If (IsEmpty ())
{
Console. WriteLine ("Queue is empty! ");
Return default (T );
}
Return data [front + 1];
}
}