Java 234树

package com.guge.test.tree234;

/**
 * Created by guugangzhu on 2017/5/12.
 */
public class Tree234 {

    private Node root=new Node();
    public int find(long key){
        Node curNode=root;
        int childNumber;
        while (true){
            if((childNumber=curNode.findItem(key))!=-1)
                return  childNumber;
            else if(curNode.isLeaf())
                return -1;
            else
                curNode=getNextChild(curNode,key);
        }
    }

    public Node getNextChild(Node theNode,long theValue){
        int numItems=theNode.getNumItemss();
        int i;
        for (i = 0; i ; i++) {
            if(theValuedDate)
                return theNode.getChild(i);
        } //end for
        return theNode.getChild(i);
    }

    public void insert(long dValue){
        Node curNode=root;
        DataItem tempItem=new DataItem(dValue);

        while (true){
            if(curNode.isFull()){
                split(curNode);
                curNode=curNode.getParent();
                curNode=getNextChild(curNode,dValue);
            }else if(curNode.isLeaf())
                break;
            else
                curNode=getNextChild(curNode,dValue);
        }
        curNode.insertItem(tempItem);
    }

    public void split(Node thisNode){
        DataItem itemB,itemC;
        Node parent,child2,child3;
        int itemIndex;

        itemC=thisNode.removeItem();
        itemB=thisNode.removeItem();
        child2=thisNode.disconnectChild(2);
        child3=thisNode.disconnectChild(3);
        Node newRight=new Node();

        if(thisNode==root){
            root=new Node();
            parent=root;
            root.connectChild(0,thisNode);
        }else
            parent=thisNode.getParent();

        itemIndex=parent.insertItem(itemB);
        int n=parent.getNumItemss();
        for (int i = n-1; i > itemIndex; i--) {
            Node temp=parent.disconnectChild(i);
            parent.connectChild(i+1,temp);
        }

        parent.connectChild(itemIndex+1,newRight);
        newRight.insertItem(itemC);
        newRight.connectChild(0,child2);
        newRight.connectChild(1,child3);
    }

    public void disPlayTree(){
        recDisplayTree(root,0,0);
    }

    private void recDisplayTree(Node thisNode,int level,int childNumber){
        System.out.print("level="+level+" child="+childNumber+" ");
        thisNode.displayNode();

        int numItems=thisNode.getNumItemss();
        for (int i = 0; i 1 ; i++) {
            Node nextNode=thisNode.getChild(i);
            if(nextNode!=null)
                recDisplayTree(nextNode,level+1,i);
            else
                return;
        }
    }
}


class DataItem{
    public long dDate;

    public DataItem(long dd){
        dDate=dd;
    }

    public void displayItem(){
        System.out.print("/"+dDate);
    }
}

class Node{
    private static final int ORDER=4;
    private int numItems;
    private Node parent;
    private Node childArray[]=new Node[ORDER];  //四个子节点
    private DataItem itemArray[]=new DataItem[ORDER-1]; //每个节点包含做多三个数据

    public DataItem[] getItemArray() {
        return itemArray;
    }

    public void connectChild(int childNum,Node child){
        childArray[childNum]=child;
        if(child!=null)
            child.parent=this;
    }

    public Node disconnectChild(int childNum){
        Node tempNode=childArray[childNum];
        childArray[childNum]=null;
        return tempNode;
    }

    public Node getChild(int childNum){
        return childArray[childNum];
    }

    public Node getParent(){
        return parent;
    }

    public boolean isLeaf(){
        return childArray[0]==null;
    }

    public int getNumItemss(){
       return numItems;
    }

    public DataItem getItem(int index){
        return itemArray[index];
    }

    public boolean isFull(){
        return numItems==ORDER-1;
    }

    public int findItem(long key){
        for (int i = 0; i <ORDER-1 ; i++) {
            if(itemArray[i]==null)
                break;
            else if(itemArray[i].dDate==key)
                return i;
        }
        return -1;
    }

    public int insertItem(DataItem newItem){
        numItems++;
        long newKey=newItem.dDate;
        for(int j=ORDER-2;j>=0;j--){
            if(itemArray[j]==null)
                continue;
            else {
                long itsKey=itemArray[j].dDate;
                if(newKeyitemArray[j+1]=itemArray[j];
                else{
                    itemArray[j+1]=newItem;
                    return j+1;
                }
            }
        }
        itemArray[0]=newItem;
        return 0;
    }

    /**
     * 去掉最大
     * @return
     */
    public DataItem removeItem(){
        DataItem temp=itemArray[numItems-1];
        itemArray[numItems-1]=null;
        numItems--;
        return temp;
    }

    public void displayNode(){
        for(DataItem dataItem:itemArray){
            if(dataItem!=null)
                dataItem.displayItem();
        }
        System.out.println("/");
    }


}
class Tree234App{
    public static void main(String[] args) {
        Tree234 tree=new Tree234();
        tree.insert(10);
        tree.insert(20);
        tree.insert(30);
        tree.insert(40);
        tree.insert(50);
        tree.insert(60);
        tree.insert(70);
        tree.insert(80);
        tree.insert(90);
        tree.insert(100);
        tree.insert(110);
        tree.insert(120);
        tree.disPlayTree();
    }
}

你可能感兴趣的:(数据结构与算法)