Creation of a Java two fork tree and implementation of various functions

Source: Internet
Author: User

Directly on the code, the code is very clear.

Package Btree;public class BTree {private node Root;private class Node {private node Lchild;private node Rchild;private in T data;public Node (int data) {This.lchild = Null;this.rchild = Null;this.data = data;}} Public BTree () {root = null;} Public Node GetNode () {return root;}  public void Createtree (node node, int data) {if (root = null) {root = new Node (data);} else {if (data < Node.data) {if (Node.lchild = = null) {Node.lchild = new node (data);} else {createtree (node.lchild, data);}} else {if (Node.rchild = = null) {Node.rchild = new node (data)} else {Createtree (node.rchild, data);}}}} /* Pre-sequence traversal */public void Preorder (node node) {if (node! = null) {System.out.print (Node.data + ""); Preorder (Node.lchild); Preorder (Node.rchild);}} /* Middle sequence traversal */public void Inorder (node node) {if (node! = null) {inorder (node.lchild); System.out.print (Node.data + ""); Inorder (Node.rchild);}} /* post-traverse */public void Tailorder (node node) {if (node! = null) {Tailorder (node.lchild); Tailorder (Node.rchild); System.out.print (NODE.DATA + "");}} /* Two fork tree height */public int Depth (node node) {int dl = 0,DR =0;if (node!=null) {dl = Depth (node.lchild);d r = Depth (Node.rchild); if (d L&GT;DR) return Dl+1;return dr+1;} return 0;} /* Number of nodes */public Int (node node) {if (node!=null) {return point (Node.lchild) +point (Node.rchild) +1;} return 0;} /* Number of leaf nodes */public int leaf (node node) {if (node==null) return 0;boolean flag = (node.lchild==null&&node.rchild== NULL), if (flag) return leaf (node.lchild) +leaf (node.rchild) +1;return leaf (node.lchild) +leaf (node.rchild);}    /* Number of nodes with a degree of */public int onedegree (node node) {if (node==null) return 0; Boolean flag = (node.lchild==null&&node.rchild!=null) | |    (Node.lchild!=null&&node.rchild==null);    if (flag) return Onedegree (Node.lchild) +onedegree (node.rchild) +1; Return Onedegree (Node.lchild) +onedegree (node.rchild);}    /* Number of nodes with two */public int twodegree (node node) {if (node==null) return 0;    Boolean flag = (node.lchild!=null&&node.rchild!=null); if (flag) return Twodegree (Node.lchild) +twoDegree (node.rchild) +1; Return Twodegree (Node.lchild) +twodegree (Node.rchild);}}

  

Package Btree;public class Btreedemo {/** * @param args */public static void main (string[] args) {int [] a ={4,3,2,6,11,8, 9,10,1,5};//int [] a = {1,2,3,4,5}; BTree BT = new BTree (); for (int i=0;i<a.length;i++) {bt.createtree (Bt.getnode (), a[i]);} System.out.print ("Pre-order Traversal:") bt. Preorder (Bt.getnode ()); System.out.println (); System.out.print ("Middle sequence Traversal:") bt. Inorder (Bt.getnode ()); System.out.println (); System.out.print ("post-post traversal:") bt. Tailorder (Bt.getnode ()); System.out.println (); System.out.println ("Height of the binary tree:" +BT. Depth (Bt.getnode ())); System.out.println ("Number of nodes:" +BT. Point (Bt.getnode ())); System.out.println ("Number of leaf nodes:" +BT. Leaf (Bt.getnode ())); System.out.println ("Number of nodes out of one:" +bt.onedegree (Bt.getnode ())); System.out.println ("Number of nodes with a degree of two:" +bt.twodegree (Bt.getnode ()));}}
Pre-sequence Traversal: 4 3 2 1 6 5 11 8 9 10 Sequence Traversal: 1 2 3 4 5 6 8 9 10 11 post-order Traversal: 1 2 3 5 10 9 8 11 6 42 Fork Tree Height: 6 node Number: 10 leaf node: 3 out of a node: 5 out of two Number of points: 2

  

  

Creation of a Java two fork tree and implementation of various functions

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.