Java implementation Two fork tree creation, recursive/non-recursive traversal

Source: Internet
Author: User

Recent review of 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 has the root node, or has Saozi right subtree (5 basic forms) * Binary Tree properties: * 1, on the second level of the binary tree, there are at most 2^ (i-1) nodes (i>=1) * 2, depth of K two fork tree up to 2^ (k)-1 nodes (k>=1) * 3, for whatever a binary tree, it is assumed that its terminal node number is n, the number of degrees of 2 is M.  Then n = m + 1 * 4, the total binary tree with n nodes has a depth of k = Floor (log2 (n)) + 1 * 5, and there are n+1 empty links in the two-linked list with n nodes * * @author 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 ();/** assumes that the right node is empty, or that the right node has been traversed before. Print 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: The definitions of stacks and queues are included in this article, and there's a blog post."

Java implementation stack and queue definition:http://blog.csdn.net/junwei_yu/article/details/38470825

Java implementation Two fork tree creation, recursive/non-recursive traversal

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.