Preface: Work for a few years, but the previous college algorithms are almost forgotten. When you are ready to change the time to pick up the algorithm, because the program language after graduation is Java, so ready to use the Java language to achieve.
Requirements: The following two-fork tree, please write an algorithm to achieve layered left-to-right printing binary tree
Expected results:
Root
left01 right01
left11 right11 left12 right12
Code:
/** * * * * * * Package my.algorithm.ch01;
Import java.util.LinkedList;
Import Java.util.Queue; /** * Layered Print binary tree * * @Builder Create by Arno * @Email admin@happy-dev.com * @Time May 1, 2017/public class Treeprint {public static void print (BinaryTree root) {//Create a queue to store nodes queue<binarytree> queue = new Lin
Kedlist<binarytree> ();
The current row prints the most right node BinaryTree last;
The next line prints the most right node BinaryTree nlast = null;
last = root;
First put the root into the queue queue.add (root); When the queue is not empty, it loops poll until the queue is empty while (Queue.size () > 0) {//eject node BinaryTree nowtree = queue
. Poll ();
If the current node has a left node, the left node is pressed into the queue if (Nowtree.hasleftnode ()) {Queue.add (Nowtree.getleftnode ());
Nlast = Nowtree.getleftnode (); ///If the current node has a right node, press the left node into the queue if (Nowtree.hasrightnode ()) {Queue.add (nowtree.getright
Node ()); Nlast = Nowtree.getrightnode ();
} System.out.print ("" + Nowtree.getvalue ());
The newline if (Last.equals (Nowtree)) {System.out.println () when the current print node is the most right node of the current row;
last = Nlast; }}/** * Test with * @param args/public static void main (string[] args) {Bina
Rytree root = new BinaryTree ();
Root.setvalue ("root");
BinaryTree left01 = new BinaryTree ("left01");
BinaryTree right01 = new BinaryTree ("right01");
Root.setleftnode (left01);
Root.setrightnode (right01);
BinaryTree left11 = new BinaryTree ("left11");
BinaryTree right11 = new BinaryTree ("right11");
Left01.setleftnode (LEFT11);
Left01.setrightnode (RIGHT11);
BinaryTree left12 = new BinaryTree ("left12");
BinaryTree right12 = new BinaryTree ("right12");
Right01.setleftnode (left12);
Right01.setrightnode (right12); print (root);
} class BinaryTree {/** * node value */private String value;
/** * Left node * * Private BinaryTree Leftnode;
/** * Right Node * * Private BinaryTree Rightnode;
/** * Default parameterless construction/public BinaryTree () {}/** * Initialization value Construction * * @param value * *
Public BinaryTree (String value) {this.value = value;
}/** * @return The value */public String GetValue () {return value;
}/** * @param value * The value to set */public void SetValue (String value) {
This.value = value;
}/** * @return the Leftnode * * * public BinaryTree Getleftnode () {return leftnode; }/** * @param leftnode * The Leftnode to set */public void Setleftnode BinaryTree lef
tnode) {this.leftnode = Leftnode; }/** * @return the Rightnode * *
Public BinaryTree Getrightnode () {return rightnode; }/** * @param rightnode * The Rightnode to set */public void Setrightnode (BinaryTree
Rightnode) {this.rightnode = Rightnode; /** * To determine if there is a left node * * @return Boolean/public boolean Hasleftnode () {if this.left Node = = NULL | |
This.leftNode.getValue () = = null) {return false;
return true; /** * To determine if there is a right node * * @return Boolean/public boolean Hasrightnode () {if This.rig Htnode = = NULL | |
This.rightNode.getValue () = = null) {return false;
return true; }
}
Test results:
Record, afraid of forgetting yourself.