Java implements Binary Trees.

Source: Internet
Author: User

I. Create a node class:

Public class node {<br/> Public node leftnode; <br/> Public node rightnode; <br/> int data; </P> <p>/** <br/> * @ author hbliu <br/> * @ Param newdata <br/> */<br/> Public node (int newdata) {<br/> This. data = newdata; <br/> This. leftnode = NULL; <br/> This. rightnode = NULL; <br/>}</P> <p>}

Ii. Create a tree class:

Package COM. test. tree; </P> <p> public class binarytree {</P> <p> private node root; </P> <p>/** <br/> * function: Create an empty binary tree. <br/> */<br/> Public binarytree () {<br/> root = NULL; <br/>}</P> <p>/** <br/> * @ author hbliu <br/> * @ Param data value to be inserted <br/> * benefits: insert data to the tree <br/> */<br/> Public void insert (INT data) {<br/> This. root = insert (root, data ); <br/>}</P> <p>/** <br/> * @ Param node: the current node is the root node. <Br/> * @ Param data: the value to be inserted <br/> * @ return <br/> * function: insert the value into the binary tree, when the current node is smaller than or equal to the left value of the current node inserted in the current node, <br/> * If data larger than the current node is inserted in the right corner of the current node, recursive comparison starting from the root node each time <br/> */<br/> private node insert (node, int data) {<br/> If (node = NULL) {<br/> node = new node (data); <br/>} else if (node. DATA> = data) {<br/> node. leftnode = insert (node. leftnode, data); <br/>}else {<br/> node. rightnode = insert (node. rightnode, data); <br/>}</P> <p> return node; <br/>}</P> <p>/** <br/> * @ author hbliu <br/> * @ Param data to be inserted <br/> * benefits: build a binary tree <br/> */<br/> private void buildtree (INT [] arrayddta) {<br/> for (INT I = 0; I <arrayddta. length; I ++) {<br/> insert (arrayddta [I]); <br/>}</P> <p> Public void buildtree () {<br/> int arraydata [] = {10, 3, 1, 2, 4, 7, 12, 15 }; <br/> buildtree (arraydata ); <br/>}</P> <p>/** <br/> * @ author hbliu <br/> */<br/> Public void printtree () {<br/> printtree (this. root); <br/> system. out. println (); <br/>}</P> <p>/** <br/> * @ author hbliu <br/> * @ Param node <br/> */<br/> Public void printtree (node) {</P> <p> If (node = NULL) {<br/> return; <br/>}< br/> printtree (node. leftnode); <br/> system. out. print (node. data + ""); <br/> printtree (node. rightnode); <br/>}</P> <p> Public static void main (string [] Arg) {<br/> binarytree bt = new binarytree (); <br/> BT. buildtree (); <br/> BT. printtree (); <br/>}</P> <p >}< br/>

3. Think carefully about the application of recursion in the 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.