Implementation and traversal of the Java Two fork tree

Source: Internet
Author: User

/* * 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

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.