Java Data Structures B-Tree
interface Comparable { public int compare(Object a, Object b); } interface Traversal { public void process(Object o); } class TreeNode { public TreeNode(Object o) { data = o;//from www . j av a2s . co m color = RED; left = right = null; } public Object getData() { return data; } public void setData(Object o) { data = o; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode l) { left = l; } public TreeNode getRight() { return right; } public void setRight(TreeNode r) { right = r; } public boolean getColor() { return color; } public void setColor(boolean c) { color = c; } public void flip() { color = !color; } public int countRedChildren() { int count = 0; if (left != null && left.color == RED) { count += left.countRedChildren(); count++; } if (right != null && right.color == RED) { count += right.countRedChildren(); count++; } return count; } public boolean isOverFull(int order) { if (color == RED) return false; if (countRedChildren() >= order - 1) return true; return false; } public String toString() { return "Node " + data; } private TreeNode left; private TreeNode right; private Object data; private boolean color; public static final boolean RED = true; public static final boolean BLACK = false; } class BTree { public BTree(int order, Comparable c) { this.order = order; this.c = c; root = null; } public void add(Object o) { root = add(root, new TreeNode(o)); root.setColor(TreeNode.BLACK); } protected TreeNode add(TreeNode root, TreeNode newNode) { if (root == null) return newNode; int val = c.compare(newNode.getData(), root.getData()); if (val < 0) { if (root.getLeft() == null) { root.setLeft(newNode); } else { root.setLeft(add(root.getLeft(), newNode)); } } else { if (root.getRight() == null) { root.setRight(newNode); } else { root.setRight(add(root.getRight(), newNode)); } } root = balance(root); if (root.isOverFull(order)) { split(root); } return root; } protected int branchCount(TreeNode child) { if (child != null) { if (child.getColor() == TreeNode.RED) { return child.countRedChildren() + 1; } } return 0; } protected TreeNode balance(TreeNode node) { if (node == null) return node; if (node.getColor() != TreeNode.BLACK) return node; if (node.countRedChildren() < 2) return node; while (branchCount(node.getLeft()) < branchCount(node.getRight()) - 1) { node = rotate(node, LEFT); } while (branchCount(node.getRight()) < branchCount(node.getLeft()) - 1) { node = rotate(node, RIGHT); } return node; } public void split(TreeNode node) { if (node.isOverFull(order) == false) return; node.flip(); if (node.getRight() != null) node.getRight().flip(); if (node.getLeft() != null) node.getLeft().flip(); } protected TreeNode rotate(TreeNode root, int direction) { TreeNode newRoot = null; TreeNode orphan = null; boolean tmp; tmp = root.getColor(); switch (direction) { case RIGHT: newRoot = root.getLeft(); if (newRoot == null) return root; orphan = newRoot.getRight(); root.setLeft(orphan); newRoot.setRight(root); break; case LEFT: newRoot = root.getRight(); if (newRoot == null) return root; orphan = newRoot.getLeft(); root.setRight(orphan); newRoot.setLeft(root); break; } root.setColor(newRoot.getColor()); newRoot.setColor(tmp); return newRoot; } protected Object search(TreeNode root, Object o) { if (root == null) { return null; } int val = c.compare(o, root.getData()); if (val == 0) { return root.getData(); } else if (val < 0) { return search(root.getLeft(), o); } else if (val > 0) { return search(root.getRight(), o); } return null; } public void traverse(Traversal t) { traverse(INORDER, t); } public void traverse(int type, Traversal t) { traverse(root, type, t); } protected void traverse(TreeNode root, int type, Traversal t) { TreeNode tmp; if (type == PREORDER) t.process(root.getData()); if ((tmp = root.getLeft()) != null) traverse(tmp, type, t); if (type == INORDER) t.process(root.getData()); if ((tmp = root.getRight()) != null) traverse(tmp, type, t); } protected TreeNode root; protected Comparable c; protected int order; public final static int INORDER = 1; public final static int PREORDER = 2; protected final static int RIGHT = 1; protected final static int LEFT = 2; } public class Main { public static void main(String args[]) { String keys[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "ZA", "YA", "UA", "XA", "WA", "VA", "TA", "SA", "RA", "QA", "PA", "OA", "NA", "MA", "LA", "KA", "JA", "IA", "HA", "GA", "FA", "EA", "DA", "CA", "BA", "AA" }; BTree t = new BTree(6, new Comparable() { public int compare(Object a, Object b) { return ((String) a).compareTo((String) b); } }); for (int i = 0; i < keys.length; i++) { t.add(keys[i]); } t.traverse(BTree.INORDER, new Traversal() { public void process(Object o) { System.out.println(o); } }); } }