Basic operations for implementing Binary Trees in Java

Source: Internet
Author: User

[Java]
Package D0726;
 
Import java. util. ArrayDeque;
Import java. util. Queue;
 
Public class TestTree {
Public static void main (String [] args ){
// Test Treenode
Treenode node = buildTree (null, 1 );
System. out. print ("sequential traversal :");
PreOrder (node );
System. out. println ();
System. out. print ("sequential traversal :");
InOrder (node );
System. out. println ();
System. out. print ("sequential traversal :");
PostOrder (node );
System. out. println ();
System. out. print ("hierarchical traversal :");
LevelOrder (node );
System. out. println ();
System. out. print ("Number of leaf nodes :");
System. out. println (leafNum (node ));
System. out. print ("binary tree depth :");
System. out. println (deep (node ));

}
 
// Create a tree @ k to control the depth of the tree
Static char I = 'a'; // Node
 
Public static Treenode buildTree (Treenode node, int k ){
If (k> 4)
Return null;
If (node = null ){
Node = new Treenode ("" + I ++ );
}
Node. lChild = buildTree (node. lChild, k + 1 );
Node. rChild = buildTree (node. rChild, k + 1 );
Return node;
}
 
// Construct a binary tree based on the first and middle orders
Public Treenode buildTree (String pre, String in ){
If (pre. length () <= 0)
Return null;
Treenode root = new Treenode (pre. charAt (0) + "");
// Centered on the root node, the middle order is divided into two subsequences
String leftin = "";
String rightin = "";
 
// The length of the left middle order of the root. The first order is divided into two sub first orders: left and right.
String leftpre = "";
String rightpre = "";
Root. lChild = buildTree (leftpre, leftin );
Root. rChild = buildTree (rightpre, rightin );
Return root;
}
 
// First-order traversal
Public static void preOrder (Treenode node ){
If (node! = Null ){
System. out. print (node. data );
PreOrder (node. lChild );
PreOrder (node. rChild );
}
}
 
// Sequential Traversal
Public static void inOrder (Treenode node ){
If (node! = Null ){
InOrder (node. lChild );
System. out. print (node. data );
InOrder (node. rChild );
}
}
 
// Post-order traversal
Public static void postOrder (Treenode node ){
If (node! = Null ){
PostOrder (node. lChild );
PostOrder (node. rChild );
System. out. print (node. data );
}
}
 
// Hierarchical Traversal
Public static void levelOrder (Treenode node ){
If (node = null)
Return;
Queue <Treenode> queue = new ArrayDeque <Treenode> ();
Queue. add (node );
While (! Queue. isEmpty ()){
Treenode temp = queue. poll ();
System. out. print (temp. data );
If (temp. lChild! = Null)
Queue. add (temp. lChild );
If (temp. rChild! = Null)
Queue. add (temp. rChild );
}
}
 
// Calculate the number of leaf nodes of the tree
Public static int leafNum (Treenode node ){
If (node! = Null ){
If (node. lChild = null & node. rChild = null ){
Return 1;
}
Return leafNum (node. lChild) + leafNum (node. rChild );
}
Return 0;
}
// Calculate the depth of a binary tree
Public static int deep (Treenode node ){
If (node = null) return 0;
If (node. lChild = null & node. rChild = null) return 1;
Return leafNum (node. lChild) + leafNum (node. rChild );
}
}
 
Class Treenode {
Public String data;
Public Treenode lChild;
Public Treenode rChild;
 
// Constructor
Public Treenode (String data ){
This. data = data;
This. lChild = null;
This. rChild = null;
}
 
}
Author: lhfight

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.