java——二分搜索树(递归非递归)

Posted 高圈圈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java——二分搜索树(递归非递归)相关的知识,希望对你有一定的参考价值。

~

import java.util.Stack;
import java.util.LinkedList;
import java.util.Queue;

//二分搜索树
public class BST <E extends Comparable<E>> {
    private class Node{
        public E e;
        public Node left, right;
        
        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }
    
    private Node root;
    private int size;
    public BST() {
        root = null;
        size = 0;
    }
    public int size() {
        return size;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    //相等的元素不会重复添加
    public void add(E e) {
        root = add(root, e);
    }
    private Node add(Node node, E e) {
        if(node == null) {
            size ++;
            node = new Node(e);
        }
        if(e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        }else if(e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }
        return node;
    }
    public boolean contains(E e) {
        return contains(root, e);
    }
    //搜索二分搜索树是否包含元素e
    private boolean contains(Node node, E e) {
        if(node == null)
            return false;
        if(e.compareTo(node.e) == 0) {
            return true;
        }else if(e.compareTo(node.e) > 0) {
            return contains(node.right, e);
        }else {
            return contains(node.left, e);
        }
    }
    //前序遍历
    public void preOrder() {
        preOrder(root);
    }
    private void preOrder(Node node) {
        if(node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }
    //非递归前序遍历:栈
    public void preOrderNR() {
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()) {
            Node cur = stack.pop();
            System.out.println(cur.e);
            if(cur.right != null)
                stack.push(cur.right);
            if(cur.left != null) {
                stack.push(cur.left);
            }
        }
    }
    //中序遍历
    public void inOrder() {
        preOrder(root);
    }
    private void inOrder(Node node) {
        if(node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }
    //后序遍历
    public void postOrder() {
        postOrder(root);
    }
    private void postOrder(Node node){
        if(node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }
    //非递归 层序遍历(广度优先遍历):队列
    public void levelOrder() {
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()) {
            Node cur = q.remove();
            System.out.println(cur.e);
            if(cur.left != null) {
                q.add(cur.left);
            }
            if(cur.right != null) {
                q.add(cur.right);
            }
        }
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        generateBSTString(root, 0, res);
        return res.toString();
    }
    private void generateBSTString(Node node, int depth, StringBuilder res) {
        if(node == null) {
            res.append(generateDepthString(depth) + "null
");
            return;
        }
        res.append(generateDepthString(depth) + node.e + "
");
        generateBSTString(node.left, depth+1, res);
        generateBSTString(node.right, depth+1, res);
    }
    
    private String generateDepthString(int depth) {
        StringBuilder res = new StringBuilder();
        for(int i = 0 ; i < depth ; i ++) {
            res.append("--");
        }
        return res.toString();
    }
    
    public static void main(String[] args) {
        BST<Integer> bst = new BST<>();
        int[] nums = {5,3,6,8,4,2};
        for(int num: nums) {
            bst.add(num);
        }
        bst.preOrder();
        System.out.println(bst);;
    }
}

 

以上是关于java——二分搜索树(递归非递归)的主要内容,如果未能解决你的问题,请参考以下文章

二分搜索的非递归实现

Day589.二分查找(非递归) -数据结构和算法Java

二分搜索(非递归)

二分查找算法(递归与非递归两种方式)

数据结构-PHP 实现二分搜索树

字节跳动面试210615大数据java(搜索二叉树后续遍历,非递归)