4.1.1 树的实现
Class TreeNode{
Object element
TreeNode firstChild
TreeNode nextSibling
}
先序遍历和后序遍历的伪代码:
先序遍历
private void listAll(int depth){
printName(depth);
if (isdirectory())
for each file c in this directory(for each child)
c.listAll(depth);
}
public void listAll()
{
listAll(0);
}
后序遍历
public int size(){
int totalSize = sizeOfThisFile();
if (isDirectory())
for each file c in this directory (for each child)
totalSize+=c.size();
return totalSize
}
4.2 二叉树
实现class BinaryNode{
Object element;
BinaryNode left;
BinaryNode right;
}
表达式树和三种遍历方式,先序,中序和后序
三种遍历以根结点为遍历的核心,先序即为根结点先遍历,其次左右孩子,剩下类推
构造表达式树使用后缀表达式转变成表达式树,大概思想是:将除操作符外的数值入栈,然后遇到操作符出栈两个数值,然后构造一个二叉树,将树作为栈的一个元素,也作为数值,如此递归成一个构造表达式树。
4.3 查找树----二叉查找树
接口(interface) comparable 详情见http://developer.51cto.com/art/200909/149028.htm,接口是含抽象方法的一种特殊类。