package com.aic.jon.bst; import java.util.LinkedList; import java.util.Queue; public class BST<E extends Comparable<E>> { //Comparable接口,方便元素进行比较 private Node root; //根节点 private int size; //元素数量 private class Node { public E e; public Node left, right; //左节点,右节点 public Node(E e) { //构造方法,将左右节点赋值Null this.e = e; left = null; right = null; } } 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); } //向以node为根的二分搜索树插入元素e,递归算法 //返回插入新节点后的树根 private Node add(Node node, E e) { if (node == null) { size++; return 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); } //通过递归算法实现搜索 private boolean contains(Node node, E e) { if (node == null) { return false; } if (e.compareTo(node.e) &#
二叉树学习(递归删除任意节点元素)
最新推荐文章于 2022-10-09 20:40:22 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)