Java Data Structure-Binary Tree

Source: Internet
Author: User

1. implement various traversal of Binary Trees

Package COM. jzm. stackqueuetree; public class bintree {private final static int max = 50; private object data; private bintree left, right; bintree [] elements = new bintree [Max]; int front; int rear; Public bintree () {} public bintree (Object Data) {This. data = data; left = right = NULL;} public bintree (Object Data, bintree left, bintree right) {This. data = data; this. left = left; this. right = right ;} @ Overridepublic string tostring () {return data. tostring ();} // traverse public static void preorder (bintree parent) {If (parent = NULL) {return;} System in the forward order. out. print (parent. data + ""); preorder (parent. left); preorder (parent. right);} // traverse public static void inorder (bintree parent) {If (parent = NULL) {return;} inorder (parent. left); system. out. print (parent. data + ""); inorder (parent. right );}// Traverse public static void postorder (bintree parent) {If (parent = NULL) {return;} postorder (parent. left); postorder (parent. right); system. out. print (parent. data + "") ;}// traverse public void layerorder (bintree parent) {elements [0] = parent; front = 0; Rear = 1; while (front <rear) {try {If (elements [Front]. data! = NULL) {system. Out. Print (elements [Front]. Data + ""); If (elements [Front]. Left! = NULL) elements [rear ++] = elements [Front]. Left; If (elements [Front]. Right! = NULL) elements [rear ++] = elements [Front]. right; front ++;} // end if} catch (exception e) {break ;}// end while} // calculates the height of the Public int height () tree () {int Height = 0; If (this = NULL) {return-1;} int leftheimpaired = (Left = NULL )? 0: Left. Height (); int rightheimpaired = (Right = NULL )? 0: Right. Height (); Height = leftheimpaired <rightheimpaired? Rightheimpaired: leftheimpaired; return height + 1;} // if the object is not in the tree,-1 is returned; otherwise, the hierarchy of the object in the tree is returned, specify the root node as the first public int level (Object object) {int leveltree = 0; If (this = NULL) {return-1 ;} // specify the root node as the first layer if (object. equals (data) {return 1;} int leftlevel = (Left = NULL )? -1: Left. Level (object); int rightlevel = (Right = NULL )? -1: Right. Level (object); If (leftlevel <0 & rightlevel <0) {return-1;} leveltree = (leftlevel <rightlevel )? Rightlevel: leftlevel; return leveltree + 1;} // change the child of each node in the tree to the public void reflect () {If (this = NULL) return; If (left! = NULL) {left. Reflect ();} If (right! = NULL) {right. reflect ();} bintree temp = left; left = right; Right = temp;} // remove all nodes in the tree and output the removed node public void defoliate () {string innernode = ""; if (this = NULL) return; // if it is a leaf node, remove if (Left = NULL & Right = NULL) {system. out. print (This + ""); Data = NULL; return;} // remove the left subtree if (left! = NULL) {left. defoliate (); left = NULL;} innernode + = This + ""; Data = NULL; // remove the right subtree if (right! = NULL) {right. defoliate (); Right = NULL ;}} public static void main (string [] ARGs) {bintree E = new bintree ("e "); bintree G = new bintree ("G"); bintree H = new bintree ("H"); bintree I = new bintree ("I "); bintree d = new bintree ("D", null, g); bintree F = new bintree ("F", H, I); bintree B = new bintree ("B ", d, e); bintree c = new bintree ("C", F, null); bintree tree = new bintree ("A", B, c); system. out. println ("heigh =" + tree. height (); system. out. println ("preordered Result:"); tree. preorder (tree); system. out. println (""); system. out. println ("intermediate result:"); tree. inorder (tree); system. out. println (""); system. out. println ("result in descending order:"); tree. postorder (tree); system. out. println (""); system. out. println ("layered traversal result:"); tree. layerorder (tree); system. out. println (""); system. out. println ("level (d)" + tree. level ("B "));}}

 

 

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.