Implement binary tree using Java

Source: Internet
Author: User

A few days ago, someone asked me how to implement the post-Order Non-recursive traversal of Binary Trees. I didn't think of it for a moment. Today, I wrote all the operations related to binary trees, including creating them, in the middle order, the first order, the second order (recursive and non-recursive), the focus is on the Java first order to create a binary tree and the Second Order Non-recursive traversal implementation.

The following are the Implementation Details:CodeThe input is the input.txt file in the project directory. "#" indicates that the node is empty.

Package COM. algorithm. tree; import Java. io. file; import Java. io. filenotfoundexception; import Java. util. queue; import Java. util. imports; import Java. util. stack; import Java. util. concurrent. linkedblockingqueue; public class tree <t> {private node <t> root; public tree () {} public tree (node <t> root) {This. root = root;} // create a binary tree public void buildtree () {partition SCN = NULL; try {SCN = new partition (new file ("input.txt" );} Catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} root = createtree (root, SCN);} // first traverse and create a binary tree private node <t> createtree (node <t> node, scanner SCN) {string temp = SCN. next (); If (temp. trim (). equals ("#") {return NULL;} else {node = new node <t> (t) temp); node. setleft (createtree (node. getleft (), SCN); node. setright (createtree (node. getright (), SCN); Return node ;}}// In-order traversal (recursion) Public void inordertraverse () {inordertraverse (Root);} public void inordertraverse (node <t> node) {If (node! = NULL) {inordertraverse (node. getleft (); system. out. println (node. getvalue (); inordertraverse (node. getright () ;}}// the traversal in the middle order (non-recursion) 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 () ;}/// first-order traversal (recursion) Public void preordertraverse () {preordertraverse (Root);} public void preordertraverse (node <t> node) {If (node! = NULL) {system. out. println (node. getvalue (); preordertraverse (node. getleft (); preordertraverse (node. getright () ;}}// first-order traversal (non-recursion) 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 () ;}/// post-order traversal (recursion) Public void postordertraverse () {postordertraverse (Root);} public void postordertraverse (node <t> node) {If (node! = NULL) {postordertraverse (node. getleft (); postordertraverse (node. getright (); system. out. println (node. getvalue () ;}}// subsequent traversal (non-recursive) Public void nrpostordertraverse () {stack <node <t> stack = new stack <node <t> (); node <t> node = root; node <t> prenode = NULL; // indicates the last accessed node while (node! = NULL |! Stack. isempty () {While (node! = NULL) {stack. push (node); node = node. getleft ();} node = stack. peek (); If (node. getright () = NULL | node. getright () = prenode) {system. out. println (node. getvalue (); node = stack. pop (); prenode = node; node = NULL;} else {node = node. getright () ;}}// traverse the public void leveltraverse () {leveltraverse (Root);} public void leveltraverse (node <t> node) {queue <node <t> queue = new linkedblockingqueue <node <t> (); Queue. Add (node); While (! Queue. isempty () {node <t> temp = queue. Poll (); If (temp! = NULL) {system. out. println (temp. getvalue (); queue. add (temp. getleft (); queue. add (temp. getright () ;}}}// Tree node class node <t> {private node <t> left; private node <t> right; private T value; 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 left ;} public void setleft (node <t> left) {This. left = left;} public node <t> getright () {return right;} public void setright (node <t> right) {This. right = right;} public t getvalue () {return value;} public void setvalue (T value) {This. value = value ;}}

Test code:

 
Package COM. algorithm. tree; public class treetest {/*** @ Param ARGs */public static void main (string [] ARGs) {tree <integer> tree = new tree <integer> (); tree. buildtree (); system. out. println ("sequential traversal"); tree. inordertraverse (); tree. nrinordertraverse (); system. out. println ("subsequent traversal"); // tree. nrpostordertraverse (); tree. postordertraverse (); tree. nrpostordertraverse (); system. out. println ("sequential traversal"); tree. preordertraverse (); tree. nrpreordertraverse ();//}}

The younger brother is using generic type for the first time. Do not spray it in an inappropriate place.

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.