2-3-4树:2-3-4 树在计算机科学中是阶为4 的B树。它可以
2-3-4树
在O(log n)时间内查找、插入和删除。
2-3-4 树把
数据存储在叫做
元素的单独单元中。它们组合成
节点。每个节点都是下列之一:
2-节点,就是说,它包含 1 个元素和 2 个儿子;
3-节点,就是说,它包含 2 个元素和 3 个儿子;
4-节点,就是说,它包含 3 个元素和 4 个儿子。
package com.ten;
import java.io.*;
public class Tree234App{
public static void main(String[] args) throws IOException
{
Tree234 theTree =newTree234();
theTree.insert(50);
theTree.insert(40);
theTree.insert(60);
theTree.insert(30);
theTree.insert(10);
theTree.insert(90);
theTree.insert(20);
theTree.insert(80);
theTree.insert(70); theTree.displayTree();
int value = 11;
int found=theTree.find(value);
if(found != -1)
System.out.println("Found"+value);
else
System.out.println("Could not find"+value);
}
}
//2-3树的节点中的数据
class Data234
{
public int data;//数据类中只含有一个数据项
public Data234(int data)
{
this.data = data;
}
public void display(){
System.out.print("/"+data+"");
}
}
//2-3树中的节点类
class Node234//因为创建对象时,引用自动设置为null,数字为0,因此Node23不用设置构造器
{
private static final int NUM = 4;//定义一个常量为最大的子节点数,则数据项最大为NUM-1
private Data234 dataArr[] = newData234[NUM-1];//创建每个节点中的数据项,类型为Data23
private Node234 childArr[] = newNode234[NUM];//创建一个父节点的子节点,最多有4个,或者3,2
private int dataNum = 0;//当前数据项个数
private Node234 parent ;//设置父节点,
public void connectChild(int childIndex,Node234 child)//初始化子节点
{
childArr[childIndex] = child;
if(child!=null)
child.parent = this;
}
public Node234 disconnectChild(int childIndex)//断开子节点和父节点的链接
{
Node234 child = childArr[childIndex];
childArr[childIndex] = null;
return child;
}
//根据子节点序号,得到子节点
public Node234 getChild(int childIndex){ returnchildArr[childIndex]; }
//得到父节点
public Node234 getParent(){ return parent; }
//判断节点是否为叶子节点
public booleanisLeaf() { return(childArr[0]==null)?true:false; }
//获得当前数据项个数
public intgetNumData() { returndataNum; }
//根据下标得到一个结点中的数据项
public Data234 getData(intdataIndex) { return dataArr[dataIndex]; }
//判断节点是否满
public booleanisFull() { return(dataNum==NUM-1)?true:false; }
public int findData(intkey) {
for(intj=0; j
if(dataArr[j]==null)
break;
elseif(dataArr[j].data == key)
return j;
}
return -1;
}
public int insertData(Data234value)//插入数据之后仍是有序的 {
//节点未满
dataNum++;
int newKey=value.data;
//若节点是非空,就从后往前找适当的位置
for(int j=NUM-2;j>=0; j--){
if(dataArr[j] ==null)//如果节点中右边的数据项为空,则继续查看其左边的数据项
continue;
else //得到最右边的非空数据中的关键值 {
int itsKey =dataArr[j].data;
if(newKey< itsKey)//如果新插入的关键值比此关键值小
dataArr[j+1] = dataArr[j];//将此关键值右移
else
{//若新插入的关键值比此关键值大,则直接插入
dataArr[j+1] = value;
returnj+1; //返回插入的下标
}
}
}
dataArr[0] =value;//如果结点是空的
return 0;
}
public Data234 removeData()//删除关键值最大的数据项 {
Data234 temp=dataArr[dataNum-1];
dataArr[dataNum-1]=null;
dataNum--;
returntemp;
}
public void displayNode() {
for(int i=0;i
dataArr[i].display();
}
System.out.println("/");
}
}
//2-3-4树类
class Tree234{
private Node234 root = new Node234();
//根据给定关键字查找数据项
public int find(int key) {
Node234 curNode = root;
int childNumber;
while(true)
{
if(( childNumber=curNode.findData(key))!= -1)
returnchildNumber; // found it
else if( curNode.isLeaf() )
return-1; // can't find it
else // searchdeeper
curNode = getNextChild(curNode, key);
}
}
public voidinsert(intdValue) {
Node234 curNode = root;
Data234 tempItem = newData234(dValue);
while(true)
{
if( curNode.isFull()) // if node full,
{
split(curNode); // split it
curNode =curNode.getParent();
curNode = getNextChild(curNode,dValue);
}
else if( curNode.isLeaf()) // if node is leaf,
break; //go insert
// node is not full, not a leaf; so go tolower level
else
curNode = getNextChild(curNode,dValue);
}
curNode.insertData(tempItem); // insert new DataItem
}
public voidsplit(Node234thisNode) // split the node
{
// assumes node is full
Data234 itemB, itemC;
Node234 parent, child2,child3;
int itemIndex;
itemC =thisNode.removeData(); // remove items from
itemB =thisNode.removeData(); // this node
child2 =thisNode.disconnectChild(2); //remove children
child3 =thisNode.disconnectChild(3); //from this node
Node234 newRight = newNode234(); // make new node
if(thisNode==root) // if this is theroot, {
root = newNode234(); // make new root
parent =root; // root is our parent
root.connectChild(0, thisNode); // connect to parent
}
else // this node notthe root
parent = thisNode.getParent();
itemIndex =parent.insertData(itemB); //item B to parent
int n =parent.getNumData(); // total items?
for(int j=n-1;j>itemIndex;j--) // move parent's
{ // connections
Node234 temp=parent.disconnectChild(j); // one child
parent.connectChild(j+1,temp); // to the right
}
parent.connectChild(itemIndex+1, newRight);
newRight.insertData(itemC); // item C to newRight
newRight.connectChild(0,child2); // connectto 0 and 1
newRight.connectChild(1,child3); // onnewRight
}
// 以theNode为根的数中的最小点
public Node234 getNextChild(Node234theNode,long theValue)
{
int j=0;
//如果theNode非空,且不满,非叶子节点
int numItems =theNode.getNumData();
for(;j
{ // are we less?
if( theValue < theNode.getData(j).data)
return theNode.getChild(j); // return leftchild
} // we're greater, so
returntheNode.getChild(j); // return right child
}
//封装recDisplayTree
public void displayTree()
{
recDisplayTree(root, 0,0);
}
//根据结点,所在层,子节点个数来遍历2-3-4树(从根开始,从上往下,从左往右开始遍历)
private voidrecDisplayTree(Node234 thisNode, int level, intchildNumber)
{
System.out.print("level="+level+"child="+childNumber+"");
thisNode.displayNode();
//遍历每个子节点,numItems为thisNode的数据项个数,numItems+1即为thisNode的子节点个数
int numItems =thisNode.getNumData();
for(int j=0; j
{
Node234 nextNode = thisNode.getChild(j);
if(nextNode != null)
recDisplayTree(nextNode, level+1,j);//递归遍历下一层
else
return;
}
}
} 结果是:
level=0 child=0 /30 /50 /80 /
level=1 child=0 /10 /20 /
level=1 child=1 /40 /
level=1 child=2 /60 /70 /
level=1 child=3 /90 /
Could not find 11
2-3树:是最简单的B-树(或-树)结构,其每个非叶节点都有两个或三个子女,而且所有叶都在统一层上。2-3树不是二叉树,其节点可拥有3个孩子。不过,2-3树与满二叉树相似。高为h的2-3树包含的节点数大于等于高度为h的满二叉树的节点数,即至少有2^h-1个节点。
将数据项放入2-3树节点中的规则是:
(1)2-节点有两个孩子,必含一个
数据项,其查找关键字大于左孩子的查找关键字,而小于右孩子的查找关键字。
(2)3-节点有三个孩子,必含两个数据项,其查找关键字S和L满足下列关系:S大于左孩子的查找关键字,而小于中孩子的查找关键字;L大于中孩子的查找关键字,而小于右孩子的查找关键字。
(3)叶子可以包含一个或两个数据项。
程序如下:
package com.ten;
import java.io.*;
import java.lang.*;
public class TestTree23 {
public static void main(Stringargs[])throws IOException{
// FileWriter fw1= newFileWriter("D:\\output.txt");
FileReader fr=null;
BufferedReader br=null;
String s;
Tree23Node tr= newTree23Node();
int t=0;
String str1= newString("insert");
String str2= newString("search");
String str3= newString("succ");
String str4= newString("sort");
String str5= newString("min");
String str6= newString("output");
String str7= newString("delete");
try{
fr= newFileReader("G:\\IPM_SOURCE\\DataStructLafore\\Tree.txt");
br= newBufferedReader(fr);
} catch(FileNotFoundExceptionexc){
System.out.println("Can notopen the file");
return;
}
try {
while((s=br.readLine())!= null){
String[] ss=s.split(" ");
if(ss[0].equals(str1)){
t=Integer.parseInt(String.valueOf(ss[1]),10);
tr.insert(t);
}
if(ss[0].equals(str2)){
t=Integer.parseInt(String.valueOf(ss[1]),10);
tr.search(tr.root,t);
}
if(ss[0].equals(str3)){
t=Integer.parseInt(String.valueOf(ss[1]),10);
tr.succ(t);
}
if(ss[0].equals(str7)){
t=Integer.parseInt(String.valueOf(ss[1]),10);
tr.delete(t);
//System.out.println(" the delete numberis:"+ss[1]);
}
if(ss[0].equals(str4)){
tr.sort(tr.root);
}
if(ss[0].equals(str5)){
System.out.println("the min number is"+tr.min(tr.root));
}
if(ss[0].equals(str6)){
tr.LayerOrder(tr.root);
}
}
}catch(IOException exc){
System.out.println("Errorreading file.");
}catch(NumberFormatExceptionex){
System.out.println("null");
}
try{
fr.close();
}catch(IOException exc){
System.out.println("Errorclosing file.");
}
}
}
class Tree23Node {
int small;//the small item in the node
int large;//the large one in the node
int temp; //stores a temporary item
intnodeID;
intnodetype;
Tree23Nodeleft;// the left child oftree
Tree23Nodemiddle;// the middle child of tree
Tree23Noderight;// the right child of tree
intitemCount; // how many items in this node
Tree23Noderoot= null;
privateTree23Node rtNode;
privateTree23Node parent;
Tree23Node(){
nodeID=0;
itemCount=0;
small = large = temp = -1;
left = middle = right = parent = null ;
} // endtree23Node constructor
Tree23Node(int newVal){
nodeID=0;
itemCount = 1;
small = newVal;
large = temp = -1;
left = middle = right = parent = null ;
} // endtree23Node constructor
publicboolean isLeaf()
{
boolean value = false ;
if (left==null && middle ==null &&right==null)
{
value = true ;
}
return value;
}// method isLeaf
public voidinsert(int data){
// if the root is empty, which means the tree is empty.
// create the tree. otherwise, call a private method
//Tree23Node root=null;
if (root==null ){
root = new Tree23Node(data);
}
else
{
insert(root, data);
}
} // endinsert
private voidinsert(Tree23Node curr, int data){
// stores the node in a temporary variable
Tree23Node ptr = curr;
while (!ptr.isLeaf()){
if (data <= ptr.small && ptr.left!=null){
ptr = ptr.left;
}
else if (data>=ptr.large && ptr.right!=null){
ptr = ptr.right;
}
else if (data
ptr = ptr.middle;
}
}
// case 1:there is one item in the node,insert data directly into aleaf
if (ptr.itemCount==1)
{
if (ptr.small <= data)
{
ptr.large = data;
ptr.itemCount ++;
}
else
{
ptr.large = ptr.small;
ptr.small = data;
ptr.itemCount ++;
}
} // end outerif
// case 2:the node is full which has two items,insert data in order, andsplit
else
{
if (ptr.large <= data)
{
ptr.temp = data; // temp always store the largest number
}
else if (ptr.small <= data)
{
ptr.temp = ptr.large;
ptr.large = data;
}
else
{
ptr.temp = ptr.large;
ptr.large = ptr.small;
ptr.small = data;
}
ptr.itemCount++;
root=ptr.split(); // return a whole tree
}
//return root;
} // endmethod insert
Tree23Nodesplit(){
Tree23Node retval, node1, node2;
int s, m, l;
s = small;
m = large;
l = temp;
node1 = new Tree23Node(s);
node2 = new Tree23Node(l);
// there are two cases
// 1. this is the root, I just need to create a new root thatcontains the middle item and connect two new nodes to it
// 2. this is not the root, I need to do the following things
// 1) consider this node is parent’s left, middle, or right, to dodifferent connections
// 2) if parent is still full, recursively split
if (parent==null)
{
// create a new node contains the middle value
retval = new Tree23Node(m);
retval.left = node1;
retval.right = node2;
}
else
{
retval = parent;
// if this node is parent’s left child
if (retval.left==this )
{
// if parent is full already, now parent should
// have four children(1 is in rtNode), three items
if (retval.itemCount>1)
{
retval.rtNode = retval.right;
retval.right = retval.middle;
retval.temp = retval.large;
}
retval.left = node1;
retval.middle = node2;
retval.large = retval.small;
retval.small = m;
}
else if (retval.right==this )
{
if (retval.itemCount==1) // the parent is not full
{
retval.middle = node1;
retval.right = node2;
retval.large = m;
}
else // the parent is full
{
retval.right = node1;
retval.rtNode = node2;
retval.temp = m;
}
}
else if (retval.middle == this )
{
retval.middle = node1;
retval.rtNode = retval.right;
retval.right = node2;
retval.temp = retval.large;
retval.large = m;
}
else // report error
{
System.out.println("pointer error!");
System.exit(0);
}
retval.itemCount++; // the parent item adds
itemCount--; //the child item decrease.
} // end else parent != null
node1.parent = node2.parent = retval;
if (!isLeaf())
{
node1.left = left;
node1.right = middle;
node2.left = right;
node2.right = rtNode;
if (node1.left!=null )
{
node1.left.parent = node1;
}
if (node1.right!=null )
{
node1.right.parent = node1;
}
if (node2.left!=null ) {
node2.left.parent = node2;
}
if (node2.right!=null ){
node2.right.parent = node2;
}
} // end if
left = middle = right = rtNode = parent = null ;
if (retval.rtNode!=null ){
return retval.split();
}
while (retval.parent!=null )
{
retval = retval.parent;
}
return retval ;
} // endmethod split
// pursuethe minimum value of the numbers
intmin(Tree23Node ro){
Tree23Node tem= ro;
int i;
while (tem.left!=null){
tem=tem.left;
}
i=tem.small;
return i;
}
voidsucc(int num){
int s=num;
int k;
Tree23Node yy;
Tree23Node xx=search(root,s);
//System.out.println(xx.small);
if(xx==null){
System.out.println("there is no"+s);
}
// one node status, pursue the successor
else if(xx.itemCount==1){
if(xx.right!=null){
k=min(xx.right);
System.out.println("the successor of"+ s +"is"+k);
}
else {
yy=xx.parent;
if(yy.itemCount==2&&xx.small==yy.middle.small){
System.out.println("the successorof"+s+"is"+yy.large);
}
else{
while(yy!=null&&(xx==yy.right||xx==yy.middle)){
xx=yy;
yy=yy.parent;
}
if(yy==null){
System.out.println("thesuccessor of2 "+ s+"is the largest number,no successor" );
}
else if(xx.large==yy.left.large||xx.small==yy.left.small){
System.out.println("the successor of1"+ s+" is "+yy.small);
}
else {
System.out.println("the successor of3"+ s+" is "+yy.large);
}
//System.out.println("the successor of"+ s+" is "+yy.small);
}
}
}
// two node status, pursue the successor
else if(xx.itemCount==2){
if(s==xx.small&&xx.middle!=null){
k=min(xx.middle);
System.out.println("the successor of"+ s+" is"+k);
}
else if(s==xx.large){
if(xx.right!=null){
k=min(xx.right);
System.out.println("the successor of"+ s+" is"+k);
}
else {
//System.out.println(s+"get here");
yy=xx.parent;
//case 1 s is the middle node
if(yy.itemCount==2&&xx.large==yy.middle.large){
System.out.println("the successor of"+ s+" is"+yy.large);
}
// case 2 s is the right, go to high layer to find the first numberis his parent left chlid
else{
//System.out.println("test");
while(yy!=null&&(xx==yy.right||xx==yy.middle)){
xx=yy;
yy=yy.parent;
// repeat case 1 status
//System.out.println("test1");
}
// System.out.println("x.smallis"+xx.right.small);
if(yy==null){
System.out.println("the successor of "+ s+"is thelargest number,no successor" );
}
elseif(xx.large==yy.left.large||xx.small==yy.left.small){
System.out.println("the successor of "+ s+" is "+yy.small);
}
else {
System.out.println("the successor of "+ s+" is "+yy.large);
}
//return;
}
}
}
else{
// two node is in a leaf, so the small one'ssuccessor is the large one item
System.out.println("the successor of "+ s+"is "+xx.large);
}
}
}
Tree23Nodesearch(Tree23Node cu,int data){
Tree23Node te=cu;// the temp restore the currentroot value
Tree23Node tt=null;
int i= data;
if(te==null){
//System.out.println(i+" is not in the 2-3 tree3");
tt=te;
}
elseif(te.itemCount==1){// 2 nodes
if(te.small==i){
//System.out.println(i+" is in the 2-3 tree");
tt=te;
}
else if(i
return search(te.left,i);
}
else {
return search(te.right,i);
}
}
// 3-node
else if(te.itemCount==2){
if((te.small==i)||(te.large==i)){
//System.out.println(i+" is in the 2-3tree");
tt=te;
}
elseif(i
return search(te.left,i);
}
elseif((ite.small)){
return search(te.middle,i);
}
else{
return search(te.right,i);
}
}
//System.out.println(tt.small);
return tt;
}
voidsort(Tree23Node ord){
Tree23Node curr=ord;
if(curr!=null){
if(curr.itemCount==1){
sort(curr.left);
System.out.println(curr.small);
sort(curr.right);
}
elseif(curr.itemCount==2){
sort(curr.left);
System.out.println(curr.small);
sort(curr.middle);
System.out.println(curr.large);
sort(curr.right);
}
else{
return;
}
}
}
voiddelete(int data){
int i=data;
int tmp;
Tree23Node qq;
Tree23Node ptr;
qq=search(root,i);
if (qq==null ) {
System.out.println("There is no number which youwant to delete");
}
else{
if (!qq.isLeaf()){ // if it is not a leaf
if (qq.small==i && qq.itemCount>1)
{
ptr = qq.middle;
}
else
{
ptr = qq.right;
}
// inorder successor
while (ptr.left!=null) {
ptr = ptr.left;
}
//interchange delete number and its successor
if(qq.small==i){
tmp=qq.small;
qq.small=ptr.small;
//System.out.println("the numberis"+ptr.small);
ptr.small=tmp;
//System.out.println("whos"+ptr.itemCount);
if(ptr.itemCount==1){
fix(ptr);//fix the changedsuccessor
}
else{
ptr.small=ptr.large;
ptr.large=-1;
ptr.itemCount--;
return;
}
}
else{
tmp=qq.large;
qq.large=ptr.small;
ptr.small=tmp;
if(ptr.itemCount==1){
fix(ptr);//fix the changedsuccessor
}
else{
ptr.small=ptr.large;
ptr.large=-1;
ptr.itemCount--;
return;
}
}
}
else{
if(qq.small==i) {
if(qq.itemCount==2){
qq.small=qq.large;
qq.large=-1;
qq.itemCount--;
}
else{
System.out.println("get here1");
fix(qq);
}
}
else if(qq.large==i){
qq.large=-1;
qq.itemCount--;
}
}
}
} // end delete
voidfix(Tree23Node node){
Tree23Node cnode;
Tree23Node pnode;
cnode=node;
if(cnode.parent.itemCount==1){//parent have one item;
//System.out.println("gethere"+cnode.small+":"+cnode.parent.right.small);
if(cnode.small==cnode.parent.right.small){// thedelete node is parent's right child
if(cnode.parent.left.itemCount==2){//redistribution
cnode.small=cnode.parent.small;
cnode.parent.small=cnode.parent.left.large;
cnode.parent.left.large=-1;
cnode.parent.left.itemCount--;
}
elseif(cnode.parent.left.itemCount==1){//merge
cnode.small=-1;
cnode.itemCount--;
cnode.parent.left.large=cnode.parent.small;
cnode.parent.left.itemCount++;
cnode.parent.small=-1;
cnode.parent.itemCount--;
pnode=cnode.parent;
System.out.println("thepnode:"+pnode.parent.itemCount);
if(pnode.parent.itemCount==1&&pnode.parent.left==pnode){
if(pnode.parent.right.itemCount==2){
pnode.small=pnode.parent.small;
pnode.parent.small=pnode.parent.right.small;
pnode.parent.right.small=pnode.parent.right.large;
pnode.parent.right.itemCount--;
cnode.small=pnode.parent.right.small;
cnode.itemCount++;
pnode.right.left=pnode.right.middle;
pnode.right.middle=null;
}
else{// the delete number is parent's rightchild&&leaf node
pnode.parent.right.large=pnode.parent.right.small;
pnode.parent.right.small=pnode.parent.small;
pnode.parent.right.itemCount++;
pnode.parent.right.middle=pnode.parent.right.left;
pnode.parent.right.left=pnode.left;
pnode.left.parent=pnode.parent.right;
root=pnode.parent.right;
return;
}
}
}
}
else if(cnode.small==cnode.parent.left.small){// the delete node isparent's left child
if(cnode.parent.right.itemCount==2){//redistribution
cnode.small=cnode.parent.small;
cnode.parent.small=cnode.parent.right.small;
cnode.parent.right.small=cnode.parent.right.large;
cnode.parent.right.large=-1;
cnode.parent.right.itemCount--;
}
elseif(cnode.parent.right.itemCount==1){//merge
// System.out.println("get there");
cnode.small=-1;
cnode.itemCount--;
cnode.parent.right.large=cnode.parent.right.small;
cnode.parent.right.small=cnode.parent.small;
cnode.parent.right.itemCount++;
cnode.parent.small=-1;
cnode.parent.itemCount--;
pnode=cnode.parent;
//the delete number is left child ofparent&&leaf node
if(pnode.parent.itemCount==1&&pnode.parent.left==pnode){
if(pnode.parent.right.itemCount==1){
pnode.parent.right.large=pnode.parent.right.small;
pnode.parent.right.small=pnode.parent.small;
pnode.parent.right.itemCount++;
pnode.parent.right.middle=pnode.parent.right.left;
pnode.parent.right.left=pnode.right;
pnode.right.parent=pnode.parent.right;
root=pnode.parent.right;
return;
}
else{
pnode.small=pnode.parent.small;
pnode.itemCount++;
pnode.left=pnode.right;
pnode.parent.small=pnode.parent.right.small;
pnode.parent.right.small=pnode.parent.large;
pnode.parent.right.large=-1;
pnode.parent.right.itemCount--;
pnode.left=pnode.parent.right.left;
pnode.parent.right.left.parent=pnode;
pnode.parent.right.left=pnode.parent.right.middle;
return;
}
}
}
}
else{//parent have twoitems
if(cnode==cnode.parent.left){
if(cnode.parent.middle.itemCount==1){
cnode.small=cnode.parent.small;
cnode.large=cnode.parent.middle.small;
cnode.parent.small=cnode.parent.large;
cnode.parent.middle=null;
cnode.itemCount++;
}
else{
cnode.small=cnode.parent.small;
cnode.parent.small=cnode.parent.middle.small;
cnode.parent.middle.small=cnode.parent.middle.large;
cnode.parent.middle.itemCount--;
}
}
else{
if(cnode.parent.middle.itemCount==1){
cnode.small=cnode.parent.middle.small;
cnode.parent.middle=null;
cnode.large=cnode.parent.small;
cnode.itemCount++;
}
else{
cnode.small=cnode.parent.small;
cnode.parent.small=cnode.parent.middle.large;
cnode.parent.middle.itemCount--;
}
}
}
}
}
voidLayerOrder(Tree23Node rootNode)throws IOException{
Tree23Node rot=rootNode;
Tree23Node []Nodes= new Tree23Node[20];
int front=-1;
int rear=-1;
int i=1;
int r=0,j=0;
FileWriter fw= newFileWriter("D:\\output.txt");
for(int k=0;k<20;k++){
Nodes[k]=null;
}
if(rot!=null)
{
rear++;
Nodes[rear]=rot;
rot.nodeID=i;
}
while(front!=rear){
front++;
rot=Nodes[front];
if(rot.left!=null){
rear++;
Nodes[rear]=rot.left;
rot.left.nodeID=(++i);
}
if(rot.middle!=null){
rear++;
Nodes[rear]=rot.middle;
rot.middle.nodeID=(++i);
}
if(rot.right!=null){
rear++;
Nodes[rear]=rot.right;
rot.right.nodeID=(++i);
}
}
while(Nodes[j]!=null){
if(Nodes[j]==null){return;}
if(!Nodes[j].isLeaf()){
if(Nodes[j].itemCount==1){
Nodes[j].nodetype=2;
}
if(Nodes[j].itemCount==2){
Nodes[j].nodetype=3;
}
}
else{
if(Nodes[j].itemCount==1){
Nodes[j].nodetype=-2;
}
if(Nodes[j].itemCount==2){
Nodes[j].nodetype=-3;
}
}
j++;
}
while(Nodes[r]!=null){
if(!Nodes[r].isLeaf()){
if(Nodes[r].itemCount==1){
fw.write("the ID is "+Nodes[r].nodeID+"nodetype:"+Nodes[r].nodetype+" x:"+Nodes[r].left.nodeID+"y:"+Nodes[r].right.nodeID+"\r\n");
}
if(Nodes[r].itemCount==2){
fw.write("the ID is "+Nodes[r].nodeID+"nodetype:"+Nodes[r].nodetype+" x:"+Nodes[r].left.nodeID+"y:"+Nodes[r].middle.nodeID+"z:"+Nodes[r].right.nodeID+"\r\n");
}
}
else{
if(Nodes[r].itemCount==1){
fw.write("the leaf ID is "+Nodes[r].nodeID+"nodetype:"+Nodes[r].nodetype+" x:"+Nodes[r].small+"\r\n");
}
if(Nodes[r].itemCount==2){
fw.write("the leaf ID is "+Nodes[r].nodeID+"nodetype:"+Nodes[r].nodetype+" x:"+Nodes[r].small+"y:"+Nodes[r].large+"\r\n");
}
}
r++;
}
fw.close();
}
}
结果是:
the successor of10 is 12
the successor of 8 is 10
the successor of4 is 8
the min number is 2
2
4
8
10
12
16