1)普通树:结点,结点关系 ,高
2) 若每个结点最多两个孩子,孩子分左右 :二叉树
3)若结点与左右孩子所存储的值呈现左<根<右 :搜索树
4)若根节点左右子树高度差相同 (<=1) :平衡树/AVL
5)红黑树
树的遍历
搜索树---》平衡树
平衡树太难了,插个眼,等我周末再回来遍历一下。
/**
* 平衡树:二叉,值大小,平衡
* 1,二叉树
* 2,链表
*/
public class MyAVL {
Node root=null; //树根
int size=0;
int count = 0;
//添加元素
public void push(int a){
Node node = new Node();
node.value = a;
node.left = null;
node.right = null;
if(root==null){
//若为空树
root = node;
}else{
//若为非空树
Node child_node = root;
Node parent_node = null;
while(child_node!=null){
parent_node = child_node;
if(child_node.value>a){
child_node = child_node.left;
}else{
child_node = child_node.right;
}
}
//插入值
if(parent_node.value>a){
parent_node.left = node;
}else{
parent_node.right = node;
}
}
size++;
//旋转?????
//求高度(左右子树的高度)
int l = hight(root.left);//!=0?hight(root.left,0)+1:0;
int r = hight(root.right);//!=0?hight(root.right,0)+1:0;
//判断高度差,判断用单旋转还是双旋转(旋点左右子树)
if(l-r>1){
//左长,右旋
Node node1 = root.left;
int ll = hight(node1.left);//!=0?hight(node1.left,0)+1:0;
int lr = hight(node1.right);//!=0?hight(node1.right,0)+1:0;
if(ll>lr){
//整体单旋
Node node2 = right_x(root);
root = node2;
}else{
//整体双旋
//个体左单旋
Node node2 = left_x(root.left);
root.left = node2;
Node node3 = right_x(root);
root = node3;
}
}else if(l-r<-1){
//右长,左旋
}
}
//左单旋
public Node left_x(Node node){
Node root_temp = node.right;
root_temp.left = node;
if(root_temp.left!=null){
node.right = root_temp.left;
root_temp.left = null;
}else{
node.right = null;
}
return root_temp;
}
//右单旋
public Node right_x(Node node){
Node root_temp = node.left;
root_temp.right = node;
if(root_temp.right!=null){
node.left = root_temp.right;
root_temp.right = null;
}else{
node.left = null;
}
return root_temp;
}
//求高度
/*public int hight(Node node,int count0){
if(count0==0){
count = 0;
}
if(node!=null){
if(node.right!=null&&node.left!=null){
count++;
hight(node.right,count0+1);
hight(node.left,count0+1);
} else if(node!=null&&(node.right!=null||node.left!=null)){
count++;
node = node.right==null?node.left:node.right;
hight(node,count0+1);
}
}else{
count++;
}
return count;
}*/
public static int hight(Node node){
if (node == null || node == null) return 0;
int left = hight(node.left);
int right = hight(node.right);
if (left > right){
return left + 1;
}else {
return right + 1;
}
}
//删除元素:假设要删除的元素 一定存在
public void delete(int a){
Node node = root;
Node parent = null;
while(node!=null&&node.value!=a){
parent = node;
if(node.value>a){
node = node.left;
}else{
node = node.right;
}
}
//删除
if(node.right==null&&node.left==null){
//1,若为叶子结点
if(parent.value>node.value){
parent.left = null;
}else{
parent.right = null;
}
}else if(node.right!=null&&node.left==null){
//2,若有一个子节点
if(parent.value>node.value){
parent.left = node.right;
}else{
parent.right = node.right;
}
}else if(node.right==null&&node.left!=null){
//2,若有一个子节点
if(parent.value>node.value){
parent.left = node.left;
}else{
parent.right = node.left;
}
}else{
//3,若有两个子节点
Node min = node.right;
Node p_min = node;
while(min.left!=null){
p_min = min;
min = min.left;
}
/*if(parent.value>node.value){
parent.left = min;
}else{
parent.right = min;
}
min.left = node.left;
min.right = node.right;*/
node.value = min.value;
if(min.left==null&&min.right==null){
if(p_min.value>min.value){
p_min.left = null;
}else{
p_min.right = null;
}
}else{
if(p_min.value>min.value){
p_min.left = min.right;
}else{
p_min.right = min.right;
}
}
}
size--;
//旋转????????
}
//修改元素
public void update(int v,int a){
delete(v);
push(a);
}
//先序遍历
public void pforeach(Node node){
Node n = node;
System.out.print(n.value+" ");
if(n.left!=null&&n.right==null){
n = n.left;
pforeach(n);
}else if(n.left==null&&n.right!=null){
n = n.right;
pforeach(n);
}else if(n.left!=null&&n.right!=null){
pforeach(n.left);
pforeach(n.right);
}
}
//中序遍历
//后序遍历
//获取大小
public int size(){
return size;
}
}
明天去次鳗鱼饭,么么!