java红黑树实现英文字典

期中的project存档。

class RBTree{
    String EnglishVersion,ChineseVersion;
    RBTree parent,leftChildTree,rightChildTree,treeRoot;
    boolean Red=true,Black=false,color,originalColorForDeletion;
    String[] RBTreeArrayForKeySearch=new String[10000];
    public RBTree(){
    }
    public void testSearch(String EnglishWord){
        RBTree searchKey=searchByEnglishValue(EnglishWord);
        if (searchKey.EnglishVersion.equals("")) {
            System.out.println("the key does not exist!");
        }
        else{
            System.out.println(searchKey.EnglishVersion+":"+searchKey.ChineseVersion);
        }
    }
    public void testUpdate(String EnglishWord,String ChineseMeaning){
        RBTree searchKey=searchByEnglishValue(EnglishWord);
        if (searchKey.EnglishVersion.equals("")) {
            testInsert(EnglishWord,ChineseMeaning);
        }
        else{
            updateChineseMeaning(EnglishWord,ChineseMeaning);
        }
    }
    public void testInsert(String EnglishWord,String ChineseMeaning){
        RBInsertion(EnglishWord,ChineseMeaning);
    }
    public void testRBDeletion(String deleteValue){
        RBDeletion(deleteValue);
    }
    public void testPrintRBTree(){
        printRBTree(this.treeRoot);
    }
    private void RBDeletion(String deleteEnglishValue){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        RBTree x,y;
        RBTree deleteKey=searchByEnglishValue(deleteEnglishValue);
        if(deleteKey.EnglishVersion.equals(leafNode.EnglishVersion)){
            System.out.println("Error:key missing!");
        }//要删除的单词不存在
        else{//删除代码正文
            y=deleteKey;
            y.originalColorForDeletion=y.color;
            if(deleteKey.leftChildTree.EnglishVersion.equals(leafNode.EnglishVersion)){
                x=deleteKey.rightChildTree;
                RBTransplant(deleteKey,deleteKey.rightChildTree);
            }//currently OK
            else if(deleteKey.rightChildTree.EnglishVersion.equals(leafNode.EnglishVersion)){
                x=deleteKey.leftChildTree;
                RBTransplant(deleteKey,deleteKey.leftChildTree);
            }//currently OK
            else{
                y=TreeSuccessor(deleteKey);//successor
                y.originalColorForDeletion= y.color;//后继若为黑色,则用它替代deleteKey时少了一个黑色;
                x=y.rightChildTree;
                if(y.parent==deleteKey){
                    x.parent=y;
                }//the case that delete h
                else{
                    RBTransplant(y,y.rightChildTree);
                    y.rightChildTree=deleteKey.rightChildTree;
                    y.rightChildTree.parent=y;
                }//put the successor on the position of deleteKey
                RBTransplant(deleteKey,y);
                y.leftChildTree=deleteKey.leftChildTree;
                y.leftChildTree.parent=y;
                y.color=deleteKey.color;
            }
            if(y.originalColorForDeletion==Black){
                RBDeletionFixUp(x);
            }
        }
    }
    private void RBDeletionFixUp(RBTree nodeX){
        while(nodeX!=treeRoot&&nodeX.color==Black){
            RBTree siblingX;
            if(nodeX==nodeX.parent.leftChildTree){//nodeX is a leftChild
                siblingX=nodeX.parent.rightChildTree;
                if(siblingX.color==Red){
                    siblingX.color=Black;//case1
                    nodeX.parent.color=Red;//case1
                    leftRotation(nodeX.parent);//case1
                    siblingX=nodeX.parent.rightChildTree;//case1
                }
                if(siblingX.leftChildTree.color==Black&&siblingX.rightChildTree.color==Black){
                    siblingX.color=Red;
                    nodeX=nodeX.parent;//case2
                }
                else if(siblingX.rightChildTree.color==Black) {
                    siblingX.leftChildTree.color = Black;
                    siblingX.color = Red;
                    rightRotation(siblingX);
                    siblingX = nodeX.parent.rightChildTree;//case3

                    siblingX.color = nodeX.parent.color;
                    nodeX.parent.color = Black;
                    siblingX.rightChildTree.color = Black;
                    leftRotation(nodeX.parent);
                    nodeX = this.treeRoot;//case4
                }
            }
            else{//nodeX is a rightChild,exchange the left and right in "if" clause
                siblingX=nodeX.parent.leftChildTree;
                if(siblingX.color==Red){
                    siblingX.color=Black;//case1
                    nodeX.parent.color=Red;//case1
                    rightRotation(nodeX.parent);//case1
                    siblingX=nodeX.parent.leftChildTree;//case1
                }
                if(siblingX.leftChildTree.color==Black&&siblingX.rightChildTree.color==Black){
                    siblingX.color=Red;
                    nodeX=nodeX.parent;//case2
                }
                else if(siblingX.leftChildTree.color==Black) {
                    siblingX.rightChildTree.color = Black;
                    siblingX.color = Red;
                    leftRotation(siblingX);
                    siblingX = nodeX.parent.leftChildTree;//case3

                    siblingX.color = nodeX.parent.color;
                    nodeX.parent.color = Black;
                    siblingX.leftChildTree.color = Black;
                    rightRotation(nodeX.parent);
                    nodeX = this.treeRoot;//case4
                }
            }
        }
        nodeX.color=Black;
    }
    private void RBTransplant(RBTree nodeU,RBTree nodeV){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        if(nodeU.parent.EnglishVersion.equals(leafNode.EnglishVersion)){
            this.treeRoot=nodeV;
        }
        else if(nodeU==nodeU.parent.leftChildTree){
            nodeU.parent.leftChildTree=nodeV;
        }
        else{
            nodeU.parent.rightChildTree=nodeV;
        }
        nodeV.parent=nodeU.parent;
    }
    private RBTree TreeSuccessor(RBTree node){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        RBTree y;
        if(node.rightChildTree!=leafNode){
            return TreeMinimum(node.rightChildTree);
        }
        y=node.parent;
        while(y!=null&&y!=leafNode&&node==y.rightChildTree){
            node=y;
            y=y.parent;
        }
        return y;
    }
    private RBTree TreeMinimum(RBTree minNode){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        while(!minNode.EnglishVersion.equals(leafNode.EnglishVersion) && !minNode.leftChildTree.EnglishVersion.equals(leafNode.EnglishVersion)){
            minNode=minNode.leftChildTree;
        }
        return minNode;
    }
    private void RBInsertion(String EnglishValue,String ChineseMeaning){//currently OK
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        RBTree insertKey=new RBTree();
        insertKey.EnglishVersion=EnglishValue;
        insertKey.ChineseVersion=ChineseMeaning;
        RBTree insertKeyParent=leafNode;
        RBTree x=treeRoot;
        if(searchByEnglishValue(EnglishValue).EnglishVersion.equals(EnglishValue)){
            System.out.println("Error:key conflict!"+" Conflicted key : "+EnglishValue);
        }
        else{//当检查得出key值不在树中
            ArraySortAndInsert(insertKey.EnglishVersion);
            while((x!=null)&&(!x.EnglishVersion.equals(leafNode.EnglishVersion))){//x不是叶节点且根节点非空
                /*if(x.leftChildTree==leafNode||x.rightChildTree==leafNode||x.EnglishVersion==""){
                    break;
                }*///为什么x会变成叶节点?应该排除了才对啊···****要是最后DDL前有空再来瞅一眼。
                insertKeyParent=x;
                if(WordOrderComparison(insertKey.EnglishVersion, x.EnglishVersion)<0){
                    x=x.leftChildTree;
                }
                else{
                    x=x.rightChildTree;
                }
            }
            insertKey.parent=insertKeyParent;
            if(insertKeyParent==leafNode){
                treeRoot=insertKey;
            }
            else if(WordOrderComparison(insertKey.EnglishVersion, insertKeyParent.EnglishVersion)<0){
                insertKeyParent.leftChildTree=insertKey;
            }
            else{
                insertKeyParent.rightChildTree=insertKey;
            }
            insertKey.rightChildTree=leafNode;
            insertKey.leftChildTree=leafNode;
            insertKey.color=Red;
            insertFixUp(insertKey);
        }
    }
    private void insertFixUp(RBTree insertKey){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        while(insertKey.parent.color==Red){
            if(insertKey.parent.parent==null||insertKey==treeRoot){
                break;
            }//insertKey's parent is treeNode
            if(insertKey.parent==insertKey.parent.parent.leftChildTree){
                RBTree RightUncle=insertKey.parent.parent.rightChildTree;
                if(RightUncle.color==Red){
                    insertKey.parent.color=Black;
                    insertKey.parent.parent.color=Red;
                    RightUncle.color=Black;
                    insertKey=insertKey.parent.parent;
                }//case1:叔叔为红色,change color
                else if(insertKey==insertKey.parent.rightChildTree){//与叔叔在同一侧
                    insertKey=insertKey.parent;//case2
                    leftRotation(insertKey);//case2
                    insertKey.parent.color=Black;//case3
                    insertKey.parent.parent.color=Red;
                    rightRotation(insertKey.parent.parent);
                }

                else if(insertKey==insertKey.parent.leftChildTree){
                    insertKey.parent.color=Black;
                    insertKey.parent.parent.color=Red;
                    rightRotation(insertKey.parent.parent);
                }//case3
            }//insertKey's parent is a left child
            else{//insertKey's parent is a right child
                RBTree leftUncle=insertKey.parent.parent.leftChildTree;
                if(leftUncle.color==Red){
                    insertKey.parent.color=Black;
                    insertKey.parent.parent.color=Red;
                    leftUncle.color=Black;
                    insertKey=insertKey.parent.parent;
                }//case1:叔叔为红色,change color
                else if(insertKey==insertKey.parent.leftChildTree){//与叔叔在同一侧
                    insertKey=insertKey.parent;
                    rightRotation(insertKey);//case2
                    insertKey.parent.color=Black;
                    insertKey.parent.parent.color=Red;
                    leftRotation(insertKey.parent.parent);
                }
                else if(insertKey==insertKey.parent.rightChildTree){
                    insertKey.parent.color=Black;
                    insertKey.parent.parent.color=Red;
                    leftRotation(insertKey.parent.parent);
                }//case3
            }

        }
        this.treeRoot.color=Black;
    }
    private void updateChineseMeaning(String EnglishValue,String ChineseMeaning){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        RBTree tempNode=this.treeRoot;
        while(tempNode!=null&& !tempNode.EnglishVersion.equals(leafNode.EnglishVersion)){
            if(WordOrderComparison(EnglishValue,tempNode.EnglishVersion)==0){
                tempNode.ChineseVersion=ChineseMeaning;
                break;
            }
            else if(WordOrderComparison(EnglishValue,tempNode.EnglishVersion)<0){
                tempNode=tempNode.leftChildTree;
            }
            else{
                tempNode=tempNode.rightChildTree;
            }
        }
    }
    public RBTree searchByEnglishValue(String EnglishValue){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        if(this.treeRoot==null){
            return leafNode;
        }
        RBTree tempNode=this.treeRoot;
        while(tempNode!=null&& !tempNode.EnglishVersion.equals(leafNode.EnglishVersion)){
            if(tempNode.leftChildTree.EnglishVersion.equals("") && tempNode.rightChildTree.EnglishVersion.equals("") && !tempNode.EnglishVersion.equals(EnglishValue)){
                return leafNode;
            }//若查找无该单词,返回leafNode
            if(WordOrderComparison(EnglishValue,tempNode.EnglishVersion)==0){
                return tempNode;
            }
            else if(WordOrderComparison(EnglishValue,tempNode.EnglishVersion)<0){
                tempNode=tempNode.leftChildTree;
            }
            else{
                tempNode=tempNode.rightChildTree;
            }
        }
        return tempNode;
    }//currently tested OK
    private int WordOrderComparison(String InsertWord,String ComparedWord){
        int insertWordLength=InsertWord.length();
        int comparedWordLength=ComparedWord.length();
        char[] insertWordCharArray=InsertWord.toUpperCase().toCharArray();
        char[] comparedWordCharArray=ComparedWord.toUpperCase().toCharArray();
        if(InsertWord.equals(ComparedWord)){
            return 0;//the word has existed
        }
        if(insertWordLength<=comparedWordLength){
            for(int i=0;i<insertWordLength;i++){
                if((i==insertWordLength-1)&&(insertWordCharArray[i]==comparedWordCharArray[i])){
                    return -1;//insert:exist;compared:existed
                }
                else if(insertWordCharArray[i]<comparedWordCharArray[i]){
                    return -1;
                }
                else if(insertWordCharArray[i]>comparedWordCharArray[i]){
                    return 1;
                }

            }
        }
        else{
            for(int i=0;i<comparedWordLength;i++){
                if((i==comparedWordLength-1)&&(insertWordCharArray[i]==comparedWordCharArray[i])){
                    return 1;//insert:exist;compared:existed
                }
                if(insertWordCharArray[i]<comparedWordCharArray[i]){
                    return -1;
                }
                else if(insertWordCharArray[i]>comparedWordCharArray[i]){
                    return 1;
                }
            }
        }
        return 0;
    }
    private void ArraySortAndInsert(String insertEnglishWord){
        int i=0;
        if(RBTreeArrayForKeySearch[0]==null){
            RBTreeArrayForKeySearch[0]=insertEnglishWord;
        }
        while(RBTreeArrayForKeySearch[i]!=null){
            if(WordOrderComparison(insertEnglishWord,RBTreeArrayForKeySearch[i])<0){
                for(int j=RBTreeArrayForKeySearch.length-1;j>=i;j--){
                    if(RBTreeArrayForKeySearch[j]!=null){
                        RBTreeArrayForKeySearch[j+1]=RBTreeArrayForKeySearch[j];
                    }
                }
                RBTreeArrayForKeySearch[i]=insertEnglishWord;
                break;
            }
            else if(RBTreeArrayForKeySearch[i+1]==null&&WordOrderComparison(insertEnglishWord,RBTreeArrayForKeySearch[i])>0){
                RBTreeArrayForKeySearch[i+1]=insertEnglishWord;
                break;
            }
            i++;
        }
    }
    private void leftRotation(RBTree Key){
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        RBTree Temp=Key.rightChildTree;
        Key.rightChildTree=Temp.leftChildTree;
        if(Key==treeRoot){
            this.treeRoot=Key.rightChildTree;
        }
        if(Temp.leftChildTree!=leafNode){
            Temp.leftChildTree.parent=Key;
        }
        Temp.parent=Key.parent;
        if(Key.parent==leafNode){
            this.treeRoot=Temp;
        }
        else if (Key== Key.parent.leftChildTree){
            Key.parent.leftChildTree=Temp;
        }
        else {
            Key.parent.rightChildTree=Temp;
        }
        Temp.leftChildTree=Key;
        Key.parent=Temp;
    }
    private void rightRotation(RBTree Key){
        //System.out.println(Key.EnglishVersion);
        if(Key==treeRoot){
            this.treeRoot=Key.leftChildTree;
        }
        RBTree leafNode=new RBTree();
        leafNode.color=Black;
        leafNode.EnglishVersion="";
        RBTree Temp=Key.leftChildTree;
        Key.leftChildTree=Temp.rightChildTree;
        if(Temp.rightChildTree!=leafNode){
            Temp.rightChildTree.parent=Key;
        }
        Temp.parent=Key.parent;
        if(Key.parent==leafNode){
            this.treeRoot=Temp;
        }
        else if (Key== Key.parent.rightChildTree){
            Key.parent.rightChildTree=Temp;
        }
        else {
            Key.parent.leftChildTree=Temp;
        }
        Temp.rightChildTree=Key;
        Key.parent=Temp;
    }
    private void printRBTree(RBTree rootNode){//目前测试OK
        RBTree leafNode=new RBTree();
        leafNode.EnglishVersion="";
        leafNode.color=Black;
        if(rootNode !=null){
            if (rootNode.EnglishVersion.equals("")) {
                System.out.print("");
            }
            else {
                printRBTree(rootNode.leftChildTree);
                if(rootNode.color==Black) {
                    System.out.println(rootNode.EnglishVersion + ":"+rootNode.ChineseVersion+" "+"Black");
                }
                else{
                    System.out.println(rootNode.EnglishVersion+":"+rootNode.ChineseVersion+" "+"Red");
                }
                printRBTree(rootNode.rightChildTree);
            }
        }
    }
}

