Binary tree traversal (recursive and non-recursive)

Source: Internet
Author: User

Theory:

1. Recursive definition of first (Root) sequential traversal: If the binary tree is not empty, perform the following operations in sequence: (1) access the root node; (2) traverse the left subtree; (3) traverse the right subtree. 2. recursive algorithm definition for sequential traversal in the middle (Root): If the binary tree is not empty, perform the following operations in sequence: (1) traverse the left subtree; (2) access the root node; (3) traverse the right subtree. 3. recursive algorithm definition after (Root) sequential traversal: If the binary tree is not empty, perform the following operations in sequence: (1) traverse the left subtree; (2) traverse the right subtree; (3) Access the root node

 

JAVA Implementation

Package binary tree; import Java. util. arraylist; import Java. util. using list; import Java. util. list; import Java. util. stack; public class bstree {private list <node> nodelist = new arraylist <node> (); Public node createtree (INT [] A) {for (INT data: a) nodelist. add (new node (data); int lastleftroot =. length/2-1; for (INT I = lastleftroot; I> = 0; I --) {node root = nodelist. get (I); int leftindex = I * 2 + 1; int rightindex = leftind Ex + 1; if (leftindex <. length); {node left = nodelist. get (leftindex); root. setleft (left);} If (rightindex <. length) {node right = nodelist. get (rightindex); root. setrgiht (right) ;}} node root = nodelist. get (0); Return root;} public static void printnode (node) {system. out. print (node. getdata () + "");} public static void preorderstack (node) {stack <node> stack = new stack <node> (); node P = node; If (n Ode! = NULL) {stack. Push (p); While (! Stack. isempty () {node Top = stack. Pop (); printnode (top); If (top. getright ()! = NULL) stack. Push (top. getright (); If (top. getleft ()! = NULL) stack. push (top. getleft () ;}} public static void minorderstack (node) {stack <node> stack = new stack <node> (); node P = node; while (P! = NULL |! Stack. isempty () {While (P! = NULL) {stack. Push (p); P = P. getleft ();} If (! Stack. isempty () {P = stack. pop (); printnode (p); P = P. getright () ;}} public static void postorderstack (node) {stack <node> stack = new stack <node> (); node P = node; node q = P; while (P! = NULL) {While (P. getleft ()! = NULL) {stack. Push (p); P = P. getleft () ;}while (P! = NULL & (P. getright () = NULL | P. getright () = q) {printnode (p); q = P; If (stack. isempty () return; P = stack. pop ();} stack. push (p); P = P. getright () ;}} public static void pretrav (node) {If (node = NULL) return; system. out. print (node. getdata () + ""); pretrav (node. getleft (); pretrav (node. getright ();} public static void midtrav (node) {If (node = NULL) return; midtrav (node. getleft (); system. out. print (node. getdata () + ""); midtrav (node. getright ();} public static void posttrav (node) {If (node = NULL) return; posttrav (node. getleft (); posttrav (node. getright (); system. out. print (node. getdata () + "");} public static void main (string [] ARGs) {// todo auto-generated method stub int [] A =, 16}; // the data is stored in bstree bt = new bstree () based on hierarchical traversal of the binary search tree; node root = BT. createtree (a); pretrav (Root); system. out. println (); preorderstack (Root); system. out. println (); midtrav (Root); system. out. println (); minorderstack (Root); system. out. println (); posttrav (Root); system. out. println (); postorderstack (Root); system. out. println ();}}

 

Binary tree traversal (recursive and non-recursive)

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.