import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BinTreeNode3{ //三叉链结点类
private BinTreeNode3 lChild,rChild,parent; //左右孩子、双亲结点引用
private int data;
public BinTreeNode3(){ //无参构造函数
lChild=null;
rChild=null;
}
public BinTreeNode3(int item){ //带参构造函数
lChild=null;
rChild=null;
data=item;
}
public BinTreeNode3(int item,BinTreeNode3 left,BinTreeNode3 right){
//构造函数
data=item;
lChild=left;
rChild=right;
}
public void setParent(BinTreeNode3 parent){ //设置双亲指针
this.parent=parent;
}
public BinTreeNode3 getParent(){ //返回双亲指针
return parent;
}
public void setLeft(BinTreeNode3 left){ //设置左孩子
lChild=left;
}
public BinTreeNode3 getLeft(){ //返回左孩子指针
return lChild;
}
public void setRight(BinTreeNode3 right){
//设置右孩子
rChild=right;
}
public BinTreeNode3 getRight(){
//返回右孩子指针
return rChild;
}
public void setData(int data){
//设置数据域
this.data=data;
}
public int getData(){
//设置数据域
return data;
}
public void insertBST(BinTreeNode3 Ra, BinTreeNode3 Rroot){
//将Ra插入到以Rroot为根的二叉排序树中
if(Ra.getData()<Rroot.getData()){
//插入到左子树
if(Rroot.getLeft()==null){
//左孩子空,Ra作为左孩子
Rroot.setLeft(Ra);
Ra.setParent(Rroot);
//同时设置双亲指针
}
else
insertBST(Ra,Rroot.getLeft());
//左孩子非空,Ra插入左子树
}
else{ //插入到右子树
if(Rroot.getRight()==null){ //右孩子空,Ra作为右孩子
Rroot.setRight(Ra);
Ra.setParent(Rroot); //同时设置双亲指针
}
else
insertBST(Ra,Rroot.getRight()); //右孩子非空,Ra插入右子树
}
return ;
}
public BinSearchTree createSortTree(){ //创建二叉排序树
BinSearchTree tree = new BinSearchTree(); //创建二叉排序树对象
try {
int num;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
num = Integer.parseInt(in.readLine()); //输入整数
BinTreeNode3 Rroot, Ri;
Rroot = new BinTreeNode3(num); //创建根结点
num = Integer.parseInt(in.readLine()); //输入整数
while (num != -1) { //当输入的字符是-1时结束创建
Ri = new BinTreeNode3(num); //创建新结点
insertBST(Ri, Rroot); //插入二叉排序树
num = Integer.parseInt(in.readLine()); //输入整数
}
tree.setRoot(Rroot); //设置对象根结点
} catch (Exception e) {
// TODO: handle exception
}
return tree;
}
}
public class BinSearchTree{
private BinTreeNode3 root; //根结点
public BinSearchTree(){ //构造函数
root=null;
}
public BinSearchTree(int item, BinSearchTree left, BinSearchTree right) {
BinTreeNode3 l=null,r=null;
if(left==null)
l=null;
else
l=left.root;
if(right==null)
r=null;
else
详细解决方案
这个main函数的测试类要如何写才好呢,总是出错!如何实现二叉排序树的插入与删除呢?遍历已经实现了
热度:63 发布时间:2016-04-22 21:27:06.0
相关解决方案
- 用eclipse打包jar后运行,弹出异常could not find main class
- main()函数个数,该如何解决
- 请教哪里有 MDF (Main Distribution Frame) 的接线教程
- main()方法引用成员变量的有关问题
- main(int, char* argv[]) 跟 main(int, wchar* argv[])
- 在网页下实现播放图片的功能。但是这个功能的间隔的时间在哪里设置.main.js里面么
- 用了<script>alert('请先登录');window.open('Main.aspx')遨游拦截 用了Redirect没有必要的提示怎么处理啊
- webllogic linux装完起步报错Exception in thread "main" java.lang.NoClassDefFoundError
- 错误信息:could not find the main class programe will exit
- Unable to compile 注意:sun.tools.javac.Main 已过时???
- exception in thread "main" java.lang.NoSuchMethodError:main
- [求助]main()
- 错误 4.txt 7: 表达式语法错在 main 函数中
- 关于int main(int argc, char *argv[])的问题
- main(int argc,char *argv[]) 本身的参数在哪里
- void main()和int main(void)有什么不同
- iframe显示的页面改变了以后,main.aspx更新的有关问题
- 小弟我自建了一个uddi中心,myeclips 6.5 uddi main find web service出错
- 浏览器滚动条上拉时 悬浮块 根据 main 里面的内容 改变
- Eclipse web项目运行main的class出现could not find the main class异常
- JAVA1.8下 Applet出现异常:Missing Permissions manifest attribute in main jar
- "main " org.hibernate.StaleStateException:
- jsp 怎么调用java类里的public static void main(String argv[]) {中的main方法
- public static void main(String[] args)解决办法
- public static void main(String[] args);这句话是否每个java程序都必须有
- main()方法执行有关问题?
- could not find the main class.Program will exit,该如何处理
- jsp 异常(sun.tools.javac.Main has been deprecated)
- could not find the main class :xxx解决方案
- 编译运行Spark时遇到的“Could not find the main class”的有关问题