main.java:

import java.io.*;
import java.util.Scanner;

class Main{
    public static void main(String[] args) throws IOException {
        RBTree rbTree = new RBTree();
        Scanner scanner = new Scanner(System.in);
        System.out.println("0 print windows.txt");
        System.out.println("1 insert");
        System.out.println("2 delete");
        System.out.println("3 search");
        System.out.println("4 update");
        System.out.println("5 exit");
        DataInputStream in = new DataInputStream(new FileInputStream("windows.txt"));
        BufferedReader read = new BufferedReader(new InputStreamReader(in));
        label:
        while (scanner.hasNext()) {
            String input = scanner.nextLine();
            switch (input) {
                case "0": {
                    System.out.println("read the windows.txt");
                    String EnglishVersion;
                    while ((EnglishVersion = read.readLine()) != null) {
                        String ChineseVersion = read.readLine();
                        rbTree.testInsert(EnglishVersion, ChineseVersion);
                    }
                    rbTree.testPrintRBTree();
                    break;
                }
                case "1": {
                    System.out.println("please input the word and the meaning separated by "+" "+"...");
                    input = scanner.nextLine();
                    String[] split = input.split("\\s+");
                    String EnglishVersion = split[0];
                    String ChineseVersion = split[1];
                    rbTree.testInsert(EnglishVersion, ChineseVersion);
                    rbTree.testPrintRBTree();
                    break;
                }
                case "2":
                    System.out.println("please input the word you need to delete"+"...");
                    input = scanner.nextLine();
                    rbTree.testRBDeletion(input);
                    rbTree.testPrintRBTree();
                    break;
                case "3":
                    System.out.println("please input the word you need to search"+"...");
                    input = scanner.nextLine();
                    rbTree.testSearch(input);
                    break;
                case "4":
                    System.out.println("please input the word you need to update"+"...");
                    input = scanner.nextLine();
                    String[] split = input.split("\\s+");
                    String EnglishVersion = split[0];
                    String ChineseVersion = split[1];
                    rbTree.testUpdate(EnglishVersion, ChineseVersion);
                    rbTree.testPrintRBTree();
                    break;
                case "5":
                    break label;
            }
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值