【二叉搜索树】Java版本,完美版本二叉搜索树,功能齐全

正文之前

经过几天下午四点到现在的功夫,然后结合算法导论和自己的一些小尝试,总算把Java版本的二叉树给做的完美了~ emm 不信的话大家伙尽管测试。。。终于!!!树形结构!用Java实现出来确实很麻烦啊!因为是传递引用,所以要特别注意。。一不小心就出错了。。而且我经常犯一些傻逼错误!真的是想哭!!!!

有点小改进: 【二叉搜索树-Java】改进了toString和gennerateTree

正文

import java.util.Arrays;
import java.util.Random;
import java.util.LinkedList;
import java.util.Queue;

class node{
    private int key;
    node leftChild;
    node rightChild;
    node parent;
    node(int x){
        this.key=x;
    }
    node(){};
    public int getKey(){
        return this.key;
    }
}

class Tree{
    node head;
    int size;
    Tree(){this.size = 0;}

    public node getHead(){
        return this.head;
    }

    private void walk(Queue<String> queue,node par, int level){
        String l = "";
        for (int i =0;i<level;++i) l+=" ---->>>> ";
        if (level == 1) queue.add(""+ par.getKey());
        if(par.leftChild!=null){
            queue.add(l+par.leftChild.getKey());
            walk(queue,par.leftChild,level+1);
        }
        if (par.rightChild!=null){
            queue.add(l+par.rightChild.getKey());
            walk(queue,par.rightChild,level+1);
        }
    }
    public String toString(){
        System.out.println("输出树形: ");
        Queue<String> queue = new LinkedList<>();
        String out = "";
        if (this.head!=null){
            String one = "ok";
            walk(queue,this.head,1);
            int count = 0;
            while(one!=null) {
                one = queue.poll();
                count++;
                if(one!=null)
                    out += (count +" : "+ one+"\n");
            }
        }
        return out;
    }
}

public class TestTree{
    private static node __findNode(node par,int z){
        if (par==null || par.getKey() == z)
            return par;
        if (z<par.getKey()) return __findNode(par.leftChild,z);
        else return __findNode(par.rightChild,z);
    }
    public static node  findNode(Tree tree,int z){
        node par = tree.getHead();
        return __findNode(par,z);
    }
    public static void transplant(Tree tree, node u, node r){
        if (u.parent == null) tree.head = r;
        else if (u == u.parent.leftChild){
            u.parent.leftChild = r;
        }
        else {
            u.parent.rightChild = r;
        }
        if (r!=null)
            u.parent = r.parent;
    }

    public static void nodeDelete(Tree tree, node z){
        tree.size-=1;
        if (z.leftChild==null)
            transplant(tree,z,z.rightChild);
        else if (z.rightChild==null)
            transplant(tree,z,z.leftChild);
        else {
            node y = minNode(z.rightChild);
            if (y.parent!=z){
                transplant(tree,y,y.rightChild);
                y.rightChild = z.rightChild;
                y.rightChild.parent = y;
            }
            transplant(tree,z,y);
            y.leftChild = z.leftChild;
            y.leftChild.parent = y;
        }
        System.out.println("Deleted :  "+ z.getKey()+"\n");
    }

