Java creates two fork trees and recursively traverses the binary tree

Source: Internet
Author: User

Binary Tree Class Code:

Package Binarytree;import Linkqueue.  Linkqueue;public class BinaryTree {class Node{public Object Data;public node lchild;public node rchild;public node (object Data) {This.data = Data;this.lchild = Null;this.rchild = null;}} Root node private node root = null;private node node = null;/** * Create Tree * * created in a fully binary tree format (subtree does not exist with 0 padding), * Numbering each node in a full binary tree starting with 0 * Then the left child of the first node is numbered 2*i+1 and the right child is 2*i+2. * * */void Createtree (String strtree) {linkqueue lqueue = new Linkqueue (); Lqueue.initqueue ();/** * The number of nodes in the layer I of a complete binary tree is 1th to I -Number of all nodes on layer 1 and * so the number of parent nodes is up to n-1, n indicates the number of nodes * */for (int parentindex =0; Parentindex<strtree.split (""). Length/2; parentindex++) {if (root = null) {root= new node (Strtree.split ("") [Parentindex]);//left Child Root.lchild = new Node ( Strtree.split ("") [parentindex*2+1]); Lqueue.enqueue (root.lchild);//Right child Root.rchild = new Node (Strtree.split ("") [ PARENTINDEX*2+2]); Lqueue.enqueue (root.rchild);} Else{if (!lqueue.isempty () && parentindex*2+1<strtree.split (""). Length)//Queue not empty {node = (node) Lqueue.dequeue (); if (pareNtindex*2+1<strtree.split (""). Length) {//left child node.lchild = new Node (Strtree.split ("") [parentindex*2+1]); Lqueue.enqueue (node.lchild);} if (Parentindex*2+2<strtree.split ("). Length) {//right child Node.rchild = new Node (Strtree.split (" ") [parentindex*2+2]); Lqueue.enqueue (Node.rchild);}} Else{return;}}}} /** * First Order Traversal binary tree * */void preordertraverse (node node) {if (node = = null) {return;} Visit (node);p reordertraverse (node.lchild);p reordertraverse (node.rchild);} /** * Middle Sequence traversal binary tree * */void inordertraverse (node node) {if (node = = null) {return;} Inordertraverse (Node.lchild); Visit (node); Inordertraverse (node.rchild);} /** * post-traverse binary tree * */void postordertraverse (node node) {if (node = = null) {return;} Postordertraverse (Node.lchild);p ostordertraverse (node.rchild); Visit (node);} /** * Print binary tree * */public void print () {System.out.print ("Sequential traversal:");p reordertraverse (root); System.out.print ("\ n sequence traversal:"); Inordertraverse (root); System.out.print ("\ n post-traversal:");p ostordertraverse (root);} /** * Access node * */private void Visit (node node) {if (!node.data.equals ("0") {System.out.print (node.data+ "");}}} 

Test the code (take the Expression a+b* (c-d)-e/f in the data structure as an example):

Package Binarytree;public class Binarytreemain {public static void main (string[] args) {Binarytree binarytree = new Binary Tree (); String strtree= "-+/A * E F 0 0 b-0 0 0 0 0 0 0 0 0 0 C D";//0 represents a position without a value binarytree.createtree (Strtree); Binarytree.print ( );}}

Operation Result:

Java creates two fork trees and recursively traverses the binary tree

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.