北邮1467
来源:互联网 发布:好网络克隆的软件 编辑:程序博客网 时间:2024/05/21 10:10
题目描述:
二叉排序树,也称为二叉查找树。可以是一颗空树,也可以是一颗具有如下特性的非空二叉树:
1. 若左子树非空,则左子树上所有节点关键字值均不大于根节点的关键字值;
2. 若右子树非空,则右子树上所有节点关键字值均不小于根节点的关键字值;
3. 左、右子树本身也是一颗二叉排序树。
现在给你N个关键字值各不相同的节点,要求你按顺序插入一个初始为空树的二叉排序树中,每次插入后成功后,求相应的父亲节点的关键字值,如果没有父亲节点,则输出-1。
- 输入:
输入包含多组测试数据,每组测试数据两行。
第一行,一个数字N(N<=100),表示待插入的节点数。
第二行,N个互不相同的正整数,表示要顺序插入节点的关键字值,这些值不超过10^8。
- 输出:
输出共N行,每次插入节点后,该节点对应的父亲节点的关键字值。
- 样例输入:
52 5 1 3 4
- 样例输出:
-12253
package com.zhanghaipeng.jobdu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class JD1467 {
public static void main(String[] args) throws IOException{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
while(st.nextToken() != StreamTokenizer.TT_EOF){
int n = (int)st.nval;
BinSortTree biTree = null;
for(int i =0; i < n; i++){
st.nextToken();
biTree = createTree(biTree,(int)st.nval);
}
}
}
public static BinSortTree createTree(BinSortTree biTree, int head) {
BinSortTree node = new BinSortTree(null, null, head);
if (biTree == null) {
biTree = node;
System.out.println(-1);
} else {
BinSortTree point = node;
while (point != null) {
if (head > point.getValue()) {
if (point.getRchild() == null) {
System.out.println(point.getValue());
point.setRchild(node);
break;
} else {
point = point.getRchild();
}
} else {
if (head < point.getValue()) {
if (point.getLchild() == null) {
System.out.println(point.getValue());
point.setLchild(node);
break;
} else {
point = point.getLchild();
}
}
}
}
}
return biTree;
}
private static class BinSortTree{
/**
*
*/
private BinSortTree lchild;
private BinSortTree rchild;
private int value;
public BinSortTree getLchild() {
return lchild;
}
public void setLchild(BinSortTree lchild) {
this.lchild = lchild;
}
public BinSortTree getRchild() {
return rchild;
}
public void setRchild(BinSortTree rchild) {
this.rchild = rchild;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinSortTree(BinSortTree lchild,BinSortTree rchild,int value){
super();
this.lchild = lchild;
this.rchild = rchild;
this.value = value;
}
}
}
- 北邮1467
- 九度题目1467 二叉排序树 北邮2012机试
- 北邮1473
- 北邮1474
- Uva-1467-Installations
- Nsu-1467-VIJOS-P1164
- 题目1467:二叉排序树
- 题目1467:二叉排序树
- 九度代码1467
- 题目1467:二叉排序树
- Clever Y [Bzoj 1467]
- UVA 1467 Installations(贪心)
- 1467: Pku3243 clever Y
- 题目1467:二叉排序树
- 题目1467:二叉排序树
- BZOJ 1467 1467: Pku3243 clever Y (exBSGS)
- FOJ 1467 Pick Up Points
- UVA 1467 - Installations(贪心)
- hibernate.cfg.xml文件的解说
- 原来php也有debug调试器的。
- 文件上传利器SWFUpload使用指南
- 各级领导干部待遇梳理:正部级住房220平方米
- JSON和JSONP 详细介绍,很COOL
- 北邮1467
- 实践中学习vim之按键序列映射
- CyclicBarrier介绍
- 浙江大学2005年机试试题第一题A+B
- Android设置窗体Activity背景透明
- 关于项目中创建控件要注意的一些地方!
- Ubuntu下基于Nginx实现Tomcat集群负载均衡
- jdk环境变量配置
- 需要学习的内容