/* * Java Implementation two fork tree */public class BinaryTree {int treeNode; BinaryTree Lefttree; BinaryTree righttree;public BinaryTree (int Data) {//TODO auto-generated constructor Stubtreenode=data;lefttree=null; Righttree=null;} public void Insert (BinaryTree node,int data) {if (data >node.treenode) {if (node.righttree==null) {righttree=new BinaryTree (data);} Else{this.insert (node.righttree, data);}} Else{if (node.lefttree==null) {lefttree=new BinaryTree (data);} Else{this.insert (node.lefttree, data);}}}
/* * to define binary tree, first order traversal, sequence traversal, post-order traversal */public class Binarytreeorder {public static void Preorder (BinaryTree root) {///first-order traversal if (root! = null) {System.out.print (Root.treenode + "-");p reorder (root.lefttree); Preorder (Root.righttree);}} public static void Inorder (BinaryTree root) {//middle order traversal if (root! = null) {inorder (root.lefttree); System.out.print (Root.treenode + "--"); Inorder (Root.righttree);}} public static void Postorder (BinaryTree root) {//post-traversal if (root! = null) {postorder (root.lefttree);p Ostorder (root.righttr EE); System.out.print (Root.treenode + "---");}} public static void Main (string[] args) {int[] array = {12, 76, 35, 22, 16, 48, 90, 46, 9, 40}; BinaryTree root = new BinaryTree (array[0]); Create a two-fork tree for (int i = 1; i < Array.Length; i++) {Root.insert (root, Array[i]) and//Insert data}system.out.println ("Sequential traversal:") into the binary tree;p ReOrder (root); System.out.println (); System.out.println ("Middle sequence Traversal:"); inorder (root); System.out.println (); System.out.println ("post-post traversal:");p Ostorder (root);}}
Implementation and traversal of the Java Two fork tree