Java 二叉树 红黑树算法

文章展示了如何用Java实现二叉树的插入、搜索及中序、前序、后序遍历。此外,还提供了红黑树的插入和中序遍历的实现,确保了数据结构的高效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人中心
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 方法用于中序遍历红黑树。

 使用的ChatGpt实现的功能  个人主页  DownLoad

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android-Developer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值