先感慨下:虽为通信工程毕业,但课程比较传统,偏向于运营商方向,因为历史上来说本院曾经是‘某某邮电学院’,邮电部直属院校五所之一,所以比较传统。《数据结构与算法》没怎么学过,不想最后大部分同学去了运营商或者邮电规划建设部门,我却做了android开发,基础之薄弱真是令我汗颜。虽然平时开发过程中没什么大的感知,但是我知道,如果不补上这一块,差距将一直存在。
说实话,‘树’的概念,是我毕业一年以后才知道的,囧。当时被华为的面试官鄙视的一塌糊涂。当时不以为意,也没有进行学习,直到最近希望有些改变,渐渐开始学习这些知识。
public class Tree {
/*************************************************/
/*********************** start *******************/
/*************************************************/
public static int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
public Tree() {
}
/**
* 把数组变成二叉树
*/
public static ArrayList<Node> sortF() {
ArrayList<Node> list = new ArrayList<Node>();
for (int i = 0; i < array.length; i++) {
list.add(new Node(array[i]));
}
for (int parentNodeIndex = 0; parentNodeIndex < array.length / 2 - 1; parentNodeIndex++) {
if (list.get(2 * parentNodeIndex + 1) != null) {
list.get(parentNodeIndex).leftChildNode = list.get(2 * parentNodeIndex + 1);
}
if (list.get(2 * parentNodeIndex + 2) != null) {
list.get(parentNodeIndex).rightChildNode = list.get(2 * parentNodeIndex + 2);
}
}
//the last parentNode
int lastParentNodeIndex = array.length / 2 - 1;
if (list.get(lastParentNodeIndex * 2 + 1) != null) {
list.get(lastParentNodeIndex).leftChildNode = list.get(lastParentNodeIndex * 2 + 1);
}
if (array.length % 2 != 0) {
if (list.get(lastParentNodeIndex * 2 + 2) != null) {
list.get(lastParentNodeIndex).rightChildNode = list.get(lastParentNodeIndex * 2 + 2);
}
}
return list;
}
public static void printNodeValue(Node node) {
System.out.println("printNodeValue--node.selfValue=" + node.selfValue);
}
public static void test() {
ArrayList<Node> list = sortF();
System.out.println("printNodeValue=====left=====");
leftTraversal(list.get(0));
System.out.println("printNodeValue======mid====");
midTraversal(list.get(0));
System.out.println("printNodeValue======right====");
rightTraversal(list.get(0));
}
/**
* 递归实现前序遍历
*/
public static void leftTraversal(Node p) {
if (p != null) {
printNodeValue(p);
leftTraversal(p.getLeftChildNode());
leftTraversal(p.getRightChildNode());
}
}
public static void midTraversal(Node p) {
if (p != null) {
midTraversal(p.getLeftChildNode());
printNodeValue(p);
midTraversal(p.getRightChildNode());
}
}
public static void rightTraversal(Node p) {
if (p != null) {
rightTraversal(p.getLeftChildNode());
rightTraversal(p.getRightChildNode());
printNodeValue(p);
}
}
public static int[] initData() {
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = i;
}
return a;
}
public static class Node {
int selfValue;
Node leftChildNode;
Node rightChildNode;
public Node(int self) {
this.selfValue = self;
}
public Node getRightChildNode() {
return rightChildNode;
}
public void setRightChildNode(Node rightChildNode) {
this.rightChildNode = rightChildNode;
}
public int getSelfValue() {
return selfValue;
}
public void setSelfValue(int selfValue) {
this.selfValue = selfValue;
}
public Node getLeftChildNode() {
return leftChildNode;
}
public void setLeftChildNode(Node leftChildNode) {
this.leftChildNode = leftChildNode;
}
}
}
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue=====left=====
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=0
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=1
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=3
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=7
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=8
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=4
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=9
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=2
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=5
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=6
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue======mid====
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=7
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=3
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=8
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=1
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=9
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=4
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=0
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=5
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=2
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=6
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue======right====
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=7
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=8
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=3
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=9
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=4
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=1
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=5
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=6
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=2
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=0