Java二叉树非递归实现前序遍历

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
					+ "]";
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值