Java binary tree traversal (recursion) program code

Source: Internet
Author: User

This article introduces the code of the binary tree traversal (recursion) Program in java. Common recursive traversal and other advanced algorithms are used to traverse the binary tree, for more information, see this article.

Test binary tree traversal and Recursive Algorithms

The Code is as follows: Copy code

 

Public class TestBinaryTree {
Public static void main (String [] args ){
Node <String> g = new Node <String> ("G", null, null );
Node <String> e = new Node <String> ("E", null, null );
Node <String> f = new Node <String> ("F", null, null );
Node <String> d = new Node <String> ("D", null, g );
Node <String> B = new Node <String> ("B", d, e );
Node <String> c = new Node <String> ("C", null, f );
Node <String> a = new Node <String> ("A", B, c );
System. out. println ("generated Binary Tree :");
System. out. println ("");
System. out. println ("| ");
System. out. println ("| --------- | ");
System. out. println ("B C ");
System. out. println ("| ");
System. out. println ("| --------- | ----- | ");
System. out. println ("d e f ");
System. out. println ("| ");
System. out. println ("---- | ");
System. out. println ("G ");
System. out. println ("binary tree depth:" + BinaryTree. getDepth ());
System. out. print ("Forward traversal :");
BinaryTree. priorderTraversal ();
System. out. println ();
System. out. print ("sequential traversal :");
BinaryTree. inorderTraversal ();
System. out. println ();
System. out. print ("sequential traversal :");
BinaryTree. postorderTraversal ();
System. out. println ();
}
}
// Binary Tree
Class BinaryTree {
// Pre-order traversal
Static <T> void priorderTraversal (Node <T> node ){
If (node! = Null ){
VisitNode (node );
PriorderTraversal (node. getLeftChild ());
PriorderTraversal (node. getRightChild ());
}
}
// Sequential Traversal
Static <T> void inorderTraversal (Node <T> node ){
If (node! = Null ){
InorderTraversal (node. getLeftChild ());
VisitNode (node );
InorderTraversal (node. getRightChild ());
}
}
// Post-order traversal
Static <T> void postorderTraversal (Node <T> node ){
If (node! = Null ){
PostorderTraversal (node. getLeftChild ());
PostorderTraversal (node. getRightChild ());
VisitNode (node );
}
}
// Binary Tree depth
Static <T> int getDepth (Node <T> node ){
If (node = null ){
Return 0;
}
Int leftDepth = 0;
Int rightDepth = 0;
LeftDepth = getDepth (node. getLeftChild ());
RightDepth = getDepth (node. getRightChild ());
Return (leftDepth> rightDepth? LeftDepth: rightDepth) + 1;
}
// Access the node
Static <T> void visitNode (Node <T> node ){
System. out. print (node. getKey () + "");
}
}
// Node
Class Node <T> {
Private T key;
Private Node <T> leftChild;
Private Node <T> rightChild;
Public Node (){
}
Public Node (T key, Node <T> leftChild, Node <T> rightChild ){
Super (); www.2cto.com
This. key = key;
This. leftChild = leftChild;
This. rightChild = rightChild;
}
Public T getKey (){
Return key;
}
Public void setKey (T key ){
This. key = key;
}
Public Node <T> getLeftChild (){
Return leftChild;
}
Public void setLeftChild (Node <T> leftChild ){
This. leftChild = leftChild;
}
Public Node <T> getRightChild (){
Return rightChild;
}
Public void setRightChild (Node <T> rightChild ){
This. rightChild = rightChild;
}
}

Non-Recursive Algorithms

After a binary tree class is created, you can create a binary tree instance and implement the first root traversal, middle root traversal, And Then Root traversal of the binary tree. The Code is as follows:

The Code is as follows: Copy code

Package package2;

Public class BinaryTreePreorder {
 
Public static void preOrder (BinaryTree root) {// first root Traversal
If (root! = Null ){
System. out. print (root. data + "-");
PreOrder (root. left );
PreOrder (root. right );
}
}
 
Public static void inOrder (BinaryTree root) {// root Traversal

If (root! = Null ){
InOrder (root. left );
System. out. print (root. data + "--");
InOrder (root. right );
}
}
 
Public static void postOrder (BinaryTree root) {// post-root Traversal

If (root! = Null ){
PostOrder (root. left );
PostOrder (root. right );
System. out. print (root. data + "---");
}
}
 
Public static void main (String [] str ){
Int [] array = {12, 76, 35, 22, 16, 48, 90, 46, 9, 40 };
BinaryTree root = new BinaryTree (array [0]); // create a binary tree
For (int I = 1; I <array. length; I ++ ){
Root. insert (root, array [I]); // insert data into a binary tree
}
System. out. println ("first root traversal :");
PreOrder (root );
System. out. println ();
System. out. println ("root traversal :");
InOrder (root );
System. out. println ();
System. out. println ("root traversal :");
PostOrder (root );

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.