Construction two fork Tree
1
/ \
2 3
/ / \
4 5 7 //
6 8
Forward, middle and subsequent traversal of one or two-fork tree (recursive and non-recursive implementation)
Two or two-fork tree breadth, depth first traversal
Third, to find the height of the two-fork tree
Import java.util.*;
depth and breadth of binary tree traverse public class treesearch{//two fork tree node definition public static class binarytreenode{int value;
Binarytreenode left;
Binarytreenode right;
Constructor public Binarytreenode (int v) {this.value=v;
}//second constructor public binarytreenode (int v,binarytreenode left,binarytreenode right) {super ();
This.value=v;
This.left=left;
This.right=left;
}///Access the node of the binary tree public static void visit (Binarytreenode node) {System.out.print (node.value+ ""); //********************************************************//Recursive implementation two fork tree first, in, after the subsequent traversal of the public static void preorder (Bin
Arytreenode node) {if (node!=null) {Visit (node);
Preorder (Node.left);
Preorder (node.right); } public static void Inorder (Binarytreenode node) {if (node!=null) {inorder node.
left);
Visit (node); Inorder (Node.rigHT); }} public static void Postorder (Binarytreenode node) {if (node!=null) {postorder (node.left
);
Postorder (Node.right);
Visit (node); }//************* non-recursive implementation of the two-fork tree first, in the sequential traversal ********************/** non-recursive implementation of the two-fork tree first-order traversal/public static void Itera
Tivepreorder (Binarytreenode node) {stack<binarytreenode> Stack = new stack<> ();
if (node!= null) {Stack.push (node);
while (!stack.empty ()) {node = Stack.pop ();
Access node visit first;
The right sub node is pressed into the stack if (node.right!= null) {Stack.push (node.right);
The Zoozi node is pressed into the stack if (node.left!= null) {Stack.push (node.left); The/** of the two-fork tree is not recursive/public static void Iterativeinorder (Binarytreenode Root) {Stack<binarytreenode> stack = new stack<> ();
Binarytreenode node = root;
while (node!= null | | stack.size () > 0) {//push all left sub nodes of the current node into the stack while (node!= null) {
Stack.push (node);
node = Node.left;
}//Access node, processing the right subtree of the node if (stack.size () > 0) {node = Stack.pop ();
Visit (node);
node = node.right; /** non-recursive use of a single stack to implement two-fork tree sequence traversal/public static void Iterativepostorder (Binarytreenode root) {St
ack<binarytreenode> stack = new stack<> ();
Binarytreenode node = root;
When accessing the root node, the right subtree is judged to be enough to be accessed binarytreenode prenode = null;
while (node!= null | | stack.size () > 0) {//The left node of the current node is all in the stack while (node!= null) {
Stack.push (node);
node = Node.left;
} if (Stack.size () > 0) { Binarytreenode temp = Stack.peek (). Right; A root node is accessed on the premise that no right subtree or right subtree has been accessed if (temp = null | | | | temp = prenode) {node = Stack.pop (
);
Visit (node);
Prenode = node;//Record node = null that has just been accessed;
else {//Process right subtree node = temp; /** non-recursive use of double stack to implement two-fork tree sequence traversal/public static void Iterativepostorderbytwostacks (BINARYTR
Eenode root) {stack<binarytreenode> Stack = new stack<> ();
stack<binarytreenode> temp = new stack<> ();
Binarytreenode node = root;
while (node!= null | | stack.size () > 0) {//Push the current node and its right child node into the stack while (node!= null) {
Stack.push (node);
Temp.push (node);
node = node.right; ///processing the left subtree of the top node of the stack if (Stack.size () > 0) {node = Stack.pop ();
node = Node.left;
} while (Temp.size () > 0) {node = Temp.pop ();
Visit (node); The breadth of the//**************************************************************//two fork tree is first traversed by the public static void Levelsea Rchbinarytree (Binarytreenode root) {queue<binarytreenode>queue=new linkedlist<> ();//Storage node I of each layer
F (root!=null) {queue.add (root);
while (!queue.isempty ()) {Binarytreenode node=queue.poll ();
Visit (node);
if (node.left!=null) {queue.add (node.left);
} if (Node.right!=null) {queue.add (node.right);
}}///Two The depth of the fork tree is first traversed by the public static void Deepsearchbinarytree (Binarytreenode root) {
Stack <binarytreenode>stack=new stack<> (); if (root!=null) {
Stack.push (root);
while (!stack.isempty ()) {Root=stack.pop ();
Visit (Root);
if (root.right!=null)//The right node is pressed into the bottom of the stack {stack.push (root.right);
} if (Root.left!=null) {Stack.push (root.left);
}}///fork tree height public static int binarytreeheight (Binarytreenode root) {
if (root==null) {return 0;
int height=0;
if (root!=null) {int left=binarytreeheight (root.left);
int Right=binarytreeheight (root.right);
height=left>right?left+1:right+1;
return height;
public static void Main (String[]args) {System.out.println ("Hello world!");
Construction two forks//1/////2 3//////4 5 7/////6 8 Binarytreenode root = New Binarytreenode (1);
Binarytreenode Node2 = new Binarytreenode (2);
Binarytreenode node3 = new Binarytreenode (3);
Binarytreenode node4 = new Binarytreenode (4);
Binarytreenode node5 = new Binarytreenode (5);
Binarytreenode node6 = new Binarytreenode (6);
Binarytreenode Node7 = new Binarytreenode (7);
Binarytreenode node8 = new Binarytreenode (8);
Root.left = Node2;
Root.right = Node3;
Node2.left = Node4;
Node3.left = NODE5;
Node3.right = Node7;
Node4.right = Node6;
Node7.left = Node8;
SYSTEM.OUT.PRINTLN ("Binary tree First Order recursive traversal");
Preorder (root);
System.out.println ();
SYSTEM.OUT.PRINTLN ("Binary tree first sequence traversal not recursive");
Iterativepreorder (root);
System.out.println ();
System.out.println ("Sequential recursive traversal in binary tree");
Inorder (root);
System.out.println ();
SYSTEM.OUT.PRINTLN ("Sequential traversal not recursive" in binary tree);
Iterativeinorder (root); System.out.println ();
SYSTEM.OUT.PRINTLN ("Binary tree Sequence recursive traversal");
Postorder (root);
System.out.println ();
SYSTEM.OUT.PRINTLN ("Binary tree Tankofi recursive subsequent traversal");
Iterativepostorder (root);
System.out.println ();
SYSTEM.OUT.PRINTLN ("Binary tree double stack non-recursive sequence traversal");
Iterativepostorderbytwostacks (root);
System.out.println ();
System.out.println ("Two fork Tree breadth (hierarchy) traversal");
Levelsearchbinarytree (root);
System.out.println ();
System.out.println ("Two fork Tree depth traversal");
Deepsearchbinarytree (root);
System.out.println ();
System.out.println ("The height of the binary tree");
System.out.println (Binarytreeheight (root));
}
}