java二叉树代码实现,java 代码实现平衡二叉树

java 代码实现平衡二叉树

/**

* 平衡二叉搜索(排序)树

*

* 平衡二叉搜索树双称为AVL树,它也是一棵二叉搜索树,是对二叉搜索树的一种改进,或都是具有下列性质的二叉树:它

* 的左子树和右子树都是平衡二叉树,且左子树和右子树的深度之差的绝对值不超过1。

*

* 平衡因子(Balance Factor,BF)定义为该节点的左子树的深度减去其右子树的深度,则平衡二叉树上所有节点的平

* 衡因子只可能是-1、0和1。只要树上有一个节点的平衡因子的绝对值大于1,则该二叉树就是不平衡的了。

*

* 使用二叉排序树保持平衡的基本思想是:每当在二叉排序树中插入一个节点时,首先检查是否因插入而破坏了平衡,若

* 是,则找出其中的最小不平衡二叉树,在保持二叉排序树特性的情况下,调整最小不平衡子s树中节点之间的关系,以达

* 到新的平衡。所谓最小不平衡子树指离插入节点最近且以平衡因子的绝对值大于1的节点作为根的子树。

*

* 对于平衡二叉搜索树,保持树的平衡的基本机制就是旋转。旋转是对树的元素顺序进行调节。旋转的目的是消除由于临

* 时插入和删除对树的平衡产生的影响。

*

* 有四种旋转:

* 1)绕某元素左旋转

*          80 ← p                    90

*          /\                        /\

*         60 90 ← r        →        80 120

*            /\                     /\  /

*          85 120                 60 85 100

*&n

相关文档:

Java中通过implements Serializable来实现对象的序列化。其实Serializable接口中并没有需要实现的方法,注明某个类implements Serializable只是为了标识或表明这个类可以被序列化。

那么什么是序列化呢,序列化又有什么作用呢?

一个类,或 ......

Swing中提供了JOptionPane类来实现类似Windows平台下的MessageBox的功能,同样在Java中也有,利用JOptionPane类中的各个static方法来生成各种标准的对话框,实现显示出信息、提出问题、警告、用户输入参数等功能。这些对话框都是模式对话框。

ConfirmDialog --- 确认对话框,提出问题,然后由用户自己来确认(按"Ye ......

LinkedList内部以链表形式存储数据

ArrayList内部以数组形式存储数据。

Vector同ArrayList,不过它与ArrayList比较起来是thread-safe的。

Hashtable是继承了Dictionary,是线程安全的。HashMap实现了Map接口,不是线程安全的。

如何保证线程安全的?每个修改容� ......

package Utils.Sort;

/**

*归并排序,要求待排序的数组必须实现Comparable接口

*/

public class MergeSort implements SortStrategy

