Recently review the data structure of the two-fork tree related issues, here to tidy up
This includes:
1, binary tree of the first order to create
2. Recursive first order traversal of binary tree
3. Non-recursive first order traversal of binary tree
4. Recursive middle sequence traversal of binary tree
5. Non-recursive middle sequence traversal of binary tree
6. Recursive sequential traversal of binary tree
7. Non-recursive sequential traversal of binary tree
8, the binary tree hierarchy traversal
Thanks for your blog http://blog.csdn.net/skylinesky/article/details/6611442 's guidance here.
/** two fork tree node definition */class node<t>{private T value;private node<t> left;private node<t> right;public node () { }public Node (node<t> left, node<t> right, T value) {this.left = Left;this.right = Right;this.value = value;} Public Node (T value) {This (null, null, value);} Public node<t> GetLeft () {return this.left;} public void Setleft (node<t> left) {this.left = left;} Public node<t> GetRight () {return this.right;} public void Setright (node<t> right) {this.right = right;} Public T GetValue () {return this.value;} public void SetValue (T value) {this.value = value;}}
Import Java.io.file;import java.io.filenotfoundexception;import java.util.linkedlist;import java.util.Scanner;/** * The definition of a binary tree: Or is empty, or only the root node, or has Saozi right subtree (5 basic forms) * Binary Tree properties: * 1, on the second level of the binary tree at most 2^ (i-1) nodes (i>=1) * 2, the depth of the two fork tree at a maximum of 2^ (k)-1 nodes (k>=1) * 3, for any two-fork tree, if its terminal node number is n, the number of nodes with the number of degrees is M, then n = m + 1 * 4, the depth of the full binary tree with n nodes is k = Floor (log2 (n)) + 1 * 5, there are n+1 empty chain fields in the two-linked list with n nodes * @au Thor Little Rookie * creation time: 2014-08-10 */public class Binarytree<t> {/** two fork tree root node */private node<t> root;public BinaryTree ( {}public BinaryTree (node<t> root) {this.root = root;} /** sequence traversal creates a two-fork tree *//**input.txt:-+ A # # # # # #/E # # F # # # # represents an empty node */public void Createbitree () {Scanner SCN = null;try {s cn = New Scanner (New File ("Input.txt"));} catch (FileNotFoundException e) {e.printstacktrace ();} This.root = Createbitree (root, SCN);} Private node<t> Createbitree (node<t> Node, Scanner SCN) {String temp = Scn.next (); if (Temp.trim (). Equals ("#" ) {return null;} Else{node = new Node<t> ((T) temp); Node.setleft (Createbitree (node.getlEFT (), SCN); Node.setright (Createbitree (Node.getright (), SCN); return node;}} /** recursive traversal of binary tree */public void Preordertraverse () {preordertraverse (root);} private void Preordertraverse (node<t> node) {if (node! = null) {System.out.println (Node.getvalue ()); Preordertraverse (Node.getleft ());p Reordertraverse (Node.getright ());}} /** First Order non-recursive traversal binary tree */public void Nrpreordertraverse () {stack<node<t>> Stack = new stack<node<t>> (); node<t> node = root;while (node! = NULL | |!stack.isempty ()) {while (node! = null) {System.out.println (Node.getvalue ( ); Stack.push (node); node = Node.getleft ();} node = Stack.pop (); node = Node.getright ();}} /** recursive traversal of binary tree */public void Inordertraverse () {inordertraverse (root);} private void Inordertraverse (node<t> node) {if (node! = null) {Inordertraverse (Node.getleft ()); System.out.println (Node.getvalue ()); Inordertraverse (Node.getright ());}} /** middle order non-recursive traversal binary tree */public void Nrinordertraverse () {stack<node<t>> Stack = new stack<node<t>> (); Node< t> node = root;while (node! = NULL | |!stack.isempty ()) {while (node! = null) {Stack.push (node); node = Node.getleft ();} node = Stack.pop (); System.out.println (Node.getvalue ()); node = Node.getright ();}} /** sequential recursion traversal binary tree */public void Postordertraverse () {postordertraverse (root);} private void Postordertraverse (node<t> node) {if (node! = null) {Postordertraverse (Node.getleft ()); Postordertraverse (Node.getright ()); System.out.println (Node.getvalue ());}} /** Post-secondary non-recursive traversal binary tree */public void Nrpostordertraverse () {stack<node<t>> Stack = new Stack<node<t>> () ; node<t> node = root; node<t> Prenode = null;//record before the right node of the traversal while (node! = NULL | |!stack.isempty ()) {while (node! = null) {Stack.push (node); node = Node.getleft ();} node = Stack.gettop ();/** If the right node is empty, or the right node has been traversed before, print the root node */if (node.getright () = null | | node.getright () = = Prenode) { System.out.println (Node.getvalue ()); node = Stack.pop ();p renode = Node;node = null;} Else{node = Node.getright ();}}} /** hierarchy traverse binary tree */public void Leveltraverse () {leveltrAverse (root);} private void Leveltraverse (node<t> Node) {queue<node<t>> Queue = new queue<node<t>> (); Queue.push (node), while (!queue.isempty ()) {node = Queue.pop (), if (node! = null) {System.out.println (Node.getvalue ()); Queue.push (Node.getleft ()); Queue.push (Node.getright ());}}} public static void Main (string[] args) {binarytree<string> bt = new binarytree<string> (); Bt.createbitree () ;//bt.preordertraverse ();//bt.inordertraverse ();//bt.postordertraverse ();//bt.nrpreordertraverse ();// Bt.nrinordertraverse ();//bt.nrpostordertraverse (); Bt.leveltraverse ();}}
Note: For a definition of stacks and queues, please refer to another blog post "
Java implementation stack and queue definition:http://blog.csdn.net/junwei_yu/article/details/38470825