结构作用
如上图所示每个节点都大于左孩子,小于右孩子,以O(logn)复杂度进行如下操作
- 插入一个元素
- 查询是否包含某个元素
- 删除某个元素
插入元素7:从根节点出发,7>3,往右边走,7>5,往右边走,7>6&&6的右节点为空,把7插入到6的右孩子
删除节点:如果删除的节点是根节点,注意修改root,叶子节点找到后直接删除,例如1,直接把2的左孩子置为null
非叶子节点分为
- 只含有左孩子的节点例如2,3.left=2.left即可
- 只含有右孩子的节点例如6,5.right=6.right即可
- 既有左孩子又有右孩子例如5,此时需要从5的左右子树选出一个节点顶替5,如果选择左子树,需要选择左子树中最大的节点,如果选择右子树,需要选择右子树最小的节点。这里我们选择右子树,那么使用6来顶替5,6.left=5.left, 3.right=6
实现
public class BSTTree<E extends Comparable<E>> {
private class Node{
public E e;
public Node left,right;
public Node(E e){
this.e = e;
}
}
private Node root;
private int size;
public int size() {
return size;
}
public boolean isEmpty() {
return size==0;
}
//非递归
public boolean addNR(E e){
if(null==e){
return false;
}
if(null==root){
root = new Node(e);
size++;
return true;
}
Node temp = null;
Node cur = root;
while(cur!=null){
temp = cur;
if(e.compareTo(cur.e)==0){
return false;
}else if(e.compareTo(cur.e)<0){
cur = cur.left;
if(null==cur){
size++;
temp.left = new Node(e);
}
}else{
cur = cur.right;
if(null==cur){
size++;
temp.right = new Node(e);
}
}
}
return true;
}
public boolean add(E e) {
if(null==e){
return false;
}
int tempSize = size;
root = add(root,e);
return size==tempSize+1;
}
private Node add(Node node,E e){
if(null==node){
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 containsNR(E e){
if(null==e){
return false;
}
if(null==root)
return false;
Node cur = root;
while(cur!=null){
if(e.compareTo(cur.e)==0){
return true;
}else if(e.compareTo(cur.e)<0){
cur = cur.left;
}else{
cur = cur.right;
}
}
return false;
}
public boolean contains(E e) {
if(null==e){
return false;
}
return contains(root,e);
}
private boolean contains(Node node, E e) {
if(null==node){
return false;
}
if(e.compareTo(node.e)<0){
return contains(node.left,e);
}else if(e.compareTo(node.e)>0){
return contains(node.right,e);
}else{
return true;
}
}
public List<E> preOrder(){
List<E> list = new ArrayList<>();
preOrder(root,list);
return list;
}
private void preOrder(Node node,List<E> list){
if(null==node)
return;
list.add(node.e);
preOrder(node.left,list);
preOrder(node.right,list);
}
public List<E> preOrderNR() {
List<E> list = new ArrayList<>();
Stack<Node> stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
Node cur = stack.pop();
list.add(cur.e);
if(cur.right!=null){
stack.push(cur.right);
}
if(cur.left!=null){
stack.push(cur.left);
}
}
return list;
}
public List<E> inOrder(){
List<E> list = new ArrayList<>();
inOrder(root,list);
return list;
}
private void inOrder(Node node,List<E> list){
if(null==node)
return;
preOrder(node.left,list);
list.add(node.e);
preOrder(node.right,list);
}
public List<E> inOrderNR() {
List<E> list = new ArrayList<>();
Stack<Node> stack = new Stack();
Node cur = root;
while(!stack.isEmpty()||cur!=null){
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
//左边走完了,cur==null
if(!stack.isEmpty()){
cur = stack.pop();
list.add(cur.e);
cur = cur.right;
}
}
return list;
}
public List<E> postOrder(){
List<E> list = new ArrayList<>();
postOrder(root,list);
return list;
}
private void postOrder(Node node,List<E> list){
if(null==node)
return;
preOrder(node.left,list);
preOrder(node.right,list);
list.add(node.e);
}
public List<E> levelOrder(){
Queue<Node> queue = new LinkedList<>();
queue.add(root);
List<E> list = new ArrayList<>();
while(!queue.isEmpty()){
Node cur = queue.poll();
list.add(cur.e);
if(cur.left!=null){
queue.add(cur.left);
}
if(cur.right!=null){
queue.add(cur.right);
}
}
return list;
}
public E removeMin(){
E min = min();
root = removeMin(root);
return min;
}
private Node removeMin(Node node) {
if(node.left==null){
Node right = node.right;
size--;
node.right = null;
return right;
}
node.left = removeMin(node.left);
return node;
}
public E removeMax(){
E max = max();
root = removeMax(root);
return max;
}
private Node removeMax(Node node) {
if(node.right==null){
Node left = node.left;
size--;
node.left = null;
return left;
}
node.right = removeMax(node.right);
return node;
}
/**
* 删除最小值所在节点,返回最小值
* @return
*/
public E removeMinNR(){
E min = min();
Node cur = root;
Node par = cur;
while(cur.left!=null){
par = cur;
cur = cur.left;
}
//cur是最小值,par是cur的父亲
if(cur==root){
root=null;
}else{
par.left = cur.right;
}
size--;
return min;
}
/**
* 删除最大值所在节点,返回最大值
* @return
*/
public E removeMaxNR(){
E max = max();
Node cur = root;
Node par = cur;
while(cur.right!=null){
par = cur;
cur = cur.right;
}
//cur是最小值,par是cur的父亲
if(cur==root){
root=null;
}else{
par.right = cur.left;
}
size--;
return max;
}
public Node parentNR(E e){
if(null==e){
return null;
}
Node cur = root;
Node par = null;
while(cur!=null){
if(e.compareTo(cur.e)<0){
par = cur;
cur = cur.left;
}else if(e.compareTo(cur.e)>0){
par = cur;
cur = cur.right;
}else{
return par;
}
}
return null;
}
public boolean remove(E e) {
if(null==e)
return false;
int tempSize = size;
root = remove(root,e);
return size == tempSize-1;
}
private Node remove(Node node, E e) {
if(null==node){
return null;
}
if(e.compareTo(node.e)<0){
node.left = remove(node.left,e);
return node;
}else if(e.compareTo(node.e)>0){
node.right = remove(node.right,e);
return node;
}else{
//待删除节点右子树为空
if(node.right==null){
Node left = node.left;
size--;
node.left = null;
return left;
}
//待删除节点左子树为空
if(node.left==null){
Node right = node.right;
size--;
node.right = null;
return right;
}
// 待删除节点左右子树均不为空的情况
// 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
// 用这个节点顶替待删除节点的位置
Node successor = min(node.right);
successor.left = node.left;
successor.right = removeMin(node.right);
node.left = node.right = null;
return successor;
}
}
public E min(){
if(size == 0)
throw new IllegalArgumentException("BST is empty!");
return min(root).e;
}
private Node min(Node node){
if(node.left==null)
return node;
return min(node.left);
}
public E max(){
if(size == 0)
throw new IllegalArgumentException("BST is empty!");
return max(root).e;
}
private Node max(Node node){
if(node.right==null)
return node;
return max(node.right);
}
public List<String> path(){
List<String> res = new ArrayList<>();
if(size==0)
return res;
path(root,"",res);
return res;
}
private void path(Node node, String s, List<String> res) {
if(node.left==null&&node.right==null){
res.add(s+node.e);
}
if(node.left!=null){
path(node.left,s+node.e+"->",res);
}
if(node.right!=null){
path(node.right,s+node.e+"->",res);
}
}
//找到pq最近公共祖先
public Node lowestCommonAncestor(E p, E q){
return lowestCommonAncestor(root,p,q);
}
public Node lowestCommonAncestor(Node node,E p, E q){
//pq全在右子树
//pq全在左子树
//else 找到了
if(p.compareTo(node.e)>0&&q.compareTo(node.e)>0){
return lowestCommonAncestor(node.right,p,q);
}else if(p.compareTo(node.e)<0&&q.compareTo(node.e)<0){
return lowestCommonAncestor(node.left,p,q);
}else{
return node;
}
}
public static void main(String[] args) {
BSTTree<Integer> tree = new BSTTree<>();
tree.addNR(2);
tree.addNR(1);
tree.addNR(3);
System.out.println(tree.path());
tree.removeMax();
System.out.println(tree.path());
tree.remove(2);
System.out.println(tree.path());
tree.remove(1);
System.out.println(tree.path());
}
}