Java two fork tree depth precedence recursive traversal

Source: Internet
Author: User

Depth first traverses the binary tree.


1. Recursive algorithm for middle sequence traversal:


If the binary tree is empty, the algorithm ends; otherwise:


The middle sequence traverses the left subtree of the root node;


Access root node;


The middle sequence traverses the right subtree of the root node.


2. Recursive algorithm for pre-order traversal:


If the binary tree is empty, the algorithm ends, otherwise:


Access root node;


The left subtree of the root node is traversed by the anterior sequence;


The pre-order traverses the right subtree of the root node.


3. Recursive algorithm for post-routing traversal:


If the binary tree is empty, the algorithm ends, otherwise:


The left sub-tree of the root node is iterated after order;


The right sub-tree of the root node is iterated after order;


Access the root node.

package com.test6;/** *  two fork Tree  *  *  Description:  1, binary tree construction  2, pre-order recursive traversal, middle sequence recursive traversal, Subsequent recursive traversal of  3, test  *  *  @author  sdc * *  @param  <e> */ public class binarytree<e> {/** *  two fork tree root node  */treenode root;/** *   Tree Size  */private int size;/** *  Specify an element as the root node  *  *  @param   Data */public binarytree (E data)  {this.root = new treenode ((int)  data) ;} /** *  returns the size of the tree  *  *  @return  */public int getsize ()  {return  this.size;} /** *  Building binary Tree  *  *  @param  node *  @param  data */public  void buildbinarytree (Treenode node, int data)  {if  (root == null)  { //  determine if the root node is empty root = new treenode (data);}  else { //  already has root node, create two fork tree If  (Data < node.data)  {if  (node.left == null)  {node.left =  new treenode (data);}  else {buildbinarytree (Node.left, data);}}  else {if  (node.right == null)  { //  If the right subtree does not, consider this node as the right node and create a Node.right  = new treenode (data);}  else {buildbinarytree (Node.right, data);}}} /** *  sequence traversal, pre-order recursive traversal  *  *  Description: After traversing to the node, first output the value of the node, and continue to traverse the left and right subtree, in order: (Root, left-to-starboard)  *   *  @param  node */public void preorder (treenode node)  {if  ( Node == null)  {return;} System.out.print (node.data +  ");p reorder (node.left);p reorder (node.right);} /** *  Recursive traversal  *  *  Description: After traversing to the node, the node is first staged, traversed the left dial hand tree and then output the value of the node, and continue to traverse the right subtree of the root node, order: (left-to-root- > right)  *  *  @param  node */public void midorder (Treenode node)  {if  (node == null) &NBsp {return;} Midorder (Node.left); System.out.print (node.data +  "); Midorder (node.right);} /** *  post-order node recursive traversal  *  *  Description: After traversing to the node, the node is first staged, the left dial hand tree is traversed and the right subtree of the root node is traversed, and the final output sequence is reached. Order: (left-to-right)  *  *  @param  node */public void postorder (TreeNode  node)  {if  (Node == null)  {return;} Postorder (Node.left);p Ostorder (node.right); System.out.print (node.data +  ");} /** *  find values for a node  *  *  @param  data *  @return  */public  Boolean search (Int data)  {boolean isFlag = false; treenode node = root;while  (True)  {if  (data == node.data)  {isflag  = true;break;}  else if  (Data > node.data)  {node = node.right;if  (node = = null)  {break;}}  else {node = node.left;if  (node == null) &nbsP {break;}}} Return isflag;} Public static void main (String[] args)  {int[] array = { 12,  9, 20, 5, 11, 39, 3 }; Binarytree<integer> bt = new binarytree<integer> (Array[0]);for  (int  i = 0; i < array.length; i++)  {bt.buildbinarytree (Bt.root, array[i] );}   Three kinds of traverse bt.preorder (Bt.root); System.out.println (); Bt.midorder (Bt.root); System.out.println (); Bt.postorder (bt.root);} /** *  two fork tree structure  *  *  @author  sdc * */class treenode {int  data; treenode left; Treenode right;public treenode (Int data)  {this.data = data;this.left =  null;this.right = null;}}}


Java two fork tree depth precedence 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.