    private static node minNode(node no){
        node x = no;
        while (x.leftChild!=null)
            x=x.leftChild;
        return x;
    }
    public static void insertSort(int []arr, int n){
        for (int i = 1; i < n; ++i)
        {
            int e = arr[i];
            int j=i;
            for (; j > 0; --j)
            {
                if (e < arr[j-1])
                    arr[j] = arr[j-1];
                else
                    break;
            }
            arr[j] = e;
        }
    }
    public static void insertToTree(Tree tree,int x){
        System.out.println("Insert into the Tree : "+x+"\n");
        tree.size+=1;
        node newnode = new node(x);
        node tmp = tree.getHead();
        if ( tmp==null)
            tree.head = newnode;
        while(tmp!=null) {
            if (x < tmp.getKey()) {
                if (tmp.leftChild==null || x > tmp.leftChild.getKey()) {
                    newnode.parent = tmp;
                    newnode.leftChild = tmp.leftChild;
                    tmp.leftChild = newnode;
                    return;
                }
                else
                    tmp = tmp.leftChild;
            }
            else {
                if ( tmp.rightChild==null || x < tmp.rightChild.getKey()) {
                    newnode.parent = tmp;
                    newnode.rightChild = tmp.rightChild;
                    tmp.rightChild = newnode;
                    return;
                }
                else
                    tmp = tmp.rightChild;
            }
        }
    }
    public static Tree generateTree(int[] arr){
        Tree tree = new Tree();
//        insertSort(arr,arr.length);
        insertToTree(tree,arr[arr.length/2]);
        for (int i=0;i<arr.length;++i) {
            if (i!=arr.length/2) {
                tree.size+=1;
                node newnode = new node(arr[i]);
                node tmp = tree.getHead();
                while(tmp!=null) {
                    if (arr[i] < tmp.getKey()) {
                        if (tmp.leftChild==null) {
                            newnode.parent = tmp;
                            newnode.leftChild = null;
                            tmp.leftChild = newnode;
                            break;
                        }
                        else
                            tmp = tmp.leftChild;
                    }
                    else {
                        if ( tmp.rightChild==null) {
                            newnode.parent = tmp;
                            newnode.rightChild =null;
                            tmp.rightChild = newnode;
                            break;
                        }
                        else
                            tmp = tmp.rightChild;
                    }
                }
            }
        }
        return tree;
    }
    public static void main(String[] args) {
        double start = System.currentTimeMillis();
        Random rdm = new Random(System.currentTimeMillis());
        int [] randomArr = new int[20];
        for(int i=0;i<20;i++)
            randomArr[i] = Math.abs(rdm.nextInt())%500 +1;
        Tree tree = generateTree(randomArr);
        System.out.println(Arrays.toString(randomArr));
        System.out.println(tree.toString());
        nodeDelete(tree,findNode(tree,randomArr[Math.abs(rdm.nextInt())%tree.size+1]));
        System.out.println(tree.toString());
        insertToTree(tree,Math.abs(rdm.nextInt())%500 +1);
        System.out.println(tree.toString());
        double end  = System.currentTimeMillis();
        System.out.println("Usage of Time: "+(end-start));
    }
}

注释什么的就不写了。都是很简单的部分,大概也就几个递归搞得我比较烦恼。。emm 然后就是删除我是直接借鉴了《算法导论》的伪代码实现的Java版本,真的厉害啊。。照着做就OK了。。。完全没想太多。。不过好歹还是吃透了。而且辅助函数都是自己写的好伐~

emm 下面看看使用过程的output:

Insert into the Tree : 241

[340, 286, 171, 462, 20, 262, 436, 184, 30, 50, 241, 373, 301, 461, 329, 120, 91, 375, 98, 105]
输出树形: 
1 : 241
2 :  ---->>>> 171
3 :  ---->>>>  ---->>>> 20
4 :  ---->>>>  ---->>>>  ---->>>> 30
5 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 50
6 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 120
7 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 91
8 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 98
9 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 105
10 :  ---->>>>  ---->>>> 184
11 :  ---->>>> 340
12 :  ---->>>>  ---->>>> 286
13 :  ---->>>>  ---->>>>  ---->>>> 262
14 :  ---->>>>  ---->>>>  ---->>>> 301
15 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 329
16 :  ---->>>>  ---->>>> 462
17 :  ---->>>>  ---->>>>  ---->>>> 436
18 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 373
19 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 375
20 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 461

Deleted :  120

输出树形: 
1 : 241
2 :  ---->>>> 171
3 :  ---->>>>  ---->>>> 20
4 :  ---->>>>  ---->>>>  ---->>>> 30
5 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 50
6 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 91
7 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 98
8 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 105
9 :  ---->>>>  ---->>>> 184
10 :  ---->>>> 340
11 :  ---->>>>  ---->>>> 286
12 :  ---->>>>  ---->>>>  ---->>>> 262
13 :  ---->>>>  ---->>>>  ---->>>> 301
14 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 329
15 :  ---->>>>  ---->>>> 462
16 :  ---->>>>  ---->>>>  ---->>>> 436
17 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 373
18 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 375
19 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 461

Insert into the Tree : 47

输出树形: 
1 : 241
2 :  ---->>>> 171
3 :  ---->>>>  ---->>>> 47
4 :  ---->>>>  ---->>>>  ---->>>> 20
5 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 30
6 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 50
7 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 91
8 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 98
9 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 105
10 :  ---->>>>  ---->>>> 184
11 :  ---->>>> 340
12 :  ---->>>>  ---->>>> 286
13 :  ---->>>>  ---->>>>  ---->>>> 262
14 :  ---->>>>  ---->>>>  ---->>>> 301
15 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 329
16 :  ---->>>>  ---->>>> 462
17 :  ---->>>>  ---->>>>  ---->>>> 436
18 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 373
19 :  ---->>>>  ---->>>>  ---->>>>  ---->>>>  ---->>>> 375
20 :  ---->>>>  ---->>>>  ---->>>>  ---->>>> 461

Usage of Time: 3.0

Process finished with exit code 0

正文之后

我是实在懒得去toString搞得尽善尽美了,现在这个样子大概也能看吧。。就一个先序遍历,,emm 爱看不看。。反正我是觉得基本没bug了。。。哈哈哈~

背景插图,自己下载哈!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容