Common Operations for Binary Trees implemented by Java [previous order creation, previous and later recursive non-recursive traversal and sequence traversal], binary tree Recursion
This example describes common Binary Tree operations implemented by Java. We will share this with you for your reference. The details are as follows:
Import java. util. arrayDeque; import java. util. queue; import java. util. stack; // build a binary tree. The previous and later recursive non-recursive traversal of the sequence traversal // Node class Node {int element; Node left; Node right; public Node () {} public Node (int element) {this. element = element ;}/// BinaryTreepublic class Tree {// creat tree from array public static Node creatTree (int [] data, int I) {if (I> = data. length | data [I] =-1) return null; Node temp = ne W Node (data [I]); temp. left = creatTree (data, I * 2 + 1); temp. right = creatTree (data, I * 2 + 2); return temp;} // pre-order traversal recursion public static void pre (Node temp) {if (temp = null) return; System. out. print (temp. element + ""); pre (temp. left); pre (temp. right);} // recursive public static void mid (Node temp) {if (temp = null) return; mid (temp. left); System. out. print (temp. element + ""); mid (temp. r Ight);} // recursive public static void last (Node temp) {if (temp = null) return; last (temp. left); last (temp. right); System. out. print (temp. element + "");} // traverse non-recursive public static void pre1 (Node temp) {Stack <Node> stack = new Stack <> (); while (temp! = Null |! Stack. isEmpty () {while (temp! = Null) {stack. push (temp); System. out. print (temp. element + ""); temp = temp. left;} if (! Stack. isEmpty () {temp = stack. pop (). right ;}}// the nonrecursive public static void mid1 (Node temp) {Stack <Node> stack = new Stack <> (); while (temp! = Null |! Stack. isEmpty () {while (temp! = Null) {stack. push (temp); temp = temp. left;} if (! Stack. isEmpty () {temp = stack. pop (); System. out. print (temp. element + ""); temp = temp. right ;}}// last1 post-order traversal non-recursive public static void last1 (Node temp) {Stack <Node> stack = new Stack <> (); stack <Node> stack2 = new Stack <> (); while (temp! = Null |! Stack. isEmpty () {while (temp! = Null) {stack. push (temp); stack2.push (temp); temp = temp. right;} if (! Stack. isEmpty () {temp = stack. pop (). left ;}} while (! Stack2.isEmpty () System. out. print (stack2.pop (). element + "");} // ceng sequence traversal public static void ceng (Node temp) {if (temp = null) return; queue <Node> queue = new ArrayDeque <> (); queue. offer (temp); while (! Queue. isEmpty () {temp = queue. poll (); System. out. print (temp. element + ""); if (temp. left! = Null) queue. offer (temp. left); if (temp. right! = Null) queue. offer (temp. right) ;}}// Demo public static void main (String [] args) {int [] array = {1, 2, 3, 4, 5, 6, 7, -1,-1, 10,-1,-1, 13}; Node tree = creatTree (array, 0); System. out. println ("helper house Test Result:"); pre (tree); System. out. println (); pre1 (tree); System. out. println (); mid (tree); System. out. println (); mid1 (tree); System. out. println (); last (tree); System. out. println (); last1 (tree); System. out. println (); ceng (tree );}}
Running result: