续一
====红黑树操作的实现,插入、删除、查找 (一)====
/*
* @(#)Rbtree.java
*
* Title: This class is to define the RBtree.
* Description: This file is the assignment of Algorithms.
* @copyright (c) OpenSource
* @author Bobby Yang
* @tester Bobby Yang
* @version 1.00
* @time 2005.11.04
*/
package rbtree;
public class Rbtree {
private Node root;
private static Node nil = new Node();
private static void initNil(){
nil.iData = 0;
nil.color = true;
nil.leftChild = null;
nil.rightChild = null;
}
private void setRoot(Node node){
this.root = node;
}
private void preOrder(Node root){
if(root != null) System.out.print("(" + root.iData + root.getColor() + ",");
if(root.leftChild == nil) System.out.print("NIL,");
else {
preOrder(root.leftChild);
System.out.print(",");
}
if(root.rightChild == nil) System.out.print("NIL)");
else{
preOrder(root.rightChild);
System.out.print(")");
}
}
private void insert(Node newNode){
if(root == null){ // no node in root
root = newNode;
root.parent = nil;
}
else // root occupied
{
Node current = root; // start at root
Node parent;
while(true){ // (exits internally)
parent = current;
if(newNode.iData < current.iData) // go left
{
current = current.leftChild;
if(current == nil){ // if end of the line,
// insert on left
parent.leftChild = newNode;
newNode.parent = parent;
return;
}
} // end if go left
else{ // or go right
current = current.rightChild;
if(current == nil){ // if end of the line
// insert on right
parent.rightChild = newNode;
newNode.parent = parent;
return;
}
} // end else go right
} // end while
} // end else not root
}
====红黑树操作的实现,插入、删除、查找 (一)====
/*
* @(#)Rbtree.java
*
* Title: This class is to define the RBtree.
* Description: This file is the assignment of Algorithms.
* @copyright (c) OpenSource
* @author Bobby Yang
* @tester Bobby Yang
* @version 1.00
* @time 2005.11.04
*/
package rbtree;
public class Rbtree {
private Node root;
private static Node nil = new Node();
private static void initNil(){
nil.iData = 0;
nil.color = true;
nil.leftChild = null;
nil.rightChild = null;
}
private void setRoot(Node node){
this.root = node;
}
private void preOrder(Node root){
if(root != null) System.out.print("(" + root.iData + root.getColor() + ",");
if(root.leftChild == nil) System.out.print("NIL,");
else {
preOrder(root.leftChild);
System.out.print(",");
}
if(root.rightChild == nil) System.out.print("NIL)");
else{
preOrder(root.rightChild);
System.out.print(")");
}
}
private void insert(Node newNode){
if(root == null){ // no node in root
root = newNode;
root.parent = nil;
}
else // root occupied
{
Node current = root; // start at root
Node parent;
while(true){ // (exits internally)
parent = current;
if(newNode.iData < current.iData) // go left
{
current = current.leftChild;
if(current == nil){ // if end of the line,
// insert on left
parent.leftChild = newNode;
newNode.parent = parent;
return;
}
} // end if go left
else{ // or go right
current = current.rightChild;
if(current == nil){ // if end of the line
// insert on right
parent.rightChild = newNode;
newNode.parent = parent;
return;
}
} // end else go right
} // end while
} // end else not root
}