java中二叉树_JAVA实现二叉树

package com.hb.binaryTree;

class BinaryTreeNode {

int data;

BinaryTreeNode left;

BinaryTreeNode right;

public BinaryTreeNode(int data) {

this.data = data;

this.left = null;

this.right = null;

}

}

public class BinaryTree {

private BinaryTreeNode root;

public BinaryTree() {

this.root = null;

}

public void insertIntoTree(int data) {

BinaryTreeNode newNode = new BinaryTreeNode(data);

if (root == null) {

root = newNode;

} else {

BinaryTreeNode currentNode = root;

BinaryTreeNode parentNode = root;

while (currentNode != null) {

if (data < currentNode.data) {

parentNode = currentNode;

currentNode = currentNode.left;

if (currentNode == null) {

parentNode.left = newNode;

}

} else if (data > currentNode.data) {

parentNode = currentNode;

currentNode = currentNode.right;

if (currentNode == null) {

parentNode.right = newNode;

}

}

}

}

}

public boolean isEmpty() {

return root == null;

}

public BinaryTreeNode search(int data) {

return BinaryTreeSearch(root, data);

}

public BinaryTreeNode BinaryTreeSearch(BinaryTreeNode node, int data) {

if (node == null)

return null;

if (data == node.data) {

return node;

} else if (data < node.data) {

return BinaryTreeSearch(node.left, data);

} else {

return BinaryTreeSearch(node.right, data);

}

}

public void preOrderTraverse(BinaryTreeNode root){

if(root!=null){

System.out.print(root.data+", ");

preOrderTraverse(root.left);

preOrderTraverse(root.right);

}

}

public void inOrderTraverse(BinaryTreeNode root){

if(root!=null){

inOrderTraverse(root.left);

System.out.print(root.data+", ");

inOrderTraverse(root.right);

}

}

public void postOrderTraverse(BinaryTreeNode root){

if(root!=null){

postOrderTraverse(root.left);

postOrderTraverse(root.right);

System.out.print(root.data+", ");

}

}

public int getMaxValue(BinaryTreeNode root){

while(root.right!=null){

root = root.right;

}

return root.data;

}

public int getMinValue(BinaryTreeNode root){

while(root.left!=null){

root = root.left;

}

return root.data;

}

public static void main(String[] args){

BinaryTree binaryTree = new BinaryTree();

binaryTree.insertIntoTree(12);

binaryTree.insertIntoTree(1);

binaryTree.insertIntoTree(2);

binaryTree.insertIntoTree(45);

binaryTree.insertIntoTree(24);

binaryTree.insertIntoTree(68);

binaryTree.insertIntoTree(89);

binaryTree.insertIntoTree(23);

System.out.println("中根遍历");

binaryTree.inOrderTraverse(binaryTree.root);

System.out.println("\n先根遍历");

binaryTree.preOrderTraverse(binaryTree.root);

System.out.println("\n后根遍历");

binaryTree.postOrderTraverse(binaryTree.root);

BinaryTreeNode node = binaryTree.search(89);

System.out.println(node.data);

System.out.println("最大数值:"+binaryTree.getMaxValue(binaryTree.root));

System.out.println("最小数值:"+binaryTree.getMinValue(binaryTree.root));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值