Java two fork tree recursion, non-recursive traversal, height, number of nodes, leaf node points

Source: Internet
Author: User

Import Java.util.linkedlist;import Java.util.queue;import Java.util.stack;public class Main {public static class Tre     enode<t>{T data;     Treenode<t> Left=null;         Treenode<t> Right=null; Public TreeNode () {} public TreeNode (T data) {This.data=data;}     Public TreeNode (T-Data, TreeNode-left, TreeNode-right) {super (); this.data = Data;this.left = Left;this.right = right;}            } public static class binarytree<t>{/** two fork Tree root node */private treenode<t> root;      Public BinaryTree () {} public BinaryTree (treenode<t> root) {this.root = root; }/* Public treenode<integer> Createbinarytree () {treenode<integer> e = new Treenode&lt      ;integer> (5);      treenode<integer> g = new treenode<integer> (7);      Treenode<integer> h = new treenode<integer> (8);      treenode<integer> L = new treenode<integer> (12); treenode<integer&Gt      m = new treenode<integer> (13);      treenode<integer> n = new treenode<integer> (14);      treenode<integer> k = new treenode<integer> (one, n, null);      Treenode<integer> j = new Treenode<integer> (Ten, L, M);      treenode<integer> i = new Treenode<integer> (9, J, K);      Treenode<integer> d = new treenode<integer> (4, NULL, g);      treenode<integer> f = new treenode<integer> (6, H, i);      Treenode<integer> B = New Treenode<integer> (2, D, e);               treenode<integer> C = New treenode<integer> (3, F, null);      treenode<integer> root = new Treenode<integer> (1, B, c);      return root;      }*///Recursive pre-order public void preorder (treenode<t> root) {if (root!=null) {visit (root);      Preorder (Root.left);      Preorder (root.right);         }}/* Non-recursive pre-order: for any node p:1) Access node p, and node p into the stack; 2) Determine if the left child of the node P is empty, and if it is empty, takeThe top node of the stack is placed and the right child of the top node of the stack is set to the current node p, which loops to 1), and if not empty, the left child of P is placed as the current node p;         3) until p is null and the stack is empty, the traversal ends. */public void Nonrecursivepreorder (treenode<t> root) {stack<treenode<t>> s=new Stack<treen      Ode<t>> (); if (root!=null) {s.push (root);//first put the root node into the stack while (!s.isempty ()) {//while stack is not empty treenode<t> node=s.pop ();//Popup      Stack visit (node); if (node.right!=null) S.push (node.right);//Put the right node into the stack if (node.left!=null) S.push (node.left); Put left node into stack}}}//recursive middle order public void inorder (treenode<t> root) {if (RO      Ot!=null) {inorder (root.left);      Visit (Root);      Inorder (Root.right); }}//Non-recursive in-sequence traversal: For any node p, put it into the stack, and then search along its left subtree until the node with no left child is found, at which point the node appears at the top of the stack,//But it cannot be stacked and accessed at this time, so its right child is also visited 。          So next follow the same rules to the right subtree of the same processing, when the end of the access to their right child, the node appears on the top of the stack, it can be out of the stack and access. public void Nonrecursiveinorder (treenode<t> root) {stack<treenode<t>> stack=new StacK<treenode<t>> ();          Treenode<t> Node=root; while (node!=null| |!          Stack.isempty ()) {//Zuozi always into the stack while (node!=null) {//Always find node Zuozi is empty node stack.push (nodes);          Node=node.left;          } node=stack.pop ();///Zuozi is empty is to access this node visit (nodes);          node=node.right;//is looking for the right subtree of the Node}}//recursive post-public void Postorder (treenode<t> root) {      if (root!=null) {postorder (root.left);      Postorder (Root.right);      Visit (Root); }}//Non-recursive post-traversal of public void Nonrecursivepostorder (treenode<t> root) {treenode<          T> Node=root; Treenode<t> prenode=null;//Record the right node before traversing stack<treenode<t>> stack=new STACK&LT;TREENODE&LT;T&GT;&G          t; (); while (node!=null| |!          Stack.isempty ()) {/* Zuozi always into the stack */while (node!=null) {Stack.push (node);                   Node=node.left;     }     Node=stack.peek ();//Get the top node of the stack but not the stack/* If the right node is empty, or the right node has been traversed before, print the root node */if (node.right==null| |          Node.right==prenode) {Visit (node);          Node=stack.pop ();          Prenode = node;                  Node=null;          } else{Node=node.right;          }}}//Hierarchy traverse public void Leveltraverse (treenode<t> root) { Queue is an interface, can not be directly instantiated, generally use its implementation class LinkedList as a queue,//queue implementation class and LinkedList, Priorityqueue, Linkedblockingqueue , Blockingqueue, Arrayblockingqueue, Linkedblockingqueue, Priorityblockingqueue queue<treenode<t>> q          Ueue=new linkedlist<treenode<t>> ();          Treenode<t> Node=root;          Queue.offer (node);//The queue uses an offer to add elements/* to each node first out of the queue and then to the left and right nodes into the queue */while (!queue.isempty ()) {          Node=queue.poll ();//queue with poll out queue if (Node!=null) {Visit (node); Left and right node into queue Queue.offer(Node.left);          Queue.offer (Node.right); }}}//recursive tree height public int treeheight (TREENODE&L T          T> root) {if (root==null) {return 0;          } else{int Lefttreeheight=treeheight (root.left);          int Righttreeheight=treeheight (root.right);          Return lefttreeheight>righttreeheight?lefttreeheight+1:righttreeheight+1;          }}//Recursive total number of nodes public int treenodes (treenode<t> root) {if (root==null) {          return 0;          } else{int Lefttreenodes=treenodes (root.left);          int Righttreenodes=treenodes (root.right);          return lefttreenodes+righttreenodes+1;          }}//recursive total number of leaf nodes public int treeleaf (treenode<t> root) {if (root==null) {          return 0;          } else{int Lefttreeleaf=treeleaf (root.left); INT Righttreeleaf=treeleaf (root.right);          if (root.left==null&&root.right==null) {return lefttreeleaf+righttreeleaf+1;          } else{return lefttreeleaf+righttreeleaf;      }}} public void visit (treenode<t> root) {System.out.print (root.data+ ""); }} public static void Main (string[] args) {treenode<integer> e = new Treenode<intege  R> (5);  treenode<integer> g = new treenode<integer> (7);  Treenode<integer> h = new treenode<integer> (8);  treenode<integer> L = new treenode<integer> (12);  treenode<integer> m = new treenode<integer> (13);  treenode<integer> n = new treenode<integer> (14);  treenode<integer> k = new treenode<integer> (one, n, null);  Treenode<integer> j = new Treenode<integer> (Ten, L, M);  treenode<integer> i = new Treenode<integer> (9, J, K); TreEnode<integer> d = new treenode<integer> (4, NULL, g);  treenode<integer> f = new treenode<integer> (6, H, i);  Treenode<integer> B = New Treenode<integer> (2, D, e);       treenode<integer> C = New treenode<integer> (3, F, null);    treenode<integer> root = new Treenode<integer> (1, B, c);  Binarytree<integer> tree=new binarytree<integer> (root);  SYSTEM.OUT.PRINTLN ("Recursive pre-order traversal of binary tree results:");  Tree.preorder (root);  System.out.println ();  SYSTEM.OUT.PRINTLN ("Non-recursive pre-order traversal of binary tree results:");  Tree.nonrecursivepreorder (root);    System.out.println ();  SYSTEM.OUT.PRINTLN ("Recursive sequential traversal of binary tree results:");  Tree.inorder (root);  System.out.println ();  SYSTEM.OUT.PRINTLN ("Non-recursive sequential traversal of binary tree results:");  Tree.nonrecursiveinorder (root);    System.out.println ();  SYSTEM.OUT.PRINTLN ("Recursive sequential traversal of binary tree results:");  Tree.postorder (root);  System.out.println ();  SYSTEM.OUT.PRINTLN ("Non-recursive sequential traversal of binary tree results:");  Tree.nonrecursivepostorder (root);    System.out.println ();  SYSTEM.OUT.PRINTLN ("Hierarchical traversal of Binary tree results:"); TRee.leveltraverse (root);    System.out.println ();  System.out.println ("Recursion to find the height of the binary tree:" + tree.treeheight (root));  SYSTEM.OUT.PRINTLN ("Recursive Binary tree Node count:" + tree.treenodes (root));         SYSTEM.OUT.PRINTLN ("Recursive Binary Tree leaf node:" +tree.treeleaf (Root)); }}


Output

Recursive pre-order traversal binary Tree results: 1 2 4 7 5 3 6 8 9 10 12 13 11 14 non-recursive pre-sequence traversal binary tree results: 1 2 4 7 5 3 6 8 9 10 12 13 11 14 Recursive middle sequence traversal binary tree results: 4 7 2 5 1 8 6 12 10 13 9 14 11 3 non-recursive mid-sequence traversal binary tree results: 4 7 2 5 1 8 6 12 10 13 9 14 11 3 Recursive sequential traversal binary tree results: 7 4 5 2 8 12 13 10 14 11 9 6 3 1 non-recursive post-traversal binary tree results: 7 4 5 2 8 12 13 10 14 11 9 6 3 1-Step traversal binary Tree results: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 recursion to find the height of the binary tree: 6 node of the recursive binary tree: 14 recursive binary tree leaf nodes 6






Java two fork tree recursion, non-recursive traversal, height, number of nodes, leaf node points

Related Article

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.