All recursive and non-recursive Traversal Algorithms for Binary Trees in java

Source: Internet
Author: User

[Java]
Construct a binary tree using arrays, all Traversal Algorithms, and recursive algorithms for finding the depth of a binary tree
[Java]
Import java. util. Collections list;
 
 
Public class BinaryTree {

Private Node <Integer> root;

Private int size; www.2cto.com

Public BinaryTree (){
Root = new Node <Integer> ();
}

Public BinaryTree (int [] values ){
System. out. print ("New binaryTree :");
For (int I: values ){
System. out. print (I );
}
System. out. println ();
Boolean isLeft = true;
Int len = values. length;
If (len = 0)
Return;
Queue list <Node <Integer> queue = new queue list <Node <Integer> ();
Root = new Node <Integer> (values [0]);
Queue. addLast (root );
Node parent = null;
Node current = null;
For (int I = 1; I <len; I ++ ){
Current = new Node <Integer> (values [I]);
Queue. addLast (current );
If (isLeft)
Parent = queue. getFirst ();
Else
Parent = queue. removeFirst ();
If (isLeft ){
Parent. setLeftChild (current );
IsLeft = false;
} Else {
Parent. setRightChild (current );
IsLeft = true;
}
}
}

Public void inorder (){
System. out. print ("binaryTree recursive sequential traversal :");
InorderTraverseRecursion (root );
System. out. println ();
}

Public void layerorder (){
System. out. print ("binaryTree hierarchical traversal :");
Queue list <Node <Integer> queue = new queue list <Node <Integer> ();
Queue. addLast (root );
Node <Integer> current = null;
While (! Queue. isEmpty ()){
Current = queue. removeFirst ();
If (current. getLeftChild ()! = Null)
Queue. addLast (current. getLeftChild ());
If (current. getRightChild ()! = Null)
Queue. addLast (current. getRightChild ());
System. out. print (current. getValue ());
}
System. out. println ();
}

Public int getLength (){
Return getLengthRecursion (root );
}

Private int getLengthRecursion (Node <Integer> node ){
If (node = null)
Return 0;
Int llen = getLengthRecursion (node. getLeftChild ());
Int rlen = getLengthRecursion (node. getRightChild ());
Int maxlen = Math. max (llen, rlen );
Return maxlen + 1;
}
Public void preorder (){
System. out. print ("binaryTree recursive first-order traversal :");
PreorderTraverseRecursion (root );
System. out. println ();
}

Private void inorderTraverseRecursion (Node <Integer> node ){
// TODO Auto-generated method stub
If (node. getLeftChild ()! = Null)
InorderTraverseRecursion (node. getLeftChild ());
System. out. print (node. getValue ());
If (node. getRightChild ()! = Null)
InorderTraverseRecursion (node. getRightChild ());
}

 
Private void preorderTraverseRecursion (Node <Integer> node ){
System. out. print (node. getValue ());
If (node. getLeftChild ()! = Null)
PreorderTraverseRecursion (node. getLeftChild ());
If (node. getRightChild ()! = Null)
PreorderTraverseRecursion (node. getRightChild ());
}

Public void preorderNoRecursion (){
System. out. print ("binaryTree non-recursive first order traversal :");
Worker list <Node <Integer> stack = new worker list <Node <Integer> ();
Stack. push (root );
Node <Integer> current = null;
While (! Stack. isEmpty ()){
Current = stack. pop ();
System. out. print (current. getValue ());
If (current. getRightChild ()! = Null)
Stack. push (current. getRightChild ());
If (current. getLeftChild ()! = Null)
Stack. push (current. getLeftChild ());
}
System. out. println ();
}

/**
* Save the elements to be accessed in the stack
*/
Public void inorderNoRecursion (){
System. out. print ("binaryTree non-recursive sequential traversal :");
Worker list <Node <Integer> stack = new worker list <Node <Integer> ();
Node <Integer> current = root;
While (current! = Null |! Stack. isEmpty ()){
While (current! = Null ){
Stack. push (current );
Current = current. getLeftChild ();
}
If (! Stack. isEmpty ()){
Current = stack. pop ();
System. out. print (current. getValue ());
Current = current. getRightChild ();
}
}
System. out. println ();
}

/**
* When the access node is the right child or the current node does not have the right child, access the current node.
*/
Public void postorderNoRecursion (){
System. out. print ("binaryTree non-recursive post-order traversal :");
Node <Integer> rNode = null;
Node <Integer> current = root;
Worker list <Node <Integer> stack = new worker list <Node <Integer> ();
While (current! = Null |! Stack. isEmpty ()){
While (current! = Null ){
Stack. push (current );
Current = current. getLeftChild ();
}
Current = stack. pop ();
While (current! = Null & (current. getRightChild () = null | current. getRightChild () = rNode )){
System. out. print (current. getValue ());
RNode = current;
If (stack. isEmpty ()){
System. out. println ();
Return;
}
Current = stack. pop ();
}
Stack. push (current );
Current = current. getRightChild ();
}

}

Public static void main (String [] args ){
BinaryTree bt = new BinaryTree (new int [] {1, 2, 3, 4, 5, 6, 7, 8 });
Bt. inorder ();
Bt. preorder ();
Bt. layerorder ();
Bt. preorderNoRecursion ();
Bt. inorderNoRecursion ();
Bt. postorderNoRecursion ();
System. out. println ("depth:" + bt. getLength ());
}
}
 
Class Node <V> {
Private V value;
Private Node <V> leftChild;
Private Node <V> rightChild;

Public Node (){
};

Public Node (V value ){
This. value = value;
LeftChild = null;
RightChild = null;
}

Public void setLeftChild (Node <V> lNode ){
This. leftChild = lNode;
}

Public void setRightChild (Node <V> rNode ){
This. rightChild = rNode;
}
 
Public V getValue (){
Return value;
}
 
Public void setValue (V value ){
This. value = value;
}
 
Public Node <V> getLeftChild (){
Return leftChild;
}
 
Public Node <V> getRightChild (){
Return rightChild;
}


}

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.