红黑树-Java实现
来源:互联网 发布:世界国家城市数据库 编辑:程序博客网 时间:2024/06/06 05:27
改实现是基于Robert Sedgewick Kevin Wayne的红宝典《算法》3.3.2节:红黑二叉查找树
github : https://github.com/renxue/algorithm-for-java/blob/master/src/com/fengchengpeng/btree/RedBlackTree.java
public class RedBlackTree<Key extends Comparable<Key>, Value> { private Node root; private static final boolean BLACK = true; private static final boolean RED = false; private class Node { Key key; Value value; Node left, right; int N; boolean color; public Node(Key key, Value value, int n, boolean color) { this.key = key; this.value = value; this.N = n; this.color = color; } } private boolean isRed(Node n) { if (n == null) return false; return n.color == RED; } public int size(Node x) { if (x == null) return 0; else return x.N; } public void flipColors(Node x) { x.color = RED; x.left.color = BLACK; x.right.color = BLACK; } public void put(Key key, Value val) { root = put(root, key, val); root.color = BLACK; } public Node put(Node h, Key key, Value val) { if (h == null) { return new Node(key, val, 1, RED); } 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.value = val; 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.N = size(h.left) + size(h.right) + 1; return h; } protected Node rotateLeft(Node h) { Node x = h.right; h.right = x.left; x.left = h; x.color = h.color; h.color = RED; x.N = h.N; h.N = 1 + size(h.left) + size(h.right); return x; } protected Node rotateRight(Node h) { Node x = h.left; h.left = x.right; x.right = h; x.color = h.color; h.color = RED; x.N = h.N; h.N = 1 + size(h.left) + size(h.right); return x; } public void iterator(Node node) { if (node == null) System.out.println("tree is null"); if (node.left != null) iterator(node.left); if (node.color) System.out.print(node.key + ":BLACK "); else System.out.print(node.key + ":RED "); if (node.right != null) iterator(node.right); } public static void main(String[] args) { RedBlackTree<String, String> redBlackTree = new RedBlackTree<>(); System.out.println(redBlackTree.size(redBlackTree.root)); redBlackTree.put("S", "s"); redBlackTree.put("E", "e"); redBlackTree.put("A", "a"); redBlackTree.put("R", "r"); redBlackTree.put("C", "c"); redBlackTree.put("H", "h"); redBlackTree.put("X", "x"); redBlackTree.put("M", "m"); redBlackTree.put("P", "p"); redBlackTree.put("L", "l"); System.out.println(redBlackTree.size(redBlackTree.root)); redBlackTree.iterator(redBlackTree.root); }
阅读全文
0 0
- 红黑树之java实现
- java实现红黑树
- 红黑树的Java实现
- 红黑树java实现
- 红黑树的JAVA实现
- java实现红黑树
- 红黑树的java实现
- 红黑树的Java实现
- 红黑树的Java实现
- 红黑树的java实现
- 数据结构:红黑树java实现
- 红黑树java实现
- Java实现红黑树
- Java实现红黑树
- java 实现红黑树
- 红黑树-java实现
- 红黑树的java实现
- 红黑树Java的实现
- 缓冲区溢出
- 微信发布的辟谣小程序
- RxJava2_整体流程分析
- C#异步编程async await
- 为什么推荐使用npm安装Vue,而不是直接在HTML中引入vue.js
- 红黑树-Java实现
- struts2+spring+hibernate集成框架的环境搭建
- 数据结构树汇总
- tcp wrapper
- 2.3线性表的链式表示和实现(静态链表)
- LeetCode OJ 279 Perfect Squares [Medium]
- date的使用
- 车架号图像识别,车架号拍照识别,车架号扫描识别,车架号OCR识别
- NEUQ 1768 一道简单的递推题