import java.util.ArrayList;
import java.util.List;
/**
* 2018-01-22
* @author Youjc
*
*/
public class math_practice {
public math_practice() {
// TODO Auto-generated constructor stub
int[] value = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17 };
int valuesize = value.length;
List<Binarytree> treeindex = new ArrayList<Binarytree>();
for (int c = 0; c < valuesize; c++) {
treeindex.add(new Binarytree(c + 1));
}
for (int c = 0; c < valuesize / 2; c++) {
if (treeindex.get(c).getSelfindex() * 2 <= valuesize) {
treeindex.get(c).setLeftindex(treeindex.get(c).getSelfindex() * 2);
}
if (treeindex.get(c).getSelfindex() * 2 + 1 <= valuesize) {
treeindex.get(c).setRightindex(treeindex.get(c).getSelfindex() * 2 + 1);
}
}
System.out.println("treeindex=" + treeindex);
List<Integer> qianxuindexlist = new ArrayList<Integer>();
for (int c = 0; c < treeindex.size(); c++) {
Binarytree oneself = treeindex.get(c);
while (oneself.getLeftindex() != null) {
if (!qianxuindexlist.contains(oneself.getSelfindex())) {
qianxuindexlist.add(oneself.getSelfindex());
}
if (!qianxuindexlist.contains(oneself.getLeftindex())) {
qianxuindexlist.add(oneself.getLeftindex());
}
oneself = treeindex.get(oneself.getLeftindex() - 1);
}
// 最后一个左侧的值
if (oneself.getLeftindex() == null) {
// 判断父类是否含有右侧
while (!qianxuindexlist.contains(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex())
&& (oneself.getSelfindex() / 2 - 1) != 0) {
if (treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() != null) {
qianxuindexlist.add(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex());
if (treeindex.get(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() - 1)
.getLeftindex() != null) {
if (!qianxuindexlist.contains(
treeindex.get(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() - 1)
.getLeftindex())) {
qianxuindexlist.add(
treeindex.get(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() - 1)
.getLeftindex());
}
}
if (treeindex.get(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() - 1)
.getRightindex() != null) {
if (!qianxuindexlist.contains(
treeindex.get(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() - 1)
.getRightindex())) {
qianxuindexlist.add(
treeindex.get(treeindex.get(oneself.getSelfindex() / 2 - 1).getRightindex() - 1)
.getRightindex());
}
}
}
oneself = treeindex.get(oneself.getSelfindex() / 2 - 1);
}
}
}
System.out.println(qianxuindexlist);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new math_practice();
}
class Binarytree {
Integer selfindex;
Integer leftindex;
Integer rightindex;
public Binarytree(Integer selfindex) {
super();
this.selfindex = selfindex;
}
public Integer getSelfindex() {
return selfindex;
}
public void setSelfindex(Integer selfindex) {
this.selfindex = selfindex;
}
public Integer getLeftindex() {
return leftindex;
}
public void setLeftindex(Integer leftindex) {
this.leftindex = leftindex;
}
public Integer getRightindex() {
return rightindex;
}
public void setRightindex(Integer rightindex) {
this.rightindex = rightindex;
}
@Override
public String toString() {
return "Binarytree [selfindex=" + selfindex + ", leftindex=" + leftindex + ", rightindex=" + rightindex
+ "]";
}
}
}
Java二叉树非递归实现前序遍历
最新推荐文章于 2022-04-14 17:44:33 发布