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