Conversion: binary tree structure implemented in Java

Source: Internet
Author: User

Public class binarytreetest {private binarytree root = NULL; Public binarytreetest () {Init ();} private void Init () {int data [] = {12, 11, 34, 45, 67, 38, 56, 43, 22, 8}; root = new binarytree (data [0]); system. out. println (":"); system. out. println ("------------------------------------"); system. out. println (data [0] + ": Root"); For (INT I = 1; I <data. length; I ++) {system. out. print (data [I] + ":"); root. inserttree (root, data [I]);} system. out. println ("----------------------------------");} public void serach (INT key) {If (searchkey (root, key) {system. out. println ("have found it:" + key);} else {system. out. println ("not found it:" + key) ;}} private Boolean searchkey (binarytree root, int key) {If (root = NULL) {return false ;} else if (root. data = Key) {return true;} else if (Key> = root. data) {return searchkey (root. rightpoiter, key);} return searchkey (root. leftpoiter, key);} class binarytree {int data; binarytree leftpoiter; binarytree rightpoiter; binarytree (INT data) {This. data = data; leftpoiter = NULL; rightpoiter = NULL;} private void inserttree (binarytree root, int data) {If (data> = root. data) {If (root. rightpoiter = NULL) {system. out. println ("-> New rightpoiter"); root. rightpoiter = new binarytree (data);} else {system. out. print ("-> rightpoiter"); inserttree (root. rightpoiter, data) ;}} else {If (root. leftpoiter = NULL) {system. out. println ("-> New leftpoiter"); root. leftpoiter = new binarytree (data);} else {system. out. print ("-> leftpoiter"); inserttree (root. leftpoiter, data) ;}}} public static void main (string ARGs []) {binarytreetest B = new binarytreetest (); int key = 8; B. serach (key );}}

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.