{

private Comparable[] bridge;

/**

*利用归并排序� ......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的平衡二叉树实现通常是基于红黑树算法,这是一种自平衡二叉查找树。在红黑树中,每个节点都被标记为红色或黑色,并满足以下特性: 1. 根节点是黑色的。 2. 所有叶子节点都是黑色的(叶子节点为NIL节点)。 3. 如果一个节点是红色的,则它的两个子节点都是黑色的。 4. 从任意节点到其每个叶子节点的所有路径都包含相同数目的黑色节点。 这些特性确保了红黑树是平衡的,因为任意路径上的黑色节点数目相同。插入和删除操作可以通过对颜色和结构进行适当的旋转来维护树的平衡性。 下面是一个简单的Java平衡二叉树实现示例: ``` import java.util.*; public class RedBlackTree<K extends Comparable<K>, V> { private static final boolean RED = true; private static final boolean BLACK = false; private Node root; private class Node { K key; V val; Node left, right; int size; boolean color; public Node(K key, V val, boolean color, int size) { this.key = key; this.val = val; this.color = color; this.size = size; } } private boolean isRed(Node x) { if (x == null) return false; return x.color == RED; } private int size(Node x) { if (x == null) return 0; return x.size; } public int size() { return size(root); } public boolean isEmpty() { return root == null; } public V get(K key) { if (key == null) throw new IllegalArgumentException("argument to get() is null"); return get(root, key); } private V get(Node x, K key) { while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else return x.val; } return null; } public boolean contains(K key) { return get(key) != null; } public void put(K key, V val) { if (key == null) throw new IllegalArgumentException("first argument to put() is null"); if (val == null) { delete(key); return; } root = put(root, key, val); root.color = BLACK; } private Node put(Node h, K key, V val) { if (h == null) return new Node(key, val, RED, 1); int cmp = key.compareTo(h.key); if (cmp < 0) h.left = put(h.left, key, val); else if (cmp > 0) h.right = put(h.right, key, val); else h.val = val; // fix-up any right-leaning links if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h); h.size = size(h.left) + size(h.right) + 1; return h; } public void deleteMin() { if (isEmpty()) throw new NoSuchElementException("BST underflow"); // if both children of root are black, set root to red if (!isRed(root.left) && !isRed(root.right)) root.color = RED; root = deleteMin(root); if (!isEmpty()) root.color = BLACK; } private Node deleteMin(Node h) { if (h.left == null) return null; if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h); h.left = deleteMin(h.left); return balance(h); } public void deleteMax() { if (isEmpty()) throw new NoSuchElementException("BST underflow"); // if both children of root are black, set root to red if (!isRed(root.left) && !isRed(root.right)) root.color = RED; root = deleteMax(root); if (!isEmpty()) root.color = BLACK; } private Node deleteMax(Node h) { if (isRed(h.left)) h = rotateRight(h); if (h.right == null) return null; if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h); h.right = deleteMax(h.right); return balance(h); } public void delete(K key) { if (key == null) throw new IllegalArgumentException("argument to delete() is null"); if (!contains(key)) return; // if both children of root are black, set root to red if (!isRed(root.left) && !isRed(root.right)) root.color = RED; root = delete(root, key); if (!isEmpty()) root.color = BLACK; } private Node delete(Node h, K key) { if (key.compareTo(h.key) < 0) { if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h); h.left = delete(h.left, key); } else { if (isRed(h.left)) h = rotateRight(h); if (key.compareTo(h.key) == 0 && (h.right == null)) return null; if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h); if (key.compareTo(h.key) == 0) { Node x = min(h.right); h.key = x.key; h.val = x.val; h.right = deleteMin(h.right); } else h.right = delete(h.right, key); } return balance(h); } private Node rotateRight(Node h) { assert (h != null) && isRed(h.left); Node x = h.left; h.left = x.right; x.right = h; x.color = x.right.color; x.right.color = RED; x.size = h.size; h.size = size(h.left) + size(h.right) + 1; return x; } private Node rotateLeft(Node h) { assert (h != null) && isRed(h.right); Node x = h.right; h.right = x.left; x.left = h; x.color = x.left.color; x.left.color = RED; x.size = h.size; h.size = size(h.left) + size(h.right) + 1; return x; } private void flipColors(Node h) { assert !isRed(h) && isRed(h.left) && isRed(h.right); h.color = RED; h.left.color = BLACK; h.right.color = BLACK; } private Node moveRedLeft(Node h) { assert (h != null); assert isRed(h) && !isRed(h.left) && !isRed(h.left.left); flipColors(h); if (isRed(h.right.left)) { h.right = rotateRight(h.right); h = rotateLeft(h); flipColors(h); } return h; } private Node moveRedRight(Node h) { assert (h != null); assert isRed(h) && !isRed(h.right) && !isRed(h.right.left); flipColors(h); if (isRed(h.left.left)) { h = rotateRight(h); flipColors(h); } return h; } private Node balance(Node h) { assert (h != null); if (isRed(h.right)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h); h.size = size(h.left) + size(h.right) + 1; return h; } public K min() { if (isEmpty()) throw new NoSuchElementException("calls min() with empty symbol table"); return min(root).key; } private Node min(Node x) { if (x.left == null) return x; else return min(x.left); } public K max() { if (isEmpty()) throw new NoSuchElementException("calls max() with empty symbol table"); return max(root).key; } private Node max(Node x) { if (x.right == null) return x; else return max(x.right); } public Iterable<K> keys() { if (isEmpty()) return new LinkedList<K>(); return keys(min(), max()); } public Iterable<K> keys(K lo, K hi) { if (lo == null) throw new IllegalArgumentException("first argument to keys() is null"); if (hi == null) throw new IllegalArgumentException("second argument to keys() is null"); Queue<K> queue = new LinkedList<K>(); keys(root, queue, lo, hi); return queue; } private void keys(Node x, Queue<K> queue, K lo, K hi) { if (x == null) return; int cmplo = lo.compareTo(x.key); int cmphi = hi.compareTo(x.key); if (cmplo < 0) keys(x.left, queue, lo, hi); if (cmplo <= 0 && cmphi >= 0) queue.offer(x.key); if (cmphi > 0) keys(x.right, queue, lo, hi); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值