1.1 树
定义:树是由n(n>=1)个有限结点组成一个具有层次关系的集合。
如图所示:
特点:
1.每个结点具有一个或多个结点
2.没有父结点的称为根结点,如图中的A
3.每个非根结点只有一个父结点
4.每个结点及其下面的结点可以看做一个树,称为当前结点父结点的一个子树
1.2 二叉树
定义:二叉树是度不超过2的树,即每个结点最多有两个子结点。
满二叉树:每个结点都有两个子结点。如图所示
完全二叉树:叶结点(即没有子结点的结点)只能出现在最下层和次下层,并且最下层的结点都集中在最左边若干位置的二叉树。如图所示:
1.3 二叉查找树的创建
** 二叉树的结点类**
private class Node{
public key key;
public value value;
public Node left;
public Node right;
public Node(key key, value value, Node left, Node right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
}
向树中插入元素
//向树中添加key-value
public void put( key key, value value){
root =put(root,key,value);
}
//向指定树中添加元素,并返回添加元素后新的树
public Node put(Node x,key key,value value){
//如果x子树为空
if (x==null){
N++;
return new Node(key,value,null,null);
}
//如果x子树不为空
//比较x的键和key的大小
int cmp = key.compareTo(x.key);
//如果key大于x的键,则继续找x的右子树
if (cmp>0){
x.right= put(x.right,key,value);
}
//如果key小于x的键,则继续找x的左子树
else if (cmp<0){
x.left=put(x.left,key,value);
}
//如果等于,则替换
else{
x.value=value;
}
return x;
}
获取指定key对应的value
//查询key对应的value
public value get(key key){
return get(root,key);
}
//查找x树中,key对应的值
public value get(Node x,key key){
//x树为null
if (x==null){
return null;
}
//x不为null
//比较key和x结点键的大小
int cmp = key.compareTo(x.key);
//如果key大于x的键,则继续找x的右子树
if (cmp>0){
return get(x.right,key);
}
//如果key小于x的键,则继续找x的左子树
else if (cmp<0){
return get(x.left,key);
}
//如果等于,则返回值
else{
return x.value;
}
}
删除指定位置的元素
//删除树中key对应的value
public void delete(key key){
root=delete(root,key);
}
//删除x树key对应的value,并返回新的树
public Node delete(Node x,key key){
//x树为null
if (x==null){
return null;
}
//x树不为null
//比较key和x结点键的大小
int cmp = key.compareTo(x.key);
//如果key大于x的键,则继续找x的右子树
if (cmp>0){
x.right=delete(x.right,key);
}
//如果key小于x的键,则继续找x的左子树
else if (cmp<0){
x.left=delete(x.left,key);
}
//如果等于,则完成删除结点动作
else{
//元素个数-1
N--;
if (x.right==null){
return x.left;
}
if (x.left==null){
return x.right;
}
//找到右子树中最小结点
Node min=x.right;
while (min.left!=null){
min=min.left;
}
//删除右子树中最小结点
Node n=x.right;
while(n.left!=null){
if (n.left.left==null){
n.left=null;
}else
//变换n
n=n.left;
}
//让x结点的左子树称为min的左子树
min.left=x.left;
//让x结点的右子树称为min的右子树
min.right=x.right;
//让x的父节点指向min
x=min;
}
return x;
}
查找最小键
//找出整个树中最小的键
public key min(){
return min(root).key;
}
//查找x树最小值所在的结点
public Node min(Node x){
if (x.left!=null){
return min(x.left);
}
else
return x;
}
查找最大键
public key max(){
return max(root).key;
}
//查找x树最大值所在的结点
public Node max(Node x){
if (x.right!=null){
return max(x.right);
}
else
return x;
}
1.4 二叉树的基础遍历
我们可以把树分为三个部分,即左子树,结点,右子树。
①前序遍历
先访问根结点,再访问左子树,最后访问右子树
②中序遍历
先访问左子树,再访问结点,最后访问右子树。(常用,遍历具有顺序)
③后序遍历
先访问左子树,再访问右子树,最后访问结点。
前序遍历代码
public Queue<key> preErgodic(){
Queue<key> keys=new Queue<>();
preErgodic(root,keys);
return keys;
}
//获取指定树x的所有键,并放到keys队列中
private void preErgodic(Node x,Queue<key> keys){
if (x==null){
return;
}
//把x结点key放在keys中
keys.enqueue(x.key);
//递归遍历左子树
if (x.left!=null){
preErgodic(x.left,keys);
}
//递归遍历右子树
if (x.right!=null){
preErgodic(x.right,keys);
}
}
中序遍历代码
public Queue<key> midErgodic(){
Queue<key> keys = new Queue<>();
midErgodic(root,keys);
return keys;
}
//获取指定树x中所有的键,并存放在keys中
private void midErgodic(Node x,Queue<key> keys){
if (x==null){
return;
}
//先递归把左子树中的键放在keys中
if (x.left!=null){
midErgodic(x.left,keys);
}
//把x的键放在keys中
keys.enqueue(x.key);
//先递归把右子树中的键放在keys中
if (x.right!=null){
midErgodic(x.right,keys);
}
}
后序遍历代码
private void afterErgodic(Node x,Queue<key> keys){
if (x==null){
return;
}
//先递归把左子树中的键放在keys中
if (x.left!=null){
afterErgodic(x.left,keys);
}
//先递归把右子树中的键放在keys中
if (x.right!=null){
afterErgodic(x.right,keys);
}
//把x的键放在keys中
keys.enqueue(x.key);
}
代码中出现的Queue是前几篇文章中提到的队列,使用时只需导入相应的包即可
1.5 层序遍历
含义:就是从根结点开始,获取每一层所有结点。
如图所示的二叉树,层序遍历的结果为:EBGADFHC
代码实现
public Queue<key> layerErgodic(){
//定义两个队列,分别存储树中的键和结点
Queue<key> keys = new Queue<>();
Queue<Node> nodes = new Queue<>();
//默认往队列中放入根结点
nodes.enqueue(root);
while (!nodes.isEmpty()){
//从队列中弹出结点,把key放在keys中
Node n = nodes.dequeue();
keys.enqueue(n.key);
//判断当前结点有没有左子节点,如果有,放在队列中
if (n.left!=null){
nodes.enqueue(n.left);
}
//判断当前结点有没有右子节点,如果有,放在队列中
if (n.right!=null){
nodes.enqueue(n.right);
}
}
return keys;
}
1.6 二叉树的最大深度问题
最大深度:即树的根结点到最远叶子结点路径上的节点数。简单点说,就是层数。
代码实现
public int maxdepth(){
return maxdepth(root);
}
//求取指定树x的最大深度
private int maxdepth(Node x){
if (x==null){
return 0;
}
int max=0;
//左子树的最大深度
int maxl=0;
//右子树的最大深度
int maxr=0;
//计算左子树的最大深度
if (x.left!=null){
maxl=maxdepth(x.left);
}
//计算右子树的最大深度
if (x.right!=null){
maxr=maxdepth(x.right);
}
//比较,取最大值,加一
max=maxl>maxr?maxl+1:maxr+1;
return max;
}
完整代码
public class BinaryTree<key extends Comparable<key>,value> {
//记录根结点
private Node root;
private int N;
private class Node{
public key key;
public value value;
public Node left;
public Node right;
public Node(key key, value value, Node left, Node right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
}
//获取树中元素的个数
public int size(){
return N;
}
//向树中添加key-value
public void put( key key, value value){
root =put(root,key,value);
}
//向指定树中添加元素,并返回添加元素后新的树
public Node put(Node x,key key,value value){
//如果x子树为空
if (x==null){
N++;
return new Node(key,value,null,null);
}
//如果x子树不为空
//比较x的键和key的大小
int cmp = key.compareTo(x.key);
//如果key大于x的键,则继续找x的右子树
if (cmp>0){
x.right= put(x.right,key,value);
}
//如果key小于x的键,则继续找x的左子树
else if (cmp<0){
x.left=put(x.left,key,value);
}
//如果等于,则替换
else{
x.value=value;
}
return x;
}
//查询key对应的value
public value get(key key){
return get(root,key);
}
//查找x树中,key对应的值
public value get(Node x,key key){
//x树为null
if (x==null){
return null;
}
//x不为null
//比较key和x结点键的大小
int cmp = key.compareTo(x.key);
//如果key大于x的键,则继续找x的右子树
if (cmp>0){
return get(x.right,key);
}
//如果key小于x的键,则继续找x的左子树
else if (cmp<0){
return get(x.left,key);
}
//如果等于,则返回值
else{
return x.value;
}
}
//删除树中key对应的value
public void delete(key key){
root=delete(root,key);
}
//删除x树key对应的value,并返回新的树
public Node delete(Node x,key key){
//x树为null
if (x==null){
return null;
}
//x树不为null
//比较key和x结点键的大小
int cmp = key.compareTo(x.key);
//如果key大于x的键,则继续找x的右子树
if (cmp>0){
x.right=delete(x.right,key);
}
//如果key小于x的键,则继续找x的左子树
else if (cmp<0){
x.left=delete(x.left,key);
}
//如果等于,则完成删除结点动作
else{
//元素个数-1
N--;
if (x.right==null){
return x.left;
}
if (x.left==null){
return x.right;
}
//找到右子树中最小结点
Node min=x.right;
while (min.left!=null){
min=min.left;
}
//删除右子树中最小结点
Node n=x.right;
while(n.left!=null){
if (n.left.left==null){
n.left=null;
}else
//变换n
n=n.left;
}
//让x结点的左子树称为min的左子树
min.left=x.left;
//让x结点的右子树称为min的右子树
min.right=x.right;
//让x的父节点指向min
x=min;
}
return x;
}
//找出整个树中最小的键
public key min(){
return min(root).key;
}
//查找x树最小值所在的结点
public Node min(Node x){
if (x.left!=null){
return min(x.left);
}
else
return x;
}
//找出整个树中最大的键
public key max(){
return max(root).key;
}
//查找x树最大值所在的结点
public Node max(Node x){
if (x.right!=null){
return max(x.right);
}
else
return x;
}
//获取整个数组中所有的键,前序遍历
public Queue<key> preErgodic(){
Queue<key> keys=new Queue<>();
preErgodic(root,keys);
return keys;
}
//获取指定树x的所有键,并放到keys队列中
private void preErgodic(Node x,Queue<key> keys){
if (x==null){
return;
}
//把x结点key放在keys中
keys.enqueue(x.key);
//递归遍历左子树
if (x.left!=null){
preErgodic(x.left,keys);
}
//递归遍历右子树
if (x.right!=null){
preErgodic(x.right,keys);
}
}
//使用中序遍历,获取树中的键
public Queue<key> midErgodic(){
Queue<key> keys = new Queue<>();
midErgodic(root,keys);
return keys;
}
//获取指定树x中所有的键,并存放在keys中
private void midErgodic(Node x,Queue<key> keys){
if (x==null){
return;
}
//先递归把左子树中的键放在keys中
if (x.left!=null){
midErgodic(x.left,keys);
}
//把x的键放在keys中
keys.enqueue(x.key);
//先递归把右子树中的键放在keys中
if (x.right!=null){
midErgodic(x.right,keys);
}
}
//使用后序遍历,获取树中的键
public Queue<key> afterErgodic(){
Queue<key> keys = new Queue<>();
afterErgodic(root,keys);
return keys;
}
//获取指定树x中所有的键,并存放在keys中
private void afterErgodic(Node x,Queue<key> keys){
if (x==null){
return;
}
//先递归把左子树中的键放在keys中
if (x.left!=null){
afterErgodic(x.left,keys);
}
//先递归把右子树中的键放在keys中
if (x.right!=null){
afterErgodic(x.right,keys);
}
//把x的键放在keys中
keys.enqueue(x.key);
}
//使用层序遍历,获取树中所有的键
public Queue<key> layerErgodic(){
//定义两个队列,分别存储树中的键和结点
Queue<key> keys = new Queue<>();
Queue<Node> nodes = new Queue<>();
//默认往队列中放入根结点
nodes.enqueue(root);
while (!nodes.isEmpty()){
//从队列中弹出结点,把key放在keys中
Node n = nodes.dequeue();
keys.enqueue(n.key);
//判断当前结点有没有左子节点,如果有,放在队列中
if (n.left!=null){
nodes.enqueue(n.left);
}
//判断当前结点有没有右子节点,如果有,放在队列中
if (n.right!=null){
nodes.enqueue(n.right);
}
}
return keys;
}
//求解树的最大深度
public int maxdepth(){
return maxdepth(root);
}
//求取指定树x的最大深度
private int maxdepth(Node x){
if (x==null){
return 0;
}
int max=0;
//左子树的最大深度
int maxl=0;
//右子树的最大深度
int maxr=0;
//计算左子树的最大深度
if (x.left!=null){
maxl=maxdepth(x.left);
}
//计算右子树的最大深度
if (x.right!=null){
maxr=maxdepth(x.right);
}
//比较,取最大值,加一
max=maxl>maxr?maxl+1:maxr+1;
return max;
}
}
b站详细讲解网址:http://yun.itheima.com/course/639.html