数据结构(四) 二叉查找树

1 性质

  1. 每个节点的儿子不超过 2 个
  2. 对于树中每个节点 X,它的左子树中所有项的值都小于 X 中的项,右子树中所有项的值都大于 X 中的项
  3. 平均深度为 O(logN),最大深度为 N – 1

2 实现

目前存储的是的是 int 类型和 String 类型的数据,如果想要进行其他类型的数据存储,只需要在 Element 类增加对应类型。

二叉树

package chapter4.binary_search_tree;

public class BinarySearchTree {
    private Node root;

    BinarySearchTree() {
        root = null;
    }

    public void clear() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    public boolean contains(Element x) {
        return contains(x, root);
    }


    public void insert(Element x) {
        root = insert(x, root);
    }

    public void remove(Element x) {
        root = remove(x, root);
    }


    public Element findMin() {
        return findMin(root);
    }

    public Element findMax() {
        return findMax(root);
    }

    public void printTree() {
        printTree(root);
    }

    private boolean contains(Element x, Node t) {
        if (t == null) {
            return false;
        }
        int comp = x.compareTo(t.getElement());
        if (comp == 0) {
            return true;
        } else if (comp > 0) {   //大于节点值,右子树中搜索
            return contains(x, t.getRight());
        } else {    //小于节点值,左子树中搜索
            return contains(x, t.getLeft());
        }
    }


    private Node insert(Element x, Node t) {
        if (t == null) {
            return new Node(x);
        }
        int comp = x.compareTo(t.getElement());
        if (comp > 0) {   //大于节点值,右子树中插入
            t.setRight(insert(x, t.getRight()));
        } else if (comp < 0) {    //小于节点值,左子树中插入
            t.setLeft(insert(x, t.getLeft()));
        } else {
            System.out.println("重复值,不插入");
        }
        return t;
    }

    private Node remove(Element x, Node t) {
        if (t == null) {   //要删除的值不存在
            return null;
        }
        int comp = x.compareTo(t.getElement());
        if (comp > 0) {   //大于节点值
            t.setRight(remove(x, t.getRight()));
        } else if (comp < 0) {    //小于节点值
            t.setLeft(remove(x, t.getLeft()));
        } else {        //要删除的就是该节点
            if (t.getLeft() == null) {     //左子树为空
                t = t.getRight();     //设为右子树
            } else if (t.getRight() == null) {   //右子树为空
                t = t.getLeft();      //设为左子树
            } else {         //左右子树都不为空
                t.setElement(findMin(t.getRight()));
                t.setRight(remove(t.getElement(), t.getRight()));
            }
        }
        return t;
    }

    private Element findMin(Node t) {
        if (t != null) {
            while (t.getLeft() != null) {
                t = t.getLeft();
            }
            return t.getElement();
        }
        return null;
    }

    private Element findMax(Node t) {
        if (t != null) {
            while (t.getRight() != null) {
                t = t.getRight();
            }
            return t.getElement();
        }
        return null;
    }

    /** * 中序遍历 * * @param t 父节点 */
    private void printTree(Node t) {
        if (t != null) {
            printTree(t.getLeft());
            t.getElement().print();
            printTree(t.getRight());
        }
    }
}

节点

package chapter4.binary_search_tree;

public class Node implements Comparable<Node> {

    private Node left;
    private Node right;
    private Element element;

    Node(Element element) {
        this(element, null, null);
    }

    Node(Element element, Node l, Node r) {
        this.element = element;
        left = l;
        right = r;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public void setElement(Element element) {
        this.element = element;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public Element getElement() {
        return element;
    }

    @Override
    public int compareTo(Node node) {
        return this.element.compareTo(node.element);
    }
}

节点元素

package chapter4.binary_search_tree;

public class Element implements Comparable<Element> {
    private int type;   //数据类型
    private int integer;
    private String string;

    Element(int i) {
        integer = i;
        type = 1;
    }

    Element(String s) {
        string = s;
        type = 2;
    }

    @Override
    public int compareTo(Element o) {
        if (type == 1) {
            return Integer.compare(this.integer, o.integer);
        } else if (type == 2) {
            return Integer.compare(this.string.length(), o.string.length());
        } else {
            System.out.println("无法比较");
            return 0;
        }
    }

    public void print() {
        if (type == 1) {
            System.out.println(integer);
        } else if (type == 2) {
            System.out.println(string);
        }
    }
}

测试类

package chapter4.binary_search_tree;

public class Test {
    public static void main(String args[]) {
        BinarySearchTree tree = new BinarySearchTree();

        Element element1 = new Element(21);
        Element element2 = new Element(0);
        Element element3 = new Element(12);
        Element element4 = new Element(2);
        Element element5 = new Element(32);
        Element element6 = new Element(16);
        Element element7 = new Element(14);
        Element element8 = new Element(-4);
        Element element9 = new Element(25);
        Element element10 = new Element(15);
        Element element11 = new Element(12);

        tree.insert(element1);
        tree.insert(element2);
        tree.insert(element3);
        tree.insert(element4);
        tree.insert(element5);
        tree.insert(element6);
        tree.insert(element7);
        tree.insert(element8);
        tree.insert(element9);

        System.out.println("intTree:");
        tree.printTree();
        System.out.println();

        System.out.print("min:");
        tree.findMin().print();
        System.out.print("max:");
        tree.findMax().print();

        System.out.println("contains 15:" + tree.contains(element10));
        System.out.println("contains 12:" + tree.contains(element11));
        System.out.println();

        tree.remove(element3);
        System.out.println("intTree:");
        tree.printTree();
        System.out.println();
        tree.clear();
        System.out.println("isEmpty:" + tree.isEmpty());

        Element element12 = new Element("abc");
        Element element13 = new Element("hello");

        tree.insert(element12);
        tree.insert(element13);

        System.out.println();
        System.out.println("stringTree:");
        tree.printTree();
    }
}

输出

intTree:
-4
0
2
12
14
16
21
25
32

min:-4
max:32
contains 15:false
contains 12:true

intTree:
-4
0
2
14
16
21
25
32

isEmpty:true

stringTree:
abc
hello
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/gtx1502/article/details/81588895
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