java树类_实现Java二叉树类

实现Java二叉树类

关注:116  答案:2  mip版

解决时间 2021-01-26 11:20

e6cb1a03ad541b3098697807b7bf1798.png

提问者安分守己的小青春

2021-01-26 04:00

写一个二叉树的类,实现添加,删除等相应的功能,并且提供相应的API;

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家陪我去流浪

2021-01-26 04:41

public class BinaryTreeTest

{

public static void main(String args[])

{

BinaryTreeTest b=new BinaryTreeTest();

int data[]={12,11,34,45,67,89,56,43,22,98};

BinaryTree root =new BinaryTree(data[0]);

System.out.print("二叉树的中的数据:  ");

for(int i=1;i{

root.insertTree(root,data[i]);

System.out.print(data[i-1]+";");

}

System.out.println(data[data.length-1]);

int key=Integer.parseInt(args[0]);

if(b.searchkey(root,key))

{

System.out.println("找到了:"+key);

}

else

{

System.out.println("没有找到:"+key);

}

}

public boolean searchkey(BinaryTree root, int key)

{

boolean bl=false;

if(root==null)

{

bl=false;

return bl;

}

else if(root.data==key)

{

bl=true;

return bl;

}

else if(key>=root.data)

{

return searchkey(root.rightpoiter,key);

}

return searchkey(root.leftpoiter,key);

}

}

class BinaryTree

{

int data;

BinaryTree leftpoiter;

BinaryTree rightpoiter;

BinaryTree(int data)

{

this.data=data;

leftpoiter=null;

rightpoiter=null;

}

public void insertTree(BinaryTree root, int data)

{

if(data>=root.data)

{

if(root.rightpoiter==null)

{

root.rightpoiter=new BinaryTree(data);

}

else

{

insertTree(root.rightpoiter,data);

}

}

else

{

if(root.leftpoiter==null)

{

root.leftpoiter=new BinaryTree(data);

}

else

{

insertTree(root.leftpoiter,data);

}

}

}

}

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼你可以浪但我不會等

2021-01-26 05:51

import java.util.list; import java.util.linkedlist; public class bintrees { private int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; private static list nodelist = null; private static class node { node leftchild; node rightchild; int data; node(int newdata) { leftchild = null; rightchild = null; data = newdata; } } // 创建二叉树 public void createbintree() { nodelist = new linkedlist(); // 将数组的值转换为node for (int nodeindex = 0; nodeindex < array.length; nodeindex++) { nodelist.add(new node(array[nodeindex])); } // 对除最后一个父节点按照父节点和孩子节点的数字关系建立二叉树 for (int parentindex = 0; parentindex < array.length / 2 - 1; parentindex++) { nodelist.get(parentindex).leftchild = nodelist.get(parentindex * 2 + 1); nodelist.get(parentindex).rightchild = nodelist.get(parentindex * 2 + 2); } // 最后一个父节点 int lastparentindex = array.length / 2 - 1; // 左孩子 nodelist.get(lastparentindex).leftchild = nodelist.get(lastparentindex * 2 + 1); // 如果为奇数,建立右孩子 if (array.length % 2 == 1) { nodelist.get(lastparentindex).rightchild = nodelist.get(lastparentindex * 2 + 2); } } // 前序遍历 public static void preordertraverse(node node) { if (node == null) { return; } system.out.print(node.data + " "); preordertraverse(node.leftchild); preordertraverse(node.rightchild); } // 中序遍历 public static void inordertraverse(node node) { if (node == null) { return; } inordertraverse(node.leftchild); system.out.print(node.data + " "); inordertraverse(node.rightchild); } // 后序遍历 public static void postordertraverse(node node) { if (node == null) { return; } postordertraverse(node.leftchild); postordertraverse(node.rightchild); system.out.print(node.data + " "); } public static void main(string[] args) { bintrees bintree = new bintrees(); bintree.createbintree(); node root = nodelist.get(0); system.out.println("前序遍历:"); preordertraverse(root); system.out.println(); system.out.println("中序遍历:"); inordertraverse(root); system.out.println(); system.out.println("后序遍历:"); postordertraverse(root); } } 输出结果: 前序遍历: 1 2 4 8 9 5 3 6 7 中序遍历: 8 4 9 2 5 1 6 3 7 后序遍历: 8 9 4 5 2 6 7 3 1

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值