---
title: "2-3 tree"
author: "leon_a"
date: "2013-10-09T17:10:02Z"
tags: []
categories: ["编程语言"]
slug: "2-3-tree"
draft: false
lastmod: "2013-10-10T11:08:29Z"
id: 1953865
url: "https://leon-a.iteye.com/blog/1953865"
---

\[code="java"] package com.leon.cc; import com.leon.util.Stack; /\** * @author : Leon * @since : 2013-10-9 * @see : \*/ public class Tree { Node root = new Node(); public void insert(String str) { Node node = search\_node(root, str); node.insert(str); while (need\_split(node)) { if (node == root) { node = node.split(); root = node; } else { node = node.split(); } } } public void delete(String str) { Node node = contain(str); Node leaf = null; if (!node.is\_leaf()) { leaf = tree\_successor(node, str); int index = node.index\_item(str); String temp = node.values\[index]; node.values\[index] = leaf.values\[0]; leaf.values\[0] = temp; } else { leaf = node; } leaf.delete(str); if (leaf.item\_size == 0) { fix(leaf); } } private void fix(Node n) { if (n.parent == null) { root = n.disConnect(0); } else { Node p = n.parent; int\[] ary = new int\[3]; int index = p.index\_child(n); if (has\_2item\_sibling(n, ary)) { if (p.item\_size == 2) { if (index == 0) { if (ary\[1] == 2) { leftRotate(0, n); } else { leftRotate(0, n); leftRotate(1, n.parent.childs\[1]); } } else if (index == 1) { if (ary\[2] == 2) { leftRotate(1, n); } else { rightRotate(1, n); } } else { if (ary\[1] == 2) { rightRotate(2, n); } else { rightRotate(2, n); rightRotate(1, n.parent.childs\[1]); } } } else { if (index == 0) { leftRotate(0, n); } else { rightRotate(1, n); } } } else { if (p.item\_size == 2) { if (index == 0) { mergeRight(0, n); } else if (index == 1) { mergeLeft(1, n); } else { mergeLeft(2, n); } } else { if (index == 0) { mergeRight(0, n); } else { mergeLeft(1, n); } fix(p); } } } } private void mergeLeft(int i, Node n) { Node child = null; Node p = n.parent; if (!n.is\_leaf()) { child = n.childs\[0]; n.delete\_child(0); } p.delete\_child(i); if (i == 1) { p.childs\[0].insert(p.values\[0]); p.delete(p.values\[0]); if (child != null) { p.childs\[0].insert\_child(2, child); } } else { int sibling\_size = p.item\_size; p.childs\[1].insert(p.values\[sibling\_size - 1]); p.delete(p.values\[sibling\_size - 1]); if (child != null) { p.childs\[1].insert\_child(2, child); } } } private void mergeRight(int i, Node n) { Node child = null; Node p = n.parent; if (!n.is\_leaf()) { child = n.childs\[0]; n.delete\_child(0); } p.delete\_child(0); p.childs\[0].insert(p.values\[0]); p.delete(p.values\[0]); if (child != null) { p.childs\[0].insert\_child(0, child); } } private boolean has\_2item\_sibling(Node n, int\[] ary) { Node p = n.parent; ary\[0] = p.childs\[0].item\_size; ary\[1] = p.childs\[1].item\_size; if (p.item\_size == 2) { ary\[2] = p.childs\[2].item\_size; } for (int i = 0; i &lt; ary.length; i++) { if (ary\[i] == 2) { return true; } } return false; } private void leftRotate(int index, Node n) { if (index == 0) { n.insert(n.parent.values\[0]); n.parent.delete(n.parent.values\[0]); n.parent.insert(n.parent.childs\[1].values\[0]); n.parent.childs\[1].delete(n.parent.childs\[1].values\[0]); if (!n.is\_leaf()) { Node node = n.parent.childs\[1].delete\_child(0); n.insert\_child(1, node); } } else { n.insert(n.parent.values\[1]); n.parent.delete(n.parent.values\[1]); n.parent.insert(n.parent.childs\[2].values\[0]); n.parent.childs\[2].delete(n.parent.childs\[2].values\[0]); if (!n.is\_leaf()) { Node node = n.parent.childs\[2].delete\_child(0); n.insert\_child(1, node); } } } private void rightRotate(int index, Node n) { if (index == 2) { n.insert(n.parent.values\[1]); n.parent.delete(n.parent.values\[1]); int sibling\_size = n.parent.childs\[1].item\_size; n.parent.insert(n.parent.childs\[1].values\[sibling\_size - 1]); n.parent.childs\[1].delete(n.parent.childs\[1].values\[sibling\_size - 1]); if (!n.is\_leaf()) { Node node = n.parent.childs\[1].delete\_child(sibling\_size); n.insert\_child(0, node); } } else { n.insert(n.parent.values\[0]); n.parent.delete(n.parent.values\[0]); int sibling\_size = n.parent.childs\[0].item\_size; n.parent.insert(n.parent.childs\[0].values\[sibling\_size - 1]); n.parent.childs\[0].delete(n.parent.childs\[0].values\[sibling\_size - 1]); if (!n.is\_leaf()) { Node node = n.parent.childs\[0].delete\_child(sibling\_size); n.insert\_child(0, node); } } } public Node tree\_successor(Node node, String str) { if (!node.is\_leaf()) { int index = node.index\_item(str); Node child = node.childs\[index + 1]; return tree\_mins(child); } return node; } private Node tree\_mins(Node child) { Node temp = child; while (!temp.is\_leaf()) { temp = temp.childs\[0]; } return temp; } public Node contain(String str) { Node current = root; while (!current.contain(str)) { if (current.item\_size == 2) { String small = current.values\[0]; String large = current.values\[1]; if (str.compareTo(small) &lt; 0) { current = current.childs\[0]; } else if (str.compareTo(small) &gt;= 0 && str.compareTo(large) &lt;= 0) { current = current.childs\[1]; } else { current = current.childs\[2]; } } else { String small = current.values\[0]; if (str.compareTo(small) &lt; 0) { current = current.childs\[0]; } else { current = current.childs\[1]; } } } return current; } public boolean need\_split(Node node) { return node.item\_size &gt; 2 ? true : false; } public Node search\_node(Node node, String str) { if (is\_leaf(node)) { return node; } else { if (node.item\_size == 2) { String small = node.values\[0]; String large = node.values\[1]; if (str.compareTo(small) &lt; 0) { Node next = create\_node(node, 0); return search\_node(next, str); } else if (str.compareTo(small) &gt;= 0 && str.compareTo(large) &lt;= 0) { Node next = create\_node(node, 1); return search\_node(next, str); } else { Node next = create\_node(node, 2); return search\_node(next, str); } } else { String small = node.values\[0]; if (str.compareTo(small) &lt; 0) { Node next = create\_node(node, 0); return search\_node(next, str); } else { Node next = create\_node(node, 1); return search\_node(next, str); } } } } private Node create\_node(Node node, int i) { Node child = node.childs\[i]; if (child == null) { child = new Node(); node.childs\[i] = child; child.parent = node; } return child; } public boolean is\_leaf(Node node) { return node.childs\[0] == null; } public static void main(String\[] args) { Tree t = new Tree(); String\[] str = new String\[] { "horse", "cow", "pig", "seal", "rat", "dog", "goat", "elephant", "fish", "rooster", "zebra", "roach", "cat", "hen", "llama", "aardvark", "hog", "donkey", "rhino", "hippo", "tiger", "lamb", "lion", "leopard", "lynx", "kitty", "ant", "ape", "animal" }; for (int i = 0; i &lt; str.length; i++) { t.insert(str\[i]); } t.delete("animal"); t.delete("aardvark"); t.delete("cat"); t.delete("ape"); t.delete("donkey"); t.delete("ant"); t.delete("hog"); t.delete("hen"); t.delete("fish"); t.delete("goat"); t.delete("hippo"); t.delete("kitty"); t.delete("cow"); t.delete("dog"); t.delete("elephant"); t.delete("tiger"); t.delete("zebra"); t.delete("horse"); t.delete("rat"); t.delete("rhino"); t.delete("pig"); t.delete("seal"); t.delete("lynx"); t.delete("roach"); t.delete("lion"); t.delete("llama"); t.delete("lamb"); t.delete("rooster"); t.delete("leopard"); String rs = t.toString(); System.out.println(rs); } public String toString() { if (root == null) { return null; } StringBuilder sb = new StringBuilder(); sb.append("digraph g {\\n"); sb.append("\\tnode\[shape = record, width = .1, height = .1];\\n"); Stack stack = new Stack(); int k = 0; Stack k\_stack = new Stack(); stack.push(root); k\_stack.push(k); sb.append("\\tnode" + k + "\[label = \\"{ " + root.getName() + " }\\", color = lightgray, style = filled];\\n"); while (!stack.is\_empty()) { Node parent = stack.pop(); String parentNode = "node" + k\_stack.pop(); for (int i = 0; i &lt; parent.childs.length; i++) { if (parent.childs\[i] != null) { String childNode = "node" + (++k); sb.append("\\t" + childNode + "\[label = \\"{ " + parent.childs\[i].getName() + " }\\", color = lightgray, style = filled];\\n"); sb.append("\\t" + parentNode + ":n-&gt;" + childNode + ":n;\\n"); stack.push(parent.childs\[i]); k\_stack.push(k); } } } sb.append("}\\n"); return sb.toString(); } } class Node { String\[] values = new String\[3]; int item\_size; Node\[] childs = new Node\[4]; Node parent; public Node(String str) { values\[0] = str; item\_size++; } public String getName() { StringBuffer sb = new StringBuffer(); for (int i = 0; i &lt; item\_size; i++) { sb.append(values\[i] + " "); } return sb.toString(); } public Node() { } public boolean contain(String str) { for (int i = 0; i &lt; item\_size; i++) { if (values\[i].equals(str)) { return true; } } return false; } public int insert(String str) { int pos = item\_size; for (int i = item\_size - 1; i &gt;= 0; i--) { if (values\[i].compareTo(str) &gt; 0) { values\[i + 1] = values\[i]; pos = i; } } values\[pos] = str; item\_size++; return pos; } public void delete(String str) { int index = index\_item(str); for (int i = index; i &lt; item\_size; i++) { values\[i] = values\[i + 1]; } item\_size--; } public void insert\_child(int index, Node node) { for (int i = childs.length - 2; i &gt;= index; i--) { childs\[i + 1] = childs\[i]; } childs\[index] = node; childs\[index].parent = this; } public Node delete\_child(int index) { Node node = childs\[index]; for (int i = index; i &lt; childs.length - 1; i++) { childs\[i] = childs\[i + 1]; } node.parent = null; return node; } public boolean is\_leaf() { return childs\[0] == null; } public int index\_child(Node node) { for (int i = 0; i &lt; childs.length; i++) { if (childs\[i].equals(node)) { return i; } } return -1; } public int index\_item(String str) { for (int i = 0; i &lt; item\_size; i++) { if (values\[i].equals(str)) { return i; } } return -1; } public Node disConnect(int index) { Node result = childs\[index]; if (result != null) result.parent = null; childs\[index] = null; return result; } public void connect(int index, Node child) { childs\[index] = child; if (child != null) child.parent = this; } public Node split() { Node left = this; String middle = values\[1]; Node right = new Node(values\[2]); left.values\[1] = null; left.values\[2] = null; left.item\_size = 1; if (parent == null) { parent = new Node(middle); parent.connect(0, left); parent.connect(1, right); } else { int index = parent.insert(middle); parent.insert\_child(index + 1, right); } if (!is\_leaf()) { Node child1 = disConnect(2); Node child2 = disConnect(3); right.connect(0, child1); right.connect(1, child2); } return parent; } } \[/code]