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)