java(树的内容)算法与数据结构

Python016

java(树的内容)算法与数据结构,第1张

其实有两种方式:

第一种就是递归 就像现在比较老的树形菜单。这种方式应该string类型应该是存不了的。就是自定义一个类型A 里面有一个成员变量 list<A>。 这种结构就是list里面嵌套list,你有多少级就有多少层。

第二种其实要做处理,就是把原数据按一定规则排序放到一个list里面,这里面不会再嵌套list。list排完序就如你的效果图一样。第一个 一级节点 》》其子节点;然后第二个一级节点》》其子节点,etc。 但是这种结构要有存的时候要循环一遍排成上述的顺序,取的时候还需要判断哪个是下一个不同级节点的开始。

js前台展示比较简单,根据父id直接添加就行了,原数据什么都不用做。但是java里这种方式不行。

数组、栈 、队列、链表、树、堆 、图、散列表 。

1:数组是计算机编程语言上,对于“Array”的中文称呼,是用于储存多个相同类型数据的集合。

2:栈是限定仅在表尾进行插入和删除操作的线性表,栈者,存储货物或供旅客住宿的地方,可引申为仓库、中转站,引入到计算机领域里,就是指数据暂时存储的地方,所以才有进栈、出栈的说法。

3:一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。

4:链表,一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

5:哈希表,是根据关键码值而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。

package tree

import java.util.LinkedList

import java.util.List

/**

* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历

*

* 参考资料0:数据结构(C语言版)严蔚敏

*

* 参考资料1:http://zhidao.baidu.com/question/81938912.html

*

* 参考资料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java

*

* @author [email protected] @date: 2011-5-17

*

*/

public class BinTreeTraverse2 {

private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }

private static List<Node>nodeList = null

/**

* 内部类:节点

*

* @author [email protected] @date: 2011-5-17

*

*/

private static class Node {

Node leftChild

Node rightChild

int data

Node(int newData) {

leftChild = null

rightChild = null

data = newData

}

}

public void createBinTree() {

nodeList = new LinkedList<Node>()

// 将一个数组的值依次转换为Node节点

for (int nodeIndex = 0nodeIndex <array.lengthnodeIndex++) {

nodeList.add(new Node(array[nodeIndex]))

}

// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树

for (int parentIndex = 0parentIndex <array.length / 2 - 1parentIndex++) {

// 左孩子

nodeList.get(parentIndex).leftChild = nodeList

.get(parentIndex * 2 + 1)

// 右孩子

nodeList.get(parentIndex).rightChild = nodeList

.get(parentIndex * 2 + 2)

}

// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理

int lastParentIndex = array.length / 2 - 1

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1)

// 右孩子,如果数组的长度为奇数才建立右孩子

if (array.length % 2 == 1) {

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2)

}

}

/**

* 先序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

*遍历的节点

*/

public static void preOrderTraverse(Node node) {

if (node == null)

return

System.out.print(node.data + " ")

preOrderTraverse(node.leftChild)

preOrderTraverse(node.rightChild)

}

/**

* 中序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

*遍历的节点

*/

public static void inOrderTraverse(Node node) {

if (node == null)

return

inOrderTraverse(node.leftChild)

System.out.print(node.data + " ")

inOrderTraverse(node.rightChild)

}

/**

* 后序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

*遍历的节点

*/

public static void postOrderTraverse(Node node) {

if (node == null)

return

postOrderTraverse(node.leftChild)

postOrderTraverse(node.rightChild)

System.out.print(node.data + " ")

}

public static void main(String[] args) {

BinTreeTraverse2 binTree = new BinTreeTraverse2()

binTree.createBinTree()

// nodeList中第0个索引处的值即为根节点

Node root = nodeList.get(0)

System.out.println("先序遍历:")

preOrderTraverse(root)

System.out.println()

System.out.println("中序遍历:")

inOrderTraverse(root)

System.out.println()

System.out.println("后序遍历:")

postOrderTraverse(root)

}

}