先创建一个结点类
package Igorithm.day7_NiXu.main;
public class Tree {
private String weight;
private int index;//节点下标
private Tree leftChild;
private Tree rightChild;
private Tree parent;
public Tree() {
}
public Tree(int index, String data){
this.index = index;
this.weight = data;
this.leftChild = null;
this.rightChild = null;
}
public Tree(String weight, int index, Tree leftChild, Tree rightChild, Tree parent) {
this.weight = weight;
this.index = index;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.parent = parent;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Tree getLeftChild() {
return leftChild;
}
public void setLeftChild(Tree leftChild) {
this.leftChild = leftChild;
}
public Tree getRightChild() {
return rightChild;
}
public void setRightChild(Tree rightChild) {
this.rightChild = rightChild;
}
public Tree getParent() {
return parent;
}
public void setParent(Tree parent) {
this.parent = parent;
}
}
在写二叉树:注我写的有缺陷,如果是单节点会默认作为左子树
package Igorithm.day7_NiXu.main;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class BinaryTrees {
private Tree root = new Tree();
private List<Tree> list = new ArrayList<Tree>();
public BinaryTrees() {
root.setIndex(0);
list.add(root);
}
public BinaryTrees(Tree root, List<Tree> list) {
this.root = root;
this.list = list;
}
/**
* 总体思路:从头结点开始遍历
* 当结点权值第一次为 0 时,创建一个新结点作为左子树
* 当结点权值第一次不为 0 时创建一个结点作为左叶子结点
* 当结点权值第二次不为 0 时创建一个结点作为右叶子结点
* 当结点权值超过两次不为 0 时,index递减 ,并且为上一个结点创建一个结点作为右叶子结点
* 当结点返回到了根节点时,要注意index的值,因为此时我要为期创建右节点
* 且会用到list集合中的最后一个,index = list.size-1而不是index++(第二个)
* 我们其实大多数都是在对list集合中最后一个元素做出操作
* 只有在生成右子树时才会找list前面的元素
* 还有,当结点权值再次为 0 时, 取最后此时index对应下的list集合元素为其建立右子树
* 。
* @param ls
* @return 在list集合中第一个元素就代表这我们要生成的二叉树
*/
public Tree createBinaryTree(List ls) {
boolean flag =false;
int index = 0;//记录最近插入的非叶子结点在list中的位置
for(int i = 1,k=0; i < ls.size();i++) {
String treeNum = (String) ls.get(i);
if (flag && k==0) {
index = list.size()-1;
}
Tree remove = (Tree) list.remove(index);
Tree tree = new Tree();
tree.setParent(remove);
if ("0".equals(treeNum)) {
k=0;
tree.setIndex(index+1);
if (!flag) {
remove.setLeftChild(tree);
}else {
remove.setRightChild(tree);
}
list.add(index,remove);
index++;
}
else if (k==0) {
tree.setWeight(treeNum);
remove.setLeftChild(tree);
list.add(index,remove);
k++;
}else {
tree.setWeight(treeNum);
remove.setRightChild(tree);
list.add(index,remove);
index--;
flag =true;
k++;
}
list.add(tree);
}
return list.get(0);
}
public static void main(String[] args) {
BinaryTrees binaryTrees = new BinaryTrees();
List<String> list = new ArrayList<>();
String str[] = new String[] {"0","0","0","2","1","5","0","3","0","6","8"};
Collections.addAll(list, str);
Tree createBinaryTree = binaryTrees.createBinaryTree(list);
show1(createBinaryTree);
System.out.println("______________");
show2(createBinaryTree);
System.out.println("______________");
show3(createBinaryTree);
}
/**
* 递归:先序遍历
* @param tree
*/
public static void show1(Tree tree) {
if (tree!=null) {
if (tree.getWeight()!=null) {
System.out.println(tree.getWeight());
}
show1(tree.getLeftChild());
show1(tree.getRightChild());
}
}
/**
* 递归:中序遍历
* @param tree
*/
public static void show2(Tree tree) {
if (tree!=null) {
show2(tree.getLeftChild());
if (tree.getWeight()!=null) {
System.out.println(tree.getWeight());
}
show2(tree.getRightChild());
}
}
/**
* 递归:后序遍历
* @param tree
*/
public static void show3(Tree tree) {
if (tree!=null) {
show3(tree.getLeftChild());
show3(tree.getRightChild());
if (tree.getWeight()!=null) {
System.out.println(tree.getWeight());
}
}
}
}