个人中心 DownLoad 以下是一个简单的二叉树算法的Java实现: ``` class Node { int value; Node left; Node right; public Node(int value) { this.value = value; this.left = null; this.right = null; } } class BinaryTree { Node root; public BinaryTree() { this.root = null; } public void insert(int value) { Node newNode = new Node(value); if (root == null) { root = newNode; return; } Node current = root; while (true) { if (value < current.value) { if (current.left == null) { current.left = newNode; return; } current = current.left; } else { if (current.right == null) { current.right = newNode; return; } current = current.right; } } } public boolean search(int value) { Node current = root; while (current != null) { if (current.value == value) { return true; } else if (value < current.value) { current = current.left; } else { current = current.right; } } return false; } public void inorderTraversal(Node node) { if (node != null) { inorderTraversal(node.left); System.out.print(node.value + " "); inorderTraversal(node.right); } } public void preorderTraversal(Node node) { if (node != null) { System.out.print(node.value + " "); preorderTraversal(node.left); preorderTraversal(node.right); } } public void postorderTraversal(Node node) { if (node != null) { postorderTraversal(node.left); postorderTraversal(node.right); System.out.print(node.value + " "); } } } public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.insert(5); tree.insert(3); tree.insert(7); tree.insert(1); tree.insert(9); System.out.println("Inorder Traversal:"); tree.inorderTraversal(tree.root); System.out.println("\nPreorder Traversal:"); tree.preorderTraversal(tree.root); System.out.println("\nPostorder Traversal:"); tree.postorderTraversal(tree.root); System.out.println("\nSearch for 7: " + tree.search(7)); System.out.println("Search for 2: " + tree.search(2)); } } ``` 这个算法实现了二叉树的插入、搜索和三种遍历方式(中序、前序和后序)。在主函数中,我们创建了一个二叉树并插入了一些节点,然后进行了遍历和搜索操作。
以下是Java语言实现的红黑树算法:
public class RedBlackTree<T extends Comparable<T>> {
private static final boolean RED = true;
private static final boolean BLACK = false;
private Node<T> root;
private class Node<T> {
T key;
Node<T> left;
Node<T> right;
boolean color;
int size;
public Node(T key, boolean color, int size) {
this.key = key;
this.color = color;
this.size = size;
}
}
// 左旋转
private Node<T> rotateLeft(Node<T> h) {
Node<T> x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = RED;
x.size = h.size;
h.size = size(h.left) + size(h.right) + 1;
return x;
}
// 右旋转
private Node<T> rotateRight(Node<T> h) {
Node<T> x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = RED;
x.size = h.size;
h.size = size(h.left) + size(h.right) + 1;
return x;
}
// 颜色反转
private void flipColors(Node<T> h) {
h.color = RED;
h.left.color = BLACK;
h.right.color = BLACK;
}
// 插入节点
public void insert(T key) {
root = insert(root, key);
root.color = BLACK;
}
private Node<T> insert(Node<T> h, T key) {
if (h == null) return new Node<>(key, RED, 1);
int cmp = key.compareTo(h.key);
if (cmp < 0) h.left = insert(h.left, key);
else if (cmp > 0) h.right = insert(h.right, key);
else h.key = key;
// 保持红黑树性质
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;
}
// 返回指定节点的子树大小
private int size(Node<T> x) {
if (x == null) return 0;
return x.size;
}
// 判断指定节点的颜色是否为红色
private boolean isRed(Node<T> x) {
if (x == null) return false;
return x.color == RED;
}
// 中序遍历红黑树
public void traverse() {
traverse(root);
}
private void traverse(Node<T> h) {
if (h == null) return;
traverse(h.left);
System.out.println(h.key);
traverse(h.right);
}
}
可以使用 RedBlackTree
类创建一个红黑树, insert
方法用于插入节点, traverse
方法用于中序遍历红黑树。