Two-tree sorting problem in Java

Source: Internet
Author: User

Original: Reproduced please indicate the source

Objective: To implement two binary tree sorting algorithm with Java

Thought: Using the object-oriented idea in Java, namely:

Tree: Class

The roots of Tree:root//static belong to every tree

Left node Tree:leftson

Right node Tree:rightson

Father Node Tree:father

On the Code tree class:

 Packagecom.cissst.dom1;/*** Binary Tree *@authorPhoebe*/ Public classTree { PublicInteger data;//the value of each node     Public StaticTree Root;//root node (with and only one)     PublicTree father;//parent Node     PublicTree Leftson;//left dial hand node     PublicTree Rightson;//Right child node//whether the Zo Shu is empty     Public BooleanHasleftson () {returnleftson!=NULL; }    //whether the right tree is empty     Public BooleanHasrightson () {returnrightson!=NULL; }        //inserting Nodes     Public voidInsert (Integer data,tree father) {/*** Thought: Let the value in data and root compare, greater than 0 insert to the right, less than 0 insert left, plan to use recursive thought*/        //equals Root.data        if(Data.compareto (Father.data) ==0){            return; }        //Greater than Root.data        if(Data.compareto (Father.data) >0){            //parent node does not have right node            if(!Father.hasrightson ()) {Father.rightson=NewTree ();//generate a right nodeFather.rightson.data=data;//Assigning a value to the right nodeFather.rightson.father=father;//specify who is the father of the right node}Else{Insert (Data,father.rightson); }        }                //less than ibid .        if(Data.compareto (Father.data) <0){            //parent node has no left node            if(!Father.hasleftson ()) {Father.leftson=NewTree ();//generate a right nodeFather.leftson.data=data;//Assigning a value to the right nodeFather.leftson.father=father;//specify who is the father of the right node}Else{Insert (Data,father.leftson); }        }    }        /*** Overall insert operation * 1. Determine if there is a root, no words add the data to the root of the tree * 2. There is an overloaded method of the root call insert to determine whether to insert to left son or right son *@paramData*/     Public voidInsert (Integer data) {if(root==NULL) {root=NewTree (); Root.data=data; return; }Else{Insert (data,root); }    }        }

Test code:

 Packagecom.cissst.dom1;/*** Tree Test code *@authorPhoebe*/ Public classTreetest { Public Static voidMain (string[] args) {treetest TT=Newtreetest (); Tree Tree=NewTree (); Tree.insert (5); Tree.insert (2); Tree.insert (3); Tree.insert (1); Tree.insert (8); Tree.insert (3);        Tt.outputtree (Tree.root);        Tt.getminvalue (Tree.root);    Tt.getmaxvalue (Tree.root); }    //traversing a collection in a tree     Public voidOutputtree (tree) {System.out.print (Tree.data+" "); if(Tree.hasleftson ()) {outputtree (Tree.leftson); }        if(Tree.hasrightson ()) {outputtree (Tree.rightson); }    }    //find the smallest value in the tree     Public voidGetminvalue (tree) {if(Tree.hasleftson ()) {getminvalue (Tree.leftson); }Else{System.out.println ("Min" +tree.data); }    }    //find the largest value in the tree     Public voidGetmaxvalue (tree) {if(Tree.hasrightson ()) {getmaxvalue (Tree.rightson); }Else{System.out.println ("Maximum value" +tree.data); }    }        }

Two-tree sorting problem in Java

Related Article

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.