手写查找二叉树

查找二叉树

随着大数据时代的来临,树形结构得到了越来越广泛的应用,废话不多说,直接开始我们的正题,查找二叉树。

何为查找二叉树

查找二叉树是二叉树的一种,又名查找树,搜索树。查找二叉树具有如下特点;

1.必须是二叉树

2.任意节点的左子树上所有的数据都比该节点数据小,或者为空树

3.任意节点的右子树上所有的数据都比该节点数据大,或者为空树

应用场景

顾名思义,查找二叉树的作用就是快速查找 ,使用于数据量特别大时候的快速查找,时间复杂度介于O(log2n)到O(n)之间,数据量越大,越接近于O(log2n),性能越优,大数据时代的必备技能之一。

代码实现

一.使用孩子双亲表示法表示树

 public class Node <T extends Comparable>{
    T item;
    Node<T> leftChild;
    Node<T> rightChild;
    Node<T> parent;

    public Node(T item) {
        this.item = item;
    }
}

二.定义属性变量

Node root;
int size;

三.实现必要的增删查询和遍历方法

add 方法实现

  public boolean add(T item) {
    Node<T> newNode = new Node<T>(item);
    if (root == null) {
        //如果是空树
        root = newNode;
        size++;
        return true;
    }
    Node node = root;
    Node parent = null;
    while (node != null) {
        parent = node;
        if (node.item.compareTo(item) == 0) {
            //去掉重复的,不进行添加操作
            return false;
        } else if (node.item .compareTo(item) >0) {
            //如果加入的节点比做节点小
            node = node.leftChild;
        } else {
            //如果加入的节点比做节点大
            node = node.rightChild;
        }
    }
    if (parent.item .compareTo(item)>0) {
        parent.leftChild = newNode;
    } else {
        parent.rightChild = newNode;
    }
    newNode.parent = parent;
    size++;
    return  true;
}

get方法实现

public Node get(T item) {
    if (root == null) {
        return null;
    }
    Node node = root;
    while (node != null) {
        if (node.item.compareTo(item)<0) {
            node = node.rightChild;
        } else if (node.item.compareTo(item)>0) {
            node = node.leftChild;
        } else {
            return node;
        }
    }
    return null;
}

遍历

/**
 * 采取中序遍历的方式对树进行遍历
 */
 public void midOrderTraseval(){
    midOrderTraseval(root);
}

private  void midOrderTraseval(Node<T> node) {
    if (node == null) {
        return;
    }
    midOrderTraseval(node.leftChild);
    System.out.print(node.item +" ");
    midOrderTraseval(node.rightChild);
}

delete方法(难点)

思路:

1.如果删除的节点左右子树都为空,即叶子节点时:

      Node<T> left = node.leftChild;
      Node<T> right = node.rightChild;
      Node<T> parent = node.parent;
      //第一种情况 node 为叶子节点

      if (left == null && right == null) {
        if(parent == null){
            root = null;
        }else {
            if (parent.leftChild == node) {
                parent.leftChild = null;
            } else {
                parent.rightChild = null;
            }
        }
        node.parent = null;
      } 

2.如果删除的节点只有右子树为空时:

     else if (left != null && right == null) {
        //第二种情况,只有左节点,没有右节点
        if(parent == null){
            root = left;
        }else {
            if (parent.leftChild == node) {
                parent.leftChild = left;
            } else {
                parent.rightChild = left;
            }
        }
        left.parent = parent;
        node.parent = null;
        node.leftChild = null;
    }

3.如果删除的节点只有左子树为空时:

      else if (left == null && right != null) {
        //第三种情况,只有右节点,没有左节点
        if(parent == null){
            root = right;
        }else {
            if (parent.leftChild == node) {
                parent.leftChild = right;
            } else {
                parent.rightChild = right;
            }
        }
        right.parent = parent;
        node.parent = null;
        node.rightChild = null;
    }

4.删除节点的左右子树均不为空时:

  else {
        //左右两边都有节点的
        Node lleftNode = getLeftChildNode(right);
        lleftNode.leftChild = left;
        left.parent = lleftNode;
        if(lleftNode.rightChild !=null){
            if(lleftNode.parent != node){
                lleftNode.parent.leftChild = lleftNode.rightChild;
                lleftNode.rightChild.parent = lleftNode.parent;
            }
        }
        if(lleftNode == right){
            lleftNode.rightChild =null;
        }else {
            lleftNode.rightChild = right;
        }
        right.parent = lleftNode;
        if(parent == null){
            lleftNode.parent = null;
            root = lleftNode;
        }else {
            if(parent.leftChild == node){
                parent.leftChild = lleftNode;
            }else {
                parent.rightChild = lleftNode;
            }
            lleftNode.parent = parent;
        }

        node.leftChild = null;
        node.rightChild =null;
        node.parent = null;
    }

完整代码

package com.example.administrator.myapplication;

/**
 * Created by Administrator on 2018-12-09.
 */

public class SearchBinaryTree<T extends Comparable> {
   Node root;
    int size;

    public class Node <T extends Comparable>{
        T item;
        Node<T> leftChild;
        Node<T> rightChild;
        Node<T> parent;

        public Node(T item) {
            this.item = item;
        }
    }


