Data structure and algorithm analysis java--Tree 2 (binary tree type)

Source: Internet
Author: User

1. Two fork Find tree

  Binary search trees (binary search tree)/ordered binary tree (ordered binary tree)/Sort binary trees (sorted binary)

1). If the left subtree of any node is not empty, then the value of all nodes on the left subtree is less than the value of its root node;

2). If the right subtree of any node is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

3). The left and right subtree of any node is also a two-fork lookup tree, respectively.

4). No nodes with key values equal (no duplicate nodes).

 Public classBinarysearchtree<anytypeextendscomparable<?SuperAnytype>>{    Private Static classBinarynode<anytype>{binarynode (AnyType theelement) { This(Theelement,NULL,NULL); } binarynode (AnyType theelement, Binarynode<AnyType> LT, binarynode<anytype>) {element=theelement; Left=lt; right=RT;        } AnyType element; Binarynode<AnyType>Left ; Binarynode<AnyType>Right ; }        PrivateBinarynode<anytype>Root; //constructor Function     PublicBinarysearchtree () {root=NULL; }         Public voidMakeempty () {root=NULL; }         PublicBooolean IsEmpty () {returnroot==NULL; }         Public Booleancontains (AnyType x) {returncontains (x,root); }         PublicAnyType findmin () {if(IsEmpty ())Throw Newunderflowexception (); returnfindmin (root). element; }         PublicAnyType Findmax () {if(IsEmpty ())Throw Newunderflowexception (); returnFindmax (root). element; }         Public voidInsert (AnyType x) {root=Insert (x,root); }         Public voidRemove (AnyType x) {root=Remove (x,root); }         Public voidPrinttree () {if(IsEmpty ()) System.out.println ("Empty Tree"); ElsePrinttree (root); }            //two contains operation of a fork tree    Private BooleanContains (AnyType x, binarynode<anytype>t) {                if(t==NULL)            return false; //the CompareTo method of the comparable interface compares two values        intcompareresult=X.compareto (t.element); if(compareresult<0)            returncontains (x,t.left); Else if(compareresult>0)            returncontains (x,t.right); Else            return true; }            //Find Minimum node    PrivateBinarynode<anytype> Findmin (binarynode<anytype>t) {        if(t==NULL)             return NULL; Else if(t.left==NULL)            returnT; returnfindmin (T.left); }        //Find Maximum node    PrivateBinarynode<anytype> Findmax (binarynode<anytype>t) {        if(t! =NULL)             while(t.right!=NULL) T=T.right; returnT; }        //Insert inserts to return a reference to the new tree root    Privatebinarynode<anytype> Insert (AnyType x, binarynode<anytype>t) {        if(t==NULL)            return NewBinarynode<anytype> (x,NULL,NULL); intcompareresult=X.compareto (t.element); if(compareresult<0) T.left=Insert (x,t.left); Else if(Compareresult >0) T.right=Insert (x,t.right); Else            ; returnT; }        //Delete a node    Privatebinarynode<anytype> Remove (AnyType x, binarynode<anytype>t) {        if(t==NULL)            returnT; intcompareresult=X.compareto (t.element); if(compareresult<0) T.left=Remove (x,t.left); Else if(compareresult>0) T.right=Remove (x,t.right); Else if(T.left! =NULL&& t.right!=NULL){            //In case of two children, the minimum value of the right subtree is populated to the node;//Delete Minimum node in right subtreeT.element=findmin (t.right). element; T.right=Remove (t.element.t.right); }        Else            //only one child .t= (T.left! =NULL) ?T.left:t.right; returnT; }        Private voidPrinttree (binarynode<anytype>t) {        if(t!=NULL) {printtree (t.left);            System.out.println (t.element);        Printtree (T.right); }    }    }
View Code

Data structure and algorithm analysis java--Tree 2 (binary tree type)

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.