基于AVL自平衡树的TreeMap实现

自平衡的插入删除,无论按什么顺序插入,得到的都是一棵平衡树。删除任意结点之后也保持平衡性。

经典的LL,RR, LR,RL那种旋转方法。注意旋转操作的特点,例如右旋,新root原来的右子树从原来属于左子树,变成属于右子树,要保证这棵移动到另一边的树是其父节点两棵子树中较小的那棵。


public class AVLTreeMap<Key extends Comparable<Key>, Value> {
	private class TreeNode {
		Key key;
		Value value;
		TreeNode left, right;
		int ht, sz;
		TreeNode(Key k, Value v) {
			this.key = k;
			this.value = v;
			this.ht = 1;
			this.sz = 1;
		}
	}
	private TreeNode root;
	private int height(TreeNode root) {
		if (root == null) return 0;
		return root.ht;
	}
	
	private int size(TreeNode root) {
		if (root == null) return 0;
		return root.sz;
	}
	private void update(TreeNode root) {
		root.sz = size(root.left) + size(root.right) + 1;
		root.ht = Math.max(height(root.left), height(root.right)) + 1;
	}
	
	private TreeNode rotateRight(TreeNode root) {
		TreeNode l = root.left;
		root.left = l.right;
		update(root);
		l.right = root;
		update(l);
		return l;
	}
	
	private TreeNode rotateLeft(TreeNode root) {
		TreeNode r = root.right;
		root.right = r.left;
		update(root);
		r.left = root;
		update(r);
		return r;
	}
	private TreeNode balance(TreeNode root) {
		if (height(root.left) - height(root.right) > 1) {
			if (height(root.left.left) > height(root.left.right)) { //LL
				root = rotateRight(root);
			}
			else { //LR
				root.left = rotateLeft(root.left);
				root = rotateRight(root);
			}
		}
		else if (height(root.right) - height(root.left) > 1) {
			if (height(root.right.right) > height(root.right.left)) { //RR
				root = rotateLeft(root);
			}
			else { //RL
				root.right = rotateRight(root.right);
				root = rotateLeft(root);
			}
		}
		return root;
	}
	private TreeNode put(TreeNode root, Key key, Value value) {
		if (root == null)  return new TreeNode(key, value);
		if (key.compareTo(root.key) < 0) root.left = put(root.left, key, value);
		else if (key.compareTo(root.key) > 0) root.right = put(root.right, key, value);
		else root.value = value;
		update(root);
		return balance(root);
	}
	public void put(Key key, Value value) {
		root = put(root, key, value);
	}
	
