用JAVA写一个二叉树示例代码
时间: 2023-09-03 12:25:08 浏览: 62
### 回答1:
// Java代码实现二叉树
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class BinaryTree {
public static TreeNode root;
public BinaryTree() {
this.root = null;
}
public void insert(int id){
TreeNode newNode = new TreeNode(id);
if(root==null){
root = newNode;
return;
}
TreeNode current = root;
TreeNode parent = null;
while(true){
parent = current;
if(id<current.val){
current = current.left;
if(current==null){
parent.left = newNode;
return;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
return;
}
}
}
}
public void display(TreeNode root){
if(root!=null){
display(root.left);
System.out.print(" " + root.val);
display(root.right);
}
}
public static void main(String arg[]){
BinaryTree b = new BinaryTree();
b.insert(3);b.insert(8);
b.insert(1);b.insert(4);b.insert(6);b.insert(2);b.insert(10);b.insert(9);
b.insert(20);b.insert(25);b.insert(15);b.insert(16);
System.out.println("原始数据:");
b.display(b.root);
System.out.println("");
}
}
### 回答2:
下面是一个使用Java编写的二叉树示例代码。
```java
// 定义二叉树节点类
class Node {
int data;
Node left;
Node right;
public Node(int item) {
data = item;
left = null;
right = null;
}
}
public class BinaryTree {
Node root;
// 初始化二叉树的根节点
public BinaryTree(int key) {
root = new Node(key);
}
public BinaryTree() {
root = null;
}
// 在二叉树中插入节点
public void insert(int key) {
root = insertRecursive(root, key);
}
// 递归方式插入节点
private Node insertRecursive(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.data) {
root.left = insertRecursive(root.left, key);
} else if (key > root.data) {
root.right = insertRecursive(root.right, key);
}
return root;
}
// 在二叉树中查找节点
public Node search(Node root, int key) {
if (root == null || root.data == key) {
return root;
}
if (key < root.data) {
return search(root.left, key);
} else {
return search(root.right, key);
}
}
// 删除二叉树中的节点
public void delete(int key) {
root = deleteNode(root, key);
}
// 递归方式删除节点
private Node deleteNode(Node root, int key) {
if (root == null) {
return root;
}
if (key < root.data) {
root.left = deleteNode(root.left, key);
} else if (key > root.data) {
root.right = deleteNode(root.right, key);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
root.data = minValue(root.right);
root.right = deleteNode(root.right, root.data);
}
return root;
}
// 获取二叉树中的最小值
private int minValue(Node root) {
int minValue = root.data;
while (root.left != null) {
minValue = root.left.data;
root = root.left;
}
return minValue;
}
// 中序遍历二叉树
private void inorderTraversal(Node root) {
if (root != null) {
inorderTraversal(root.left);
System.out.print(root.data + " ");
inorderTraversal(root.right);
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
// 插入节点
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// 中序遍历二叉树
System.out.println("二叉树中序遍历结果:");
tree.inorderTraversal(tree.root);
System.out.println();
// 查找节点
int key = 40;
Node searchedNode = tree.search(tree.root, key);
if (searchedNode != null) {
System.out.println("找到了节点 " + key);
} else {
System.out.println("没有找到节点 " + key);
}
// 删除节点
tree.delete(30);
System.out.println("删除节点 30 后的二叉树中序遍历结果:");
tree.inorderTraversal(tree.root);
}
}
```
这段代码实现了一个二叉树的基本操作,包括插入节点、查找节点、删除节点和中序遍历等功能。
### 回答3:
下面是一个使用Java编写的二叉树的示例代码:
```java
// 定义二叉树节点的数据结构
class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// 定义二叉树类
class BinaryTree {
TreeNode root;
public BinaryTree() {
this.root = null;
}
// 向二叉树插入节点
public void insert(int data) {
root = insertNode(root, data);
}
private TreeNode insertNode(TreeNode root, int data) {
if (root == null) {
root = new TreeNode(data);
return root;
}
if (data < root.data) {
root.left = insertNode(root.left, data);
} else if (data > root.data) {
root.right = insertNode(root.right, data);
}
return root;
}
// 先序遍历二叉树
public void preorderTraversal() {
preorder(root);
}
private void preorder(TreeNode node) {
if (node != null) {
System.out.print(node.data + " ");
preorder(node.left);
preorder(node.right);
}
}
// 中序遍历二叉树
public void inorderTraversal() {
inorder(root);
}
private void inorder(TreeNode node) {
if (node != null) {
inorder(node.left);
System.out.print(node.data + " ");
inorder(node.right);
}
}
// 后序遍历二叉树
public void postorderTraversal() {
postorder(root);
}
private void postorder(TreeNode node) {
if (node != null) {
postorder(node.left);
postorder(node.right);
System.out.print(node.data + " ");
}
}
}
// 测试二叉树代码
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(4);
tree.insert(6);
tree.insert(9);
// 先序遍历二叉树
System.out.println("先序遍历结果:");
tree.preorderTraversal();
// 中序遍历二叉树
System.out.println("\n中序遍历结果:");
tree.inorderTraversal();
// 后序遍历二叉树
System.out.println("\n后序遍历结果:");
tree.postorderTraversal();
}
}
```
该示例代码定义了一个`TreeNode`类作为二叉树节点的数据结构,以及一个`BinaryTree`类来表示二叉树。其中,`insert`方法用于插入新的节点,`preorderTraversal`、`inorderTraversal`和`postorderTraversal`方法用于先序、中序和后序遍历二叉树。在主函数中,创建一个二叉树并向其中插入一些节点,然后分别进行先序、中序和后序遍历输出结果。