数据结构第四章树 平衡二叉树,实现插入,删除操作,Java实现,递归实现

二叉搜索树自动调整平衡(左右子树高度差小于等于1)——平衡二叉树

目录

1.编写测试程序,测试二叉树的四种平衡操作

2.编写内部类:树结点

3.根节点,类构造器

4.插入

5.平衡

5.1计算平衡值

5.2.计算深度 

5.3.平衡节点(调用四个旋转的条件)

5.3.1. RR旋转

5.3.2. LL旋转

5.3.3. RL旋转

5.3.4. LR旋转

5.3.5. 逆时针旋转

5.3.6 顺时针旋转

6.删除 

6.1删除无儿子节点

6.2 删除有一个儿子的节点 

6.3 删除有两个儿子的节点 

 7.前序遍历,用于验证二叉树

8.全部代码如下



1.编写测试程序,测试二叉树的四种平衡操作

public class AVLTree {
	public static void main(String[] args) {
	//验证插入操作
		BalancedBinaryTree test0 = new BalancedBinaryTree(100);	
		test0.insert(50);
		test0.insert(200);
		System.out.print("草稿纸上运算为{100,50,200}: ");
		test0.preOrder();
		System.out.println();
		
	//验证LL旋转
		//发生在root
		BalancedBinaryTree test1 = new BalancedBinaryTree(100);	
		test1.insert(70);
		test1.insert(30);
		test1.insert(10);
		test1.insert(5);
		System.out.print("草稿纸上运算为{70,10,5,30,100}: ");
		test1.preOrder();
		System.out.println();
		//发生在非root
		BalancedBinaryTree test4 = new BalancedBinaryTree(100);	
		test4.insert(50);
		test4.insert(200);
		test4.insert(150);
		test4.insert(130);
		System.out.print("草稿纸上运算为{100,50,150,130,200 }: ");
		test4.preOrder();
		System.out.println();	
		
	//验证RR旋转
		//发生在root
		BalancedBinaryTree test2 = new BalancedBinaryTree(100);	
		test2.insert(200);
		test2.insert(300);
		test2.insert(400);
		test2.insert(500);
		System.out.print("草稿纸上运算为{200,100,400,300,500}: ");
		test2.preOrder();
		System.out.println();
		//发生在非root
		BalancedBinaryTree test3 = new BalancedBinaryTree(100);	
		test3.insert(50);
		test3.insert(200);
		test3.insert(75);
		test3.insert(80);
		System.out.print("草稿纸上运算为{100,75,50,80,200}: ");
		test3.preOrder();
		System.out.println();
		
	//验证LR旋转
		BalancedBinaryTree test5 = new BalancedBinaryTree(100);	
		test5.insert(50);
		test5.insert(200);
		test5.insert(75);
		test5.insert(30);
		test5.insert(80);
		System.out.print("LR草稿纸上运算为{75,50,30,100,80,200}: ");
		test5.preOrder();
		System.out.println();
		
	//验证RL旋转
		BalancedBinaryTree test6 = new BalancedBinaryTree(100);	
		test6.insert(50);
		test6.insert(200);
		test6.insert(150);
		test6.insert(300);
		test6.insert(125);
		System.out.print("RL草稿纸上运算为{150,100,50,125,200,300}: ");
		test6.preOrder();
		System.out.println();	
	
	
	//验证删除操作
		BalancedBinaryTree test7 = new BalancedBinaryTree(100);	
		test7.insert(50);
		test7.insert(200);
		test7.insert(150);
		test7.insert(300);
		test7.insert(25);
		test7.insert(75);
		System.out.print("delete草稿纸上运算为{100,50,25,75,200,150,300}: ");
		test7.preOrder();
		System.out.println();
		
		//删除没有儿子的节点
		test7.delete(150);
		System.out.print("删除没有儿子的节点150,delete草稿纸上运算为{100 50 25 75 200 300 }: ");
		test7.preOrder();
		System.out.println();
		//删除有一个儿子的节点
		test7.delete(200);
		System.out.print("删除有一个儿子的节点200,delete草稿纸上运算为{100 50 25 75 200 300 }: ");
		test7.preOrder();
		System.out.println();
		//删除有两个儿子的节点
		test7.delete(100);
		System.out.print("删除有两个儿子的节点100,delete草稿纸上运算为{75,50,25,300}: ");
		test7.preOrder();
		System.out.println();
		
		test7.delete(75);
		System.out.print("删除节点75,delete草稿纸上运算为{50,25,300}: ");
		test7.preOrder();
		System.out.println();
		
		test7.delete(50);
		System.out.print("删除节点50,delete草稿纸上运算为{25,300}: ");
		test7.preOrder();
		System.out.println();
	}
}

2.编写内部类:树结点

        class AVLNode {
		public int data;
		public int depth;//当前子树深度的 depth
		public int balance;
		public AVLNode parent = null;//指向父节点的指针
		public AVLNode left = null;
		public AVLNode right = null;
		
		public AVLNode() {
			depth = 1;
			balance = 0;
			left = null;
			right = null;
		}
		
		public AVLNode(int data) {
			this.data = data;
			depth = 1;
			balance = 0;
			left = null;
			right = null;			
		}
	}

3.根节点,类构造器

class BalancedBinaryTree {
	/**
	 *  根节点
	 */
	private AVLNode root = null;
    /**
	 * 构造器
	 */
	public BalancedBinaryTree()	{		
	}
	
	public BalancedBinaryTree(int data)	{
		root = new AVLNode(data);
	}
    //以及别的操作
}

4.插入

插入过程分为:二叉查找树的插入过程+再平衡过程

再平衡过程分为:计算深度和平衡值+进行平衡操作

        public void insert(int data) {
		if (root == null) {
			root = new AVLNode(data);
		} else {
			insertSon(root, data);
		}
	}
	
	private void insertSon(AVLNode node, int data) {
		if (data < node.data) {
			if (node.left != null) {
				insertSon(node.left, data);
			} else {
				node.left = new AVLNode(data);
				node.left.parent = node;//子节点锁住parent
			}
		} else {
			if (node.right != null) {
				insertSon(node.right, data);
			} else {
				node.right = new AVLNode(data);
				node.right.parent = node;
			}
		}
		// 计算平衡和深度
		node.balance = calculateBalance(node);
		node.depth = calculateDepth(node);
		// 平衡节点
		balanceNode(node);
	}

5.平衡

5.1计算平衡值

        private int calculateBalance(AVLNode node) {
		int leftDepth;
		int rightDepth;
		if (node.left != null) {
			leftDepth = node.left.depth;
		} else {
			leftDepth = 0;
		}
		if (node.right != null) {
			rightDepth = node.right.depth;
		} else {
			rightDepth = 0;
		}
		return leftDepth - rightDepth;
	}

5.2.计算深度 

	private int calculateDepth(AVLNode node) {
		int depth = 0;
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值