java实现二叉树的常见操作

java

       树型结构是最常见的非线性结构,其中二叉树最为常见。今天我主要就是用java来实现一下树的一些常见操作。

       首先需要一个用来存储树节点值的javabean:

public class TreeBean {

private int nodeValue;

public int getNodeValue() {

return nodeValue;

}

public void setNodeValue(int nodeValue) {

this.nodeValue = nodeValue;

}

}

       然后是树的节点bean:

public class TreeNode{

private TreeBean data;

private TreeNode leftNode;

private TreeNode rightNode;

//构造函数

public TreeNode(){

data = new TreeBean();

}

public TreeBean getData() {

return data;

}

public void setData(TreeBean data) {

this.data = data;

}

public TreeNode getLeftNode() {

return leftNode;

}

public void setLeftNode(TreeNode leftNode) {

this.leftNode = leftNode;

}

public TreeNode getRightNode() {

return rightNode;

}

public void setRightNode(TreeNode rightNode) {

this.rightNode = rightNode;

}

}

       最后是Tree的主体:

public class Tree {

//树的根节点

private TreeNode root;

public TreeNode getRoot() {

return root;

}

public void setRoot(TreeNode root) {

this.root = root;

}

//无参构造函数

Tree(){}

Tree(int nodeValue){

root = new TreeNode();

TreeBean nodeBean = new TreeBean();

nodeBean.setNodeValue(nodeValue);

root.setData(nodeBean);

}

/**

* 销毁树,将树置空。

* @author letthinking

* @param tree

* @return

*/

public static Tree destroy(Tree tree){

return null;

}

/**

* 给树插入数据

* @author letthinking

* @param root

* @param node

*/

public void insert(TreeNode root,TreeNode node){

//如果根节点为空,则赋值给根节点

if(root == null){

root = node;

}else{

//该节点与它的双亲节点比较,如果小于双亲节点,就将它作为左孩子,否则为右孩子。

if(node.getData().getNodeValue() < root.getData().getNodeValue()){

//判断该节点是否为空,如果不为空就继续递归。

if(root.getLeftNode() == null){

root.setLeftNode(node);

}else{

insert(root.getLeftNode(),node);

}

}else{

if(root.getRightNode() == null){

root.setRightNode(node);

}else{

insert(root.getRightNode(),node);

}

}

}

}

/**

* 将树的所有节点清空

* @author letthinking

* @param root 树的根节点

*/

public void clearTree(TreeNode root){

if(root.getData() == null){

if(root.getLeftNode() != null){

clearTree(root.getLeftNode());

}

if(root.getRightNode() != null){

clearTree(root.getRightNode());

}

}else{

root.setData(null);

if(root.getLeftNode() != null){

clearTree(root.getLeftNode());

}

if(root.getRightNode() != null){

clearTree(root.getRightNode());

}

}

}

/**

* 前序遍历二叉树

* @author letthinking

* @param root

* @return

*/

public String middleIterator(TreeNode root){

StringBuilder str = new StringBuilder();

//判断节点是否为空

if(root == null){

return str.toString();

}else{

//输出节点的值

if(root.getData() != null){

str.append(root.getData().getNodeValue()+",");

}else{

str.append("null,");

}

//递归输出左孩子的值

str.append(middleIterator(root.getLeftNode()));

//递归输出右孩子的值

str.append(middleIterator(root.getRightNode()));

}

return str.toString();

}

/**

* 后序遍历二叉树

* @author letthinking

* @param root

* @return

*/

public String afterIterator(TreeNode root){

StringBuilder str = new StringBuilder();

//判断节点是否为空

if(root == null){

return str.toString();

}else{

//递归输出左孩子的值

str.append(afterIterator(root.getLeftNode()));

//递归输出右孩子的值

str.append(afterIterator(root.getRightNode()));

//输出节点的值

if(root.getData() != null){

str.append(root.getData().getNodeValue()+",");

}else{

str.append("null,");

}

}

return str.toString();

}

/**

* 求树的深度

* @author letthinking

* @param node

* @return

*/

public int treeDepth(TreeNode node){

//定义两个变量用来存储左深度和右深度

int leftDepth = 0;

int rightDepth = 0;

if(node == null){

return 0;

}else{

leftDepth = treeDepth(node.getLeftNode())+1;

rightDepth = treeDepth(node.getRightNode())+1;

}

//返回值最大的深度

return leftDepth>=rightDepth?leftDepth:rightDepth;

}

public static void main(String [] args){

//构造一个只有根节点的空树

Tree tree = new Tree(35);

//创建5个节点

TreeNode treeNode = new TreeNode();

treeNode.getData().setNodeValue(23);

TreeNode treeNode1 = new TreeNode();

treeNode1.getData().setNodeValue(56);

TreeNode treeNode2 = new TreeNode();

treeNode2.getData().setNodeValue(45);

TreeNode treeNode3 = new TreeNode();

treeNode3.getData().setNodeValue(12);

TreeNode treeNode4 = new TreeNode();

treeNode4.getData().setNodeValue(37);

TreeNode treeNode5 = new TreeNode();

treeNode5.getData().setNodeValue(19);

//插入树中

tree.insert(tree.root, treeNode);

tree.insert(tree.root, treeNode1);

tree.insert(tree.root, treeNode2);

tree.insert(tree.root, treeNode3);

tree.insert(tree.root, treeNode4);

tree.insert(tree.root, treeNode5);

//前序变量

String result = tree.middleIterator(tree.getRoot());

System.out.println(result);

//后序序变量

result = tree.afterIterator(tree.getRoot());

System.out.println(result);

//清空数所有节点的值

tree.clearTree(tree.getRoot());

result = tree.middleIterator(tree.getRoot());

System.out.println(result);

//得到树的深度

System.out.println(tree.treeDepth(tree.getRoot()));

}

}

       可能会有地方写的不对,希望大家给指出。




以上是 java实现二叉树的常见操作 的全部内容, 来源链接: utcz.com/z/390578.html

回到顶部