Chained storage binary tree (Java)

Source: Internet
Author: User

chain store A full binary tree code:

Binary tree (chained storage) import Java.util.arraydeque;import java.util.linkedlist;import java.util.list;import java.util.Queue; Import Java.util.stack;public class Mytree {static node Root;class node{private node lchild;private node rchild;private in T data; Node (int d) {data=d;} Public Node Getlchild () {return lchild;} public void Setlchild (Node lchild) {this.lchild = Lchild;} Public Node Getrchild () {return rchild;} public void Setrchild (Node rchild) {this.rchild = Rchild;} public int GetData () {return data;} public void SetData (int data) {this.data = data;}} Create two fork Tree Node creattree (int[] a) {list<node> nodelist = new linkedlist<> (); for (int i=0;i<a.length;i++) { Nodelist.add (New Node (A[i));} for (int i=0;i<a.length/2-1;i++) {nodelist.get (i). Setlchild (Nodelist.get (2*i+1)); Nodelist.get (i). SetrChild ( Nodelist.get (2*i+2));} int index = a.length/2-1;nodelist.get (index). Setlchild (Nodelist.get (2*index+1)); if (a.length%2==1) {Nodelist.get ( Index). Setrchild (Nodelist.get (2*index+2));} Root=nodelist.get (0); return rOot;} First order traversal of void preorder (Node N) {if (n==null) {return;} System.out.print (N.getdata () + "");p Reorder (N.getlchild ());p Reorder (N.getrchild ());} Ask for height int getheight (Node N) {if (n==null) {return 0;} Else{intleft = GetHeight (N.getlchild ()), int right = GetHeight (N.getrchild ()), return 1+math.max (left, right);//each upward returns a layer , plus a}}//depth-first traversal (Dfs) void Dfsorder () {if (root==null) {System.out.println ("empty Tree"); Stack<node>stack=new stack<> (); Stack.push (root); while (!stack.isempty ()) {node node = Stack.pop (); System.out.print (Node.getdata () + ""); if (Node.getrchild ()!=null) {Stack.push (Node.getrchild ());} if (Node.getlchild ()!=null) {Stack.push (Node.getlchild ());}}} Breadth-First traversal (BFS) void Bfsorder () {if (root==null) {System.out.println ("empty Tree");} Queue<node>queue=new arraydeque<> (); Queue.add (root); while (!queue.isempty ()) {Node n = queue.remove (); System.out.print (N.getdata () + ""); if (N.getlchild ()!=null) {Queue.add (N.getlchild ());} if (N.getrchild ()!=null) {Queue.add (N.getrchild ());}}} Find the width of the tree int getweight(Node N) {if (n==null) {return 1;} Queue<node>queue = new Arraydeque<> (), Queue.add (n), int maxsize=1;//maximum length while (true) {int len = queue.size () ///Current layer node number if (len==0) {break;//Jump Out of}while (len>0) {//If there is a current layer node nodes = Queue.poll (); Len--;if (Node.getlchild ()! = NULL) {Queue.add (Node.getlchild ());} if (Node.getrchild ()!=null) {Queue.add (Node.getrchild ());}} MaxSize = Math.max (MaxSize, Queue.size ());//The largest width of the new queue and the size of the new queue to take a larger value}return maxsize;}}

Test:

public static void Main (string[] args) {int[] arr = {1,2,3,4,78,6,7,8,9}; Mytree m = new Mytree (); Node node = m.creattree (arr); M.preorder (node); System.out.println (); System.out.println ("The tree height is" +m.getheight (root)); System.out.println ("depth-first traversal:"); M.dfsorder (); System.out.println (); System.out.println ("Breadth-first traversal:"); M.bfsorder (); System.out.println (); System.out.println ("The width of the tree is:" +m.getweight (Root));}

Test results:

Chained storage binary tree (Java)

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.