Build Binary Tree in java, recursive/non-recursive first-order traversal, recursive/non-recursive middle-order traversal, and hierarchical Traversal

Source: Internet
Author: User

import java.util.LinkedList;import java.util.Scanner;import java.util.Stack;//structure of binary treeclass BiTree {BiTree lchild;BiTree rchild;String data;}public class BiTreeTest {static Scanner scanner = new Scanner(System.in);// test case: a b c # # d e # g # # f # # #static BiTree createBiTree(BiTree root) {String data = scanner.next();if (data.equals("#")) {return null;} else {root = new BiTree();root.data = data;root.lchild = createBiTree(root.lchild);root.rchild = createBiTree(root.rchild);return root;}}// preOrder recursive traversestatic void preOrderRecur(BiTree root) {if (root != null) {System.out.print(root.data + " ");preOrderRecur(root.lchild);preOrderRecur(root.rchild);}}// inOrder recursive traversestatic void inOrderRecur(BiTree root) {if (root != null) {inOrderRecur(root.lchild);System.out.print(root.data + " ");inOrderRecur(root.rchild);}}// preOrder in non-recursivestatic void preOrder(BiTree root) {Stack<BiTree> stack = new Stack<BiTree>();BiTree cur;stack.push(root);while (!stack.empty()) {while ((cur = stack.peek()) != null) {System.out.print(cur.data + " ");stack.push(cur.lchild);}cur = stack.pop();if (!stack.empty() && (cur = stack.pop()) != null) {stack.push(cur.rchild);}}}// inOrder in non-recursivestatic void inOrder(BiTree root) {Stack<BiTree> stack = new Stack<BiTree>();BiTree cur;stack.push(root);while (!stack.empty()) {while ((cur = stack.peek()) != null) {stack.push(cur.lchild);}stack.pop();if (!stack.empty() && (cur = stack.pop()) != null) {System.out.print(cur.data + " ");stack.push(cur.rchild);}}}// level traverse,use LinkedList instead of queue data structurestatic void levelTraverse(BiTree root) {LinkedList<BiTree> list = new LinkedList<BiTree>();BiTree cur;list.add(root);while (list.size() != 0) {cur = list.removeFirst();if (cur != null) {System.out.print(cur.data + " ");}if (cur.lchild != null) {list.add(cur.lchild);}if (cur.rchild != null) {list.add(cur.rchild);}}}public static void main(String[] args) {BiTree root = null;root = createBiTree(root);// preOrderRecur(root);// inOrderRecur(root);// inOrder(root);levelTraverse(root);}}

 

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.