	private TreeNode min(TreeNode root) {
		if (root.left == null) return root;
		return min(root.left);
	}
	private TreeNode remove(TreeNode root, Key key) {
		if (root == null) return null;
		if (key.compareTo(root.key) < 0) {
			root.left = remove(root.left, key);
		}
		else if (key.compareTo(root.key) > 0) {
			root.right = remove(root.right, key);
		}
		else {
			if (root.left == null) return root.right;
			else if (root.right == null) return  root.left;
			else {
				TreeNode successor = min(root.right);
				Key tempKey = root.key;
				root.key = successor.key;
				root.value = successor.value;
				successor.key = tempKey;
				root.right = remove(root.right, tempKey);			
			}
		}
		update(root);
		return balance(root);
	}
	public void remove(Key key) {
		remove(root, key);
	}
	private int rank(TreeNode root, Key key) {
		if (root == null) return 0;
		if (key.compareTo(root.key) == 0) return size(root.left);
		if (key.compareTo(root.key) < 0) return rank(root.left, key);
		return size(root.left) + 1 + rank(root.right, key);
	}
	public int rank(Key key) {
		return rank(root, key);
	}
	private TreeNode select(TreeNode root, int rank) {
		if (root == null) return null;
		if (rank == size(root.left)) return root;
		if (rank < size(root.left)) return select(root.left, rank);
		return select(root.right, rank - size(root.left) - 1);
	}
	public Key select(int rank) {
		TreeNode x = select(root, rank);
		if (x == null) return null;
		return x.key;
	}
	private TreeNode floor(TreeNode root, Key key) {
		if (root == null) return null;
		int cmp = key.compareTo(root.key);
		if (cmp == 0) return root;
		if (cmp < 0) return floor(root.left, key);
		TreeNode f = floor(root.right, key);
		if (f == null) return root;
		return f;
	}
	public Key floor(Key key) {
		TreeNode f = floor(root, key);
		if (f == null) return null;
		return f.key;
	}
	private TreeNode ceiling(TreeNode root, Key key) {
		if (root == null) return null;
		int cmp = key.compareTo(root.key);
		if (cmp == 0) return root;
		if (cmp > 0)  return ceiling(root.right, key);
		TreeNode ceil = ceiling(root.left, key);
		if (ceil == null) return root;
		return ceil;
	}
	public Key ceiling(Key key) {
		TreeNode x = ceiling(root, key);
		if (x == null) return null;
		return x.key;
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVL平衡树是一种自平衡二叉搜索树,其基本思想是通过旋转来保证树的左右子树的高度差不超过1,从而实现平衡。 下面是一个非指针实现的简单的AVL平衡树的代码示例: ``` #define MAX_SIZE 1000 // 树的最大容量 // AVL树节点的定义 typedef struct AVLNode { int data; // 节点数据 int height; // 节点高度 int left; // 左子树在数组中的下标 int right; // 右子树在数组中的下标 } AVLNode; // AVL树的定义 typedef struct AVLTree { AVLNode nodes[MAX_SIZE]; // 存储树节点的数组 int root; // 根节点在数组中的下标 int size; // 树的大小 } AVLTree; // 求节点的高度 int height(AVLTree *tree, int index) { if (index == -1) { // 空节点高度为0 return 0; } else { return tree->nodes[index].height; } } // 求节点的平衡因子 int balance_factor(AVLTree *tree, int index) { return height(tree, tree->nodes[index].left) - height(tree, tree->nodes[index].right); } // 更新节点的高度 void update_height(AVLTree *tree, int index) { tree->nodes[index].height = 1 + max(height(tree, tree->nodes[index].left), height(tree, tree->nodes[index].right)); } // 左旋操作 int left_rotate(AVLTree *tree, int index) { int right = tree->nodes[index].right; tree->nodes[index].right = tree->nodes[right].left; tree->nodes[right].left = index; update_height(tree, index); update_height(tree, right); return right; } // 右旋操作 int right_rotate(AVLTree *tree, int index) { int left = tree->nodes[index].left; tree->nodes[index].left = tree->nodes[left].right; tree->nodes[left].right = index; update_height(tree, index); update_height(tree, left); return left; } // 插入节点 int insert_node(AVLTree *tree, int data, int index) { if (index == -1) { // 空节点,插入新节点 index = tree->size++; tree->nodes[index].data = data; tree->nodes[index].height = 1; tree->nodes[index].left = -1; tree->nodes[index].right = -1; } else if (data < tree->nodes[index].data) { // 插入左子树 tree->nodes[index].left = insert_node(tree, data, tree->nodes[index].left); if (balance_factor(tree, index) == 2) { // 需要旋转 if (balance_factor(tree, tree->nodes[index].left) == 1) { // 左-左情况,进行右旋 index = right_rotate(tree, index); } else { // 左-右情况,进行左旋再右旋 tree->nodes[index].left = left_rotate(tree, tree->nodes[index].left); index = right_rotate(tree, index); } } } else { // 插入右子树 tree->nodes[index].right = insert_node(tree, data, tree->nodes[index].right); if (balance_factor(tree, index) == -2) { // 需要旋转 if (balance_factor(tree, tree->nodes[index].right) == -1) { // 右-右情况,进行左旋 index = left_rotate(tree, index); } else { // 右-左情况,进行右旋再左旋 tree->nodes[index].right = right_rotate(tree, tree->nodes[index].right); index = left_rotate(tree, index); } } } update_height(tree, index); return index; } // 中序遍历 void inorder_traversal(AVLTree *tree, int index) { if (index != -1) { inorder_traversal(tree, tree->nodes[index].left); printf("%d ", tree->nodes[index].data); inorder_traversal(tree, tree->nodes[index].right); } } ``` 这个示例代码是一个使用数组实现的简单AVL平衡树,其中包括了节点的定义,树的定义和一些基本操作,如插入节点、左旋、右旋等等。可以通过调用`insert_node`函数向树中插入节点,通过调用`inorder_traversal`函数进行中序遍历输出树中的所有节点。需要注意的是,这个代码示例没有包含删除节点的操作,如果需要实现删除功能,还需要进行一些修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值