Basic Algorithms of Binary Trees

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace BitTree
{
// TODO: Print Binary Tree

Class Constant
{
Public static readonly string Space = "";
}

Class Program
{
Static void Main (string [] args)
{
// "" Indicates the leaf node

String preorder = "abc de g f ";
// String preorder = "abdh I EJ K CFL G ";

Console. WriteLine ("Tree node: {0}", preorder );
BitTree bt = new BitTree (preorder );
Node root = bt. CreateBitTreePreOrder ();

Int width = bt. GetTreeWidth (root );

Console. WriteLine ("tree width: {0}", width );

// Tree height
Int depth = bt. GetTreeDepth (root );
Console. WriteLine ("tree height recursive algorithm: {0}", depth );

Depth = bt. GetTreeDepthNoneRecursion (root );
Console. WriteLine ("tree height non-recursive algorithm: {0}", depth );

// Preface
Console. WriteLine ("first-order recursive traversal :");
Bt. PreTraverse (root );
Console. WriteLine ();
Console. WriteLine ("sequential non-recursive traversal :");
Bt. PreTraverseNoneRecursion (root );
Console. WriteLine ();

// Middle Order
Console. WriteLine ("sequential recursive traversal :");
Bt. MidTraverse (root );
Console. WriteLine ();
Console. WriteLine ("ordinal non-recursive traversal :");
Bt. MidTraverseNoneRecursion (root );
Console. WriteLine ();

// Post-order
Console. WriteLine ("recursive post-order traversal :");
Bt. PostTraverse (root );
Console. WriteLine ();

Console. WriteLine ("post-Order Non-recursive traversal :");
Bt. PostTraverseNoneRecursion (root );
Console. WriteLine ();

// Hierarchical Traversal
Console. WriteLine ("layered traversal :");
Bt. TopToBottomReserver (root );
Console. ReadLine ();
}
}

Internal class Node
{
Internal Node Left {get; set ;}
Internal Node Right {get; set ;}
Internal string Value {get; set ;}
}

Class BitTree
{
String inputSequence;

Public BitTree (string input)
{
InputSequence = input;
}

# Region first-order algorithm
// Create a binary tree in sequence
Public Node CreateBitTreePreOrder ()
{
Return CreateBitTreePreOrder (null );
}

Private Node CreateBitTreePreOrder (Node node)
{
If (string. IsNullOrEmpty (inputSequence ))
{
Return null;
}

String value = inputSequence. Substring (0, 1 );
InputSequence = inputSequence. Substring (1 );

If (string. IsNullOrEmpty (value) | value = Constant. Space)
{
Return null;
}

Node = new Node ();
Node. Value = value;
Node. Left = CreateBitTreePreOrder (node. Left );
Node. Right = CreateBitTreePreOrder (node. Right );

Return node;
}

// Traverse Binary Trees in sequence and recursively
Public void PreTraverse (Node node)
{
If (node! = Null)
{
Console. Write (node. Value );
PreTraverse (node. Left );
PreTraverse (node. Right );
}
}

// Traverse the binary tree in sequence, non-recursion
Public void PreTraverseNoneRecursion (Node node)
{
Stack <Node> stack = new Stack <Node> ();

While (node! = Null | stack. Count> 0) // while is very good
{
If (node! = Null) // simultaneous access to the pressure Stack
{
Console. Write (node. Value );
Stack. Push (node );
Node = node. Left;
}
Else // process the right side
{
Node = stack. Pop ();
Node = node. Right;
}
}
}

# Endregion

# Region sequential Traversal Algorithm

// A binary tree cannot be uniquely identified in the middle order.

// Traverses binary trees in the middle order and Recursion
Public void MidTraverse (Node node)
{
If (node! = Null)
{
MidTraverse (node. Left );
Console. Write (node. Value );
MidTraverse (node. Right );
}
}

// Traverses binary trees in the middle order, non-recursion
Public void MidTraverseNoneRecursion (Node node)
{
Stack <Node> stack = new Stack <Node> ();

While (node! = Null | stack. Count> 0) // while is very good
{
If (node! = Null) // press the stack
{
Stack. Push (node );
Node = node. Left;
}
Else // access after pop-up
{
Node = stack. Pop ();
Console. Write (node. Value );
Node = node. Right;
}
}
}

# Endregion

# Region post-order traversal algorithm

// Traverse Binary Trees in descending order and Recursion
Public void PostTraverse (Node node)
{
If (node! = Null)
{
PostTraverse (node. Left );
PostTraverse (node. Right );
Console. Write (node. Value );
}
}

// Post-order traversal of Binary Trees, non-recursion
Public void PostTraverseNoneRecursion (Node node)
{
Stack <Node> stack = new Stack <Node> ();

Node lastvisit = node; // The Last accessed Node

While (node! = Null | stack. Count> 0)
{
If (node! = Null)
{
Stack. Push (node );
Node = node. Left;
}
Else
{
Node p = stack. Peek (); // cannot be obtained

// | (P. Right = lastvisit) // if the Right side is empty or has been accessed, it is returned to the parent node.
If (p. Right = null | p. Right = lastvisit)
{
Stack. Pop ();
Console. Write (p. Value );
Lastvisit = p;
}
Else // non-leaf node, which has not been accessed on the right and accessed on the right
{
Node = p. Right;
}
}
}
}

# Endregion

# Region layered Traversal

Public void TopToBottomReserver (Node node)
{
Queue <Node> queue = new Queue <Node> ();

Queue. Enqueue (node );
While (queue. Count> 0)
{
Node = queue. Dequeue ();

If (node! = Null)
{
Console. Write (node. Value );

Queue. Enqueue (node. Left );
Queue. Enqueue (node. Right );
}
}
}

# Endregion

# Region height
Public int GetTreeDepth (Node node)
{
If (node = null)
{
Return 0;
}
Else if (node. Left = null & node. Right = null)
{
Return 1;
}
Else
{
Int ldepth = GetTreeDepth (node. Left );
Int rdepth = GetTreeDepth (node. Right );
Return ldepth> rdepth? 1 + ldepth: 1 + rdepth;
}
}

// The method is the same as the traversal, and two intls are used as the marker.
Public int GetTreeDepthNoneRecursion (Node root)
{
Int depth = 0;

Stack <Node> stack = new Stack <Node> ();

Node lastvisit = root; // The Last accessed Node

While (root! = Null | stack. Count> 0)
{
If (root! = Null)
{
Stack. Push (root );

Depth = stack. Count> depth? Stack. Count: depth;
Root = root. Left;
}
Else
{
Node p = stack. Peek (); // cannot be obtained

If (p. Right = null | p. Right = lastvisit)
{
Stack. Pop ();
Lastvisit = p;
}
Else // non-leaf node, which has not been accessed on the right and accessed on the right
{
Root = p. Right;
}
}
}

Return depth;
}
# Endregion

Public int GetTreeWidth (Node root)
{
If (root = null)
{
Return 0;
}
Else if (root. Left = null & root. Right = null)
{
Return 1;
}
Else
{
Int lwidth = GetTreeWidth (root. Left );
Int rwidth = GetTreeWidth (root. Right );

Return 1 + lwidth-rwidth;
}
}

// Total number of returned nodes
Public int GetNodeCount (Node root)
{
If (root = null)
{
Return 0;
}
Else if (root. Left = null & root. Right = null)
{
Return 1;
}
Else
{
Int lwidth = GetNodeCount (root. Left );
Int rwidth = GetNodeCount (root. Right );

Return 1 + lwidth + rwidth;
}
}

// Obtain all nodes on the specified layer
Private List <Node> GetNodeListInDepth (Node root, int depthNum)
{
Int treeheight = GetTreeDepth (root );

If (root = null | depthNum> treeheight)
{
Return new List <Node> ();
}

List <Node> sumNode = new List <Node> ();

If (depthNum = 1)
{
SumNode. Add (root );
Return sumNode;
}

Node node = root;

List <Node> lchilrenlist = new List <Node> ();
Lchilrenlist = GetNodeListInDepth (node. Left, depthNum-1 );

List <Node> rchilrenlist = new List <Node> ();
Rchilrenlist = GetNodeListInDepth (node. Right, depthNum-1 );

SumNode. AddRange (lchilrenlist );
SumNode. AddRange (rchilrenlist );

Return sumNode;
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.