    public int size() {
        return size;
    }

public boolean add(T item) {
    Node<T> newNode = new Node<T>(item);
    if (root == null) {
        //如果是空树
        root = newNode;
        size++;
        return true;
    }
    Node node = root;
    Node parent = null;
    while (node != null) {
        parent = node;
        if (node.item.compareTo(item) == 0) {
            //去掉重复的,不进行添加操作
            return false;
        } else if (node.item .compareTo(item) >0) {
            //如果加入的节点比做节点小
            node = node.leftChild;
        } else {
            //如果加入的节点比做节点大
            node = node.rightChild;
        }
    }
    if (parent.item .compareTo(item)>0) {
        parent.leftChild = newNode;
    } else {
        parent.rightChild = newNode;
    }
    newNode.parent = parent;
    size++;
    return  true;
}


    public Node get(T item) {
        if (root == null) {
            return null;
        }
        Node node = root;
        while (node != null) {
            if (node.item.compareTo(item)<0) {
                node = node.rightChild;
            } else if (node.item.compareTo(item)>0) {
                node = node.leftChild;
            } else {
                return node;
            }
        }
        return null;
    }

/**
 * 采取中序遍历的方式对树进行遍历
 */
public void midOrderTraseval(){
    midOrderTraseval(root);
}

private  void midOrderTraseval(Node<T> node) {
    if (node == null) {
        return;
    }
    midOrderTraseval(node.leftChild);
    System.out.print(node.item +" ");
    midOrderTraseval(node.rightChild);
}


public Node<T> delete(T item){
    Node<T> node = get(item);
    if(node !=null){
        delete(node);
    }
    return node;
}

private void delete(Node<T> node) {
    if (node == null) {
        return;
    }
    Node<T> left = node.leftChild;
    Node<T> right = node.rightChild;
    Node<T> parent = node.parent;
    //第一种情况 node 为叶子节点
    if (left == null && right == null) {
        if(parent == null){
            root = null;
        }else {
            if (parent.leftChild == node) {
                parent.leftChild = null;
            } else {
                parent.rightChild = null;
            }
        }
        node.parent = null;
    } else if (left != null && right == null) {
        //第二种情况,只有左节点,没有右节点
        if(parent == null){
            root = left;
        }else {
            if (parent.leftChild == node) {
                parent.leftChild = left;
            } else {
                parent.rightChild = left;
            }
        }
        left.parent = parent;
        node.parent = null;
        node.leftChild = null;
    }else if (left == null && right != null) {
        //第三种情况,只有右节点,没有左节点
        if(parent == null){
            root = right;
        }else {
            if (parent.leftChild == node) {
                parent.leftChild = right;
            } else {
                parent.rightChild = right;
            }
        }
        right.parent = parent;
        node.parent = null;
        node.rightChild = null;
    }else {
        //左右两边都有节点的
        Node lleftNode = getLeftChildNode(right);
        lleftNode.leftChild = left;
        left.parent = lleftNode;
        if(lleftNode.rightChild !=null){
            if(lleftNode.parent != node){
                lleftNode.parent.leftChild = lleftNode.rightChild;
                lleftNode.rightChild.parent = lleftNode.parent;
            }
        }
        if(lleftNode == right){
            lleftNode.rightChild =null;
        }else {
            lleftNode.rightChild = right;
        }
        right.parent = lleftNode;
        if(parent == null){
            lleftNode.parent = null;
            root = lleftNode;
        }else {
            if(parent.leftChild == node){
                parent.leftChild = lleftNode;
            }else {
                parent.rightChild = lleftNode;
            }
            lleftNode.parent = parent;
        }

        node.leftChild = null;
        node.rightChild =null;
        node.parent = null;
    }
    size--;
}


private Node<T> getLeftChildNode(Node<T> node){
    Node<T> leftChild = node;
    Node<T> newNode = node;
    while (newNode!=null){
        leftChild = newNode;
        newNode = newNode.leftChild;
    }
    return leftChild;
}

}

测试及结果打印

 @Test
    public void testSearchTree(){
        SearchBinaryTree<Integer> tree = new SearchBinaryTree();
        int[] array = new int[]{5,4,9,2,0,7,3,1,8};
        for (int i : array) {
            tree.add(i);
         }
        for (int i : array) {
            tree.midOrderTraseval();
            System.out.println("-----------------------");
            tree.delete(i);
        }
        for (int i : array) {
            tree.add(i);
        }
        tree.midOrderTraseval();
    }

结果打印

      0 1 2 3 4 5 7 8 9 -----------------------
      0 1 2 3 4 7 8 9 -----------------------
      0 1 2 3 7 8 9 -----------------------
      0 1 2 3 7 8 -----------------------
      0 1 3 7 8 -----------------------
      1 3 7 8 -----------------------
      1 3 8 -----------------------
      1 8 -----------------------
      8 -----------------------
      0 1 2 3 4 5 7 8 9 

完美收工:

初次写技术博客,各位大牛请多包涵,共同进步!

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

推荐阅读更多精彩内容

  • 一些概念 数据结构就是研究数据的逻辑结构和物理结构以及它们之间相互关系,并对这种结构定义相应的运算,而且确保经过这...
    Winterfell_Z阅读 5,806评论 0 13
  • 查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找。本文...
    北方蜘蛛阅读 2,886评论 1 4
  • 按上一篇TOP 5的提纲,记述如下。 Chapter 1 雪温泉# 北陆,日本的“蜂腰”。因被日本的“阿尔卑斯山”...
    是介波阅读 630评论 2 0
  • IO模型 一、NIO原理 Netty 是基于Java NIO 封装的网络通讯框架,只有充分理解了 Java NIO...
    康康不遛猫阅读 2,372评论 0 3
  • 英语: 每日一句,对待公平应该分明 数学: 真题概率专题之独立性与相关性 政治: 解放战争,解放区土改,《中国土地...
    疯狂太阳花阅读 104评论 0 0