Data Structure-Tree operations (recursive and non-recursive tree traversal)-(4-complement), data structure Recursion

Source: Internet
Author: User

Data Structure-Tree operations (recursive and non-recursive tree traversal)-(4-complement), data structure Recursion

Previously I wrote some operations on the tree, but there is no non-Recursive Method for traversing the tree.

There are usually four ways to traverse a tree: 1. Layered traversal (the height of the tree is required, which is not taken into account in this article)

2. pre-order traversal (left and right); 3. Middle-order traversal (left and right); 4. Post-order traversal (left and right)

The structure of the tree is as follows:

Hierarchical traversal: 123456789

Forward traversal: 124895367

In-order traversal: 849251637

Post-order traversal: 894526731

Java code implements recursive and non-recursive implementations of three types of Traversal

Package com. lip. datastructure. tree; import java. util. stack;/*** @ author lip */public class Tree {public static void main (String [] args) {Node <Integer> root = getNode (); System. out. println ("pre-order traversal (non-recursive)"); preOrder (root); System. out. println ("pre-order traversal (recursion)"); preOrderRecursive (root); System. out. println (); System. out. println ("sequential traversal (non-recursive)"); infixOrder (root); System. out. println ("recursive traversal"); infixOrderRecursive (root); System. out. println (); System. out. println ("post-order traversal (non-recursive)"); postOrder (root); System. out. println ("post-order traversal (recursion)"); postOrderRecursive (root);} public static Node getNode () {Node <Integer> node1 = new Node (1 ); node <Integer> node2 = new Node (2); Node <Integer> node3 = new Node (3); node1.left = node2; node1.right = node3; node <Integer> node4 = new Node (4); Node <Integer> node5 = new Node (5); node2.left = node4; node2.right = node5; Node <Integer> n Ode6 = new Node (6); Node <Integer> node7 = new Node (7); node3.left = node6; node3.right = node7; Node <Integer> node8 = new Node (8 ); node <Integer> node9 = new Node (9); node4.left = node8; node4.right = node9; return node1 ;}// pre-order traversal, non-recursion @ SuppressWarnings ("rawtypes ") public static void preOrder (Node root) {Stack <Node> stack = new Stack <Node> (); stack. push (root); while (stack. size ()> 0) {Node tempNode = stack. pop (); if (tempNode! = Null) {System. out. print (tempNode. data); stack. push (tempNode. right); stack. push (tempNode. left) ;}} System. out. println ();} // pre-order traversal (left and right), recursive public static void preOrderRecursive (Node root) {if (root! = Null) {System. out. print (root. data); preOrderRecursive (root. left); preOrderRecursive (root. right) ;}}// the traversal in the middle order (left root and right), non-recursive public static void infixOrder (Node root) {Stack <Node> stack = new Stack <Node> (); stack. push (root); while (stack. size ()> 0) {Node temp = stack. pop (); if (temp! = Null) {if (temp. left = null) & (temp. right = null) System. out. print (temp. data); else {stack. push (temp. right); stack. push (new Node (temp. data); stack. push (temp. left) ;}} System. out. println ();} // The traversal in the middle order (left root and right), recursive public static void infixOrderRecursive (Node root) {if (root! = Null) {infixOrderRecursive (root. left); System. out. print (root. data); infixOrderRecursive (root. right) ;}}// post-order traversal (left and right root), non-recursive public static void postOrder (Node root) {Stack <Node> stack = new Stack <Node> (); stack. push (root); Node temp; while (stack. size ()> 0) {temp = stack. pop (); if (temp! = Null) {if (temp. left = null & temp. right = null) System. out. print (temp. data); else {stack. push (new Node (temp. data); stack. push (temp. right); stack. push (temp. left) ;}} System. out. println ();} // post-order traversal (left and right root), recursive public static void postOrderRecursive (Node root) {if (root! = Null) {postOrderRecursive (root. left); postOrderRecursive (root. right); System. out. print (root. data) ;}}} class Node <T> {public Node left; public Node right; public T data; public Node (T data) {this. data = data ;}}

In fact, the difference between Recursion and non-recursion is that non-recursion uses stack for backtracking (it can also be seen as a special recursion)

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.