在上篇 排序二叉树实现的基础上,增加自动平衡功能。
package TreeDemos ;
import java. util. ArrayList ;
public class BalancedTree < K extends Comparable < K > , V > {
public static class Node < K extends Comparable < K > , V > {
public K key;
public V value;
public Node < K , V > lchlid;
public Node < K , V > rchlid;
public Integer BI= 0 ;
public Integer height= 0 ;
public Node ( K key, V value, Node < K , V > lchlid, Node < K , V > rchlid) {
this . key = key;
this . value = value;
this . lchlid = lchlid;
this . rchlid = rchlid;
}
public Node ( ) {
}
public void rightBalance ( Node < K , V > node, Node < K , V > newNode) {
if ( node. BI< - 1 ) {
if ( newNode. key. compareTo ( node. rchlid. key) > 0 ) {
leftRotate ( node) ;
}
else if ( newNode. key. compareTo ( node. rchlid. key) < 0 ) {
rightRotate ( node. rchlid) ;
leftRotate ( node) ;
}
}
}
public void leftBalance ( Node < K , V > node, Node < K , V > newNode) {
if ( node. BI> 1 ) {
if ( newNode. key. compareTo ( node. lchlid. key) < 0 ) {
rightRotate ( node) ;
}
else if ( newNode. key. compareTo ( node. lchlid. key) > 0 ) {
leftRotate ( node. lchlid) ;
rightRotate ( node) ;
}
}
}
public void put ( Node < K , V > newNode) {
if ( newNode. key. compareTo ( this . key) < 0 ) {
if ( lchlid != null ) {
lchlid. put ( newNode) ;
this . getHeight ( ) ;
this . getBI ( ) ;
System . out. println ( "r" + this . key+ ":" + this . height+ ":" + this . BI) ;
leftBalance ( this , newNode) ;
} else {
lchlid = newNode;
lchlid. height+= 1 ;
this . getHeight ( ) ;
this . getBI ( ) ;
}
} else if ( newNode. key. compareTo ( this . key) > 0 ) {
if ( rchlid != null ) {
rchlid. put ( newNode) ;
this . getHeight ( ) ;
this . getBI ( ) ; ;
System . out. println ( "l" + this . key+ ":" + this . height+ ":" + this . BI) ;
rightBalance ( this , newNode) ;
} else {
rchlid = newNode;
rchlid. height+= 1 ;
this . getHeight ( ) ;
this . getBI ( ) ;
}
}
}
public void getHeight ( ) {
if ( this . lchlid!= null && this . rchlid!= null ) {
this . height= ( Math . max ( this . lchlid. height, this . rchlid. height) ) + 1 ;
}
else if ( this . lchlid== null && this . rchlid== null ) {
this . height= 1 ;
}
else {
this . height= this . lchlid!= null ? this . lchlid. height+ 1 : this . rchlid. height+ 1 ;
}
}
public void getBI ( ) {
if ( this . lchlid!= null && this . rchlid!= null ) {
this . BI= - this . rchlid. height+ this . lchlid. height;
}
else if ( this . lchlid== null && this . rchlid== null ) {
this . BI= 0 ;
}
else {
this . BI= this . lchlid!= null ? this . lchlid. height: - this . rchlid. height;
}
}
public Node < K , V > find ( K key) {
if ( key. compareTo ( this . key) < 0 && lchlid != null ) {
return lchlid. find ( key) ;
} else if ( key. compareTo ( this . key) > 0 && rchlid != null ) {
return rchlid. find ( key) ;
} else if ( key. compareTo ( this . key) == 0 ) {
return this ;
} else {
return null ;
}
}
public void delete ( K key, Node < K , V > parent, Node < K , V > root) {
if ( key. compareTo ( this . key) < 0 && lchlid != null ) {
lchlid. delete ( key, this , root) ;
} else if ( key. compareTo ( this . key) > 0 && rchlid != null ) {
rchlid. delete ( key, this , root) ;
} else if ( key. compareTo ( this . key) == 0 ) {
delete1 ( parent, root) ;
} else {
System . out. println ( "此节点不存在" ) ;
}
}
public Node < K , V > getPre ( ) {
Node < K , V > l= lchlid;
while ( l. rchlid!= null ) {
l= l. rchlid;
}
return l;
}
public void delete1 ( Node < K , V > parent, Node < K , V > root) {
if ( lchlid == null && rchlid == null ) {
if ( parent. lchlid == this ) {
parent. lchlid = null ;
}
if ( parent. rchlid == this ) {
parent. rchlid = null ;
}
} else if ( lchlid != null && rchlid == null ) {
value = lchlid. value;
this . key= lchlid. key;
lchlid = null ;
} else if ( lchlid == null ) {
value = rchlid. value;
this . key= rchlid. key;
rchlid = null ;
} else {
Node < K , V > pre= getPre ( ) ;
root. delete ( pre. key, null , root) ;
this . value= pre. value;
this . key= pre. key;
}
}
public void leftRotate ( Node < K , V > node) {
Node < K , V > temp= new Node < K , V > ( ) ;
temp. value= node. value;
temp. key= node. key;
temp. rchlid= node. rchlid. lchlid;
temp. lchlid= node. lchlid;
temp. getHeight ( ) ;
temp. getBI ( ) ;
node. value= node. rchlid. value;
node. key= node. rchlid. key;
node. rchlid= node. rchlid. rchlid;
node. lchlid= temp;
node. getHeight ( ) ;
node. getBI ( ) ;
System . out. println ( "lt" ) ;
}
public void rightRotate ( Node < K , V > node) {
Node < K , V > temp= new Node < K , V > ( ) ;
temp. value= node. value;
temp. key= node. key;
temp. lchlid= node. lchlid. rchlid;
temp. rchlid= node. rchlid;
temp. getHeight ( ) ;
temp. getBI ( ) ;
node. value= node. lchlid. value;
node. key= node. lchlid. key;
node. lchlid= node. lchlid. lchlid;
node. rchlid= temp;
node. getHeight ( ) ;
node. getBI ( ) ;
System . out. println ( "rt" ) ;
}
public String toString ( ) {
return "[" + key + ":" + value + ":" + BI+ ":" + height+ "]" ;
}
public String toString2 ( ) {
StringBuilder stringBuilder = new StringBuilder ( ) ;
if ( lchlid != null ) {
stringBuilder. append ( lchlid. toString2 ( ) ) ;
}
stringBuilder. append ( this ) ;
if ( rchlid != null ) {
stringBuilder. append ( rchlid. toString2 ( ) ) ;
}
return stringBuilder. toString ( ) ;
}
}
public Node < K , V > root;
public BalancedTree ( ArrayList < Node < K , V > > nodes) {
for ( Node < K , V > node : nodes) {
put ( node) ;
}
}
public void put ( Node < K , V > node) {
if ( root== null ) {
root= node;
root. height+= 1 ;
}
else {
root. put ( node) ;
}
}
public Node < K , V > find ( K key) {
return root. find ( key) ;
}
public void delete ( K key) {
root. delete ( key, null , root) ;
}
public String toString ( ) {
return root. toString2 ( ) ;
}
public static void main ( String [ ] args) {
ArrayList < Node < Integer , String > > nodes= new ArrayList < Node < Integer , String > > ( ) ;
nodes. add ( new Node < Integer , String > ( 3 , "A" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 2 , "B" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 1 , "J" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 4 , "F" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 5 , "G" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 6 , "K" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 7 , "K" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 10 , "K" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 9 , "K" , null , null ) ) ;
nodes. add ( new Node < Integer , String > ( 8 , "K" , null , null ) ) ;
BalancedTree < Integer , String > myTree= new BalancedTree < > ( nodes) ;
System . out. println ( myTree) ;
Node < Integer , String > res= myTree. find ( 56 ) ;
myTree. delete ( 56 ) ;
System . out. println ( myTree) ;
}
}