一、前驱节点
二、后继节点
代码以二叉搜索树为例:
// 前驱节点
public Node<E> predecessor(Node<E> node) {
if (node == null)
return null;
// 前驱节点在左子树当中(left.right.right.........)
Node<E> p = node.left;
if (p != null) {
while (p.right != null) {
p = p.right;
}
return p;
}
// 从父节点、祖父节点当中去找前驱节点
while (node.parent != null && node == node.parent.right) {
node = node.parent;
}
// node.parent == null && node.left == null
return node.parent;
}
// 后继节点
public Node<E> successor(Node<E> node) {
if (node == null)
return null;
// 前驱节点在左子树当中(right.left.left.........)
Node<E> p = node.right;
if (p != null) {
while (p.left != null) {
p = p.left;
}
return p;
}
// 从父节点、祖父节点当中去找前驱节点
while (node.parent != null && node == node.parent.left) {
node = node.parent;
}
// node.parent == null && node.right == null
return node.parent;
}
三、完善二叉搜索树代码,remove只针对二叉搜索树
删除代码:
public void remove(E element) {
remove(node(element));
}
private void remove(Node<E> node) {
if (node == null) {
return;
}
size --;
//度为2的节点
if (node.hasTwoChildrenNode()) {
//找到后继节点
Node<E> s = successor(node);
//用后继节点的值覆盖度为2的节点的值
node.element = s.element;
//删除后继节点
node = s;
}
//删除node节点(node的度必然是1或0)
Node<E> replacement = node.left != null ? node.left:node.right;
if (replacement != null) {//node是度为1节点
//更改parent
replacement.parent = node.parent;
//更改parent的left/right的指向
if (node.parent == null) {//node是度为1的节点,并且是根节点
root = replacement;
}else if (node == node.parent.left) {
node.parent.left = replacement;
}else { //node == node.parent.right
node.parent.right = replacement;
}
}else if (node.parent == null) {//node是叶子节点并且是根节点
root = null;
}else {//node是叶子节点,但不是根节点
if (node == node.parent.right) {
node.parent.right = null;
}else {//node == node.parent.left
node.parent.left = null;
}
}
}
private Node<E> node(E element) {
Node<E> node = root;
while (node != null) {
int cmp = compare(element, node.element);
if (cmp == 0) {
return node;
}
if (cmp > 0) {
node = node.right;
}else {
node = node.left;
}
}
return null;
}
二叉搜索树完整代码
package com.weyan;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import com.weyan.printer.BinaryTreeInfo;
@SuppressWarnings("unused")
//使用自定义打印器 需要实现BinaryTreeInfo类中的几个方法:root/ left/ right/ string
public class BinarySearchTree<E> implements BinaryTreeInfo {
private int size;
// 根节点
private Node<E> root;
private Comparator<E> comparator;
// 构造函数 不传入参数
public BinarySearchTree() {
this(null);
}
// 构造函数 传入参数:比较器
public BinarySearchTree(Comparator<E> comparator) {
this.comparator = comparator;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void clear() {
root = null;
size = 0;
}
public void add(E element) {
elementNotNullCheck(element);
// 添加第一个节点
if (root == null) {
root = new Node<>(element, null);
size++;
return;
}
// 添加的不是第一个节点
// 找到父节点
Node<E> parent = root;
Node<E> node = root;
int cmp = 0;
while (node != null) {
parent = node;
cmp = compare(element, node.element);
if (cmp > 0) {
node = node.right;
} else if (cmp < 0) {
node = node.left;
} else {// 相等
node.element = element;
return;
}
}
// 看看插入到父节点的哪个位置
Node<E> newNode = new Node<E>(element, parent);
if (cmp > 0) {
parent.right = newNode;
} else if (cmp < 0) {
parent.left = newNode;
}
size++;
}
public void remove(E element) {
remove(node(element));
}
private void remove(Node<E> node) {
if (node == null) {
return;
}
size --;
//度为2的节点
if (node.hasTwoChildrenNode()) {
//找到后继节点
Node<E> s = successor(node);
//用后继节点的值覆盖度为2的节点的值
node.element = s.element;
//删除后继节点
node = s;
}
//删除node节点(node的度必然是1或0)
Node<E> replacement = node.left != null ? node.left:node.right;
if (replacement != null) {//node是度为1节点
//更改parent
replacement.parent = node.parent;
//更改parent的left/right的指向
if (node.parent == null) {//node是度为1的节点,并且是根节点
root = replacement;
}else if (node == node.parent.left) {
node.parent.left = replacement;
}else { //node == node.parent.right
node.parent.right = replacement;
}
}else if (node.parent == null) {//node是叶子节点并且是根节点
root = null;
}else {//node是叶子节点,但不是根节点
if (node == node.parent.right) {
node.parent.right = null;
}else {//node == node.parent.left
node.parent.left = null;
}
}
}
//根据元素查找对应的节点
private Node<E> node(E element) {
Node<E> node = root;
while (node != null) {
int cmp = compare(element, node.element);
if (cmp == 0) {
return node;
}
if (cmp > 0) {
node = node.right;
}else {
node = node.left;
}
}
return null;
}
///是否包含一个元素
public boolean contains(E element) {
return node(element) != null;
}
/** ---递归写法--- **/
// 二叉树的高度
// public int height() {
// return nodeHeight(root);
// }
// 节点高度
// private int nodeHeight(Node<E> node) {
// if (node == null) return 0;
// return 1 + Math.max(nodeHeight(node.left), nodeHeight(node.right));
// }
/** ---层序遍历写法--- **/
// 二叉树的高度
public int height() {
if (root == null)
return 0;
int height = 0;
// 存储着每一层的元素数量
int levelSize = 1;
Queue<Node<E>> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node<E> node = queue.poll();
levelSize--;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
if (levelSize == 0) {// 意味着即将要访问下一层
levelSize = queue.size();
height++;
}
}
return height;
}
// 比较两个节点,返回值==0代表e1和e2相等;返回值>0代表e1>e2;返回值<0代表e1<e2
@SuppressWarnings("unchecked")
private int compare(E e1, E e2) {
if (comparator != null) {
return comparator.compare(e1, e2);
}
return ((Comparable<E>) e1).compareTo(e2);
}
// 判断一个节点是否为空
private void elementNotNullCheck(E element) {
if (element == null) {
throw new IllegalArgumentException("element must not be null");
}
}
// 前序遍历(递归方法)
public void preorderTraversal() {
preorderTraversal(root);
}
private void preorderTraversal(Node<E> node) {
if (node == null)
return;
System.out.println(node.element);
preorderTraversal(node.left);
preorderTraversal(node.right);
}
// 中序遍历(递归方法)
public void inorderTraversal() {
preorderTraversal(root);
}
private void inorderTraversal(Node<E> node) {
if (node == null)
return;
preorderTraversal(node.left);
System.out.println(node.element);
preorderTraversal(node.right);
}
// 后序遍历(递归方法)
public void postorderTraversal() {
postorderTraversal(root);
}
private void postorderTraversal(Node<E> node) {
if (node == null)
return;
preorderTraversal(node.left);
preorderTraversal(node.right);
System.out.println(node.element);
}
// 层序遍历(通过链表实现)
private void levelorderTraversal() {
if (root == null)
return;
Queue<Node<E>> queue = new LinkedList<>();
// 入队
queue.offer(root);
while (!queue.isEmpty()) {
// 出队
Node<E> node = queue.poll();
System.out.println(node.element);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
/// 判断一棵树是否为完全二叉树
public boolean isComplete() {
if (root == null)
return false;
Queue<Node<E>> queue = new LinkedList<>();
queue.offer(root);
boolean leaf = false;
while (!queue.isEmpty()) {
Node<E> node = queue.poll();
if (leaf && !node.isLeaf())
return false;
if (node.left != null) {
queue.offer(node.left);
} else if (node.right != null) {
return false;
}
if (node.right != null) {
queue.offer(node.right);
} else {
// 后面遍历的节点都必须是叶子节点
leaf = true;
}
}
return true;
}
// 前驱节点
public Node<E> predecessor(Node<E> node) {
if (node == null)
return null;
// 前驱节点在左子树当中(left.right.right.........)
Node<E> p = node.left;
if (p != null) {
while (p.right != null) {
p = p.right;
}
return p;
}
// 从父节点、祖父节点当中去找前驱节点
while (node.parent != null && node == node.parent.right) {
node = node.parent;
}
// node.parent == null && node.left == null
return node.parent;
}
// 后继节点
public Node<E> successor(Node<E> node) {
if (node == null)
return null;
// 前驱节点在左子树当中(right.left.left.........)
Node<E> p = node.right;
if (p != null) {
while (p.left != null) {
p = p.left;
}
return p;
}
// 从父节点、祖父节点当中去找前驱节点
while (node.parent != null && node == node.parent.left) {
node = node.parent;
}
// node.parent == null && node.right == null
return node.parent;
}
private static class Node<E> {
E element;
// 左子节点
Node<E> left;
// 右子节点
Node<E> right;
// 父节点
Node<E> parent;
// 构造函数
public Node(E element, Node<E> parent) {
this.element = element;
this.parent = parent;
}
// 是否是叶子节点
public boolean isLeaf() {
return left == null && right == null;
}
//度为2节点
public boolean hasTwoChildrenNode() {
return left != null && right != null;
}
}
/*
* 使用自定义打印器 需要实现以下几个方法
*
*/
@Override
public Object root() {
// TODO Auto-generated method stub
return root;
}
@SuppressWarnings("unchecked")
@Override
public Object left(Object node) {
// TODO Auto-generated method stub
return ((Node<E>) node).left;
}
@SuppressWarnings("unchecked")
@Override
public Object right(Object node) {
// TODO Auto-generated method stub
return ((Node<E>) node).right;
}
@SuppressWarnings("unchecked")
@Override
public Object string(Object node) {
// TODO Auto-generated method stub
// 打印出parent
Node<E> myNode = (Node<E>) node;
String parentString = "null";
if (myNode.parent != null) {
parentString = myNode.parent.element.toString();
}
return myNode.element + "_(" + parentString + ")";
}
}