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 ); |