节点拥有的子树的数目成为节点的度,各节点的最大度是树的度,二叉树的度是2
树中节点的最大层次称为树的高度或深度,根节点的高度或深度为1
一个节点总数为n的树,有n-1条边
满二叉树(full binary tree)
美国以及国际上所定义的满二叉树和国内的定义不同,美国NIST给出的定义为:A binary tree in which each node has exactly zero or two children. In other words, every node is either a leaf or has two children. For efficiency, any Huffman coding is a full binary tree.满二叉树的任意节点,要么度为0,要么度为2.换个说法即要么为叶子结点,要么同时具有左右孩子。霍夫曼树是符合这种定义的,满足国际上定义的满二叉树,但是不满足国内的定义.
非空满二叉树的叶节点的数目比分支节点数大1
完全二叉树(Complete Binary Tree)
若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。
前序、中序、后序遍历是基于根节点来说的,
前序遍历:根节点,左子树,右子树
中序遍历:左子树,根节点,右子树
后序遍历:左子树,右子树,根节点
节点定义
public class treeNode {
private int value;
private treeNode left;
private treeNode right;
public treeNode(int value, treeNode left, treeNode right) {
this.value = value;
this.left = left;
this.right = right;
}
public treeNode() {
};
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public treeNode getLeft() {
return left;
}
public void setLeft(treeNode left) {
this.left = left;
}
public treeNode getRight() {
return right;
}
public void setRight(treeNode right) {
this.right = right;
}
public boolean isleaf() {
return left == null && right == null;
}
}
树的遍历
public class Tree {
public static void preorder(treeNode root){
if (root==null) return;
System.out.print(root.getValue()+" ");
preorder(root.getLeft());
preorder(root.getRight());
}
public static void midorder(treeNode root){
if (root==null) return;
midorder(root.getLeft());
System.out.print(root.getValue()+" ");
midorder(root.getRight());
}
public static void afterorder(treeNode root){
if (root==null) return;
afterorder(root.getLeft());
afterorder(root.getRight());
System.out.print(root.getValue()+" ");
}
public static int count(treeNode root){
if (root==null) {return 0;}
return 1+count(root.getLeft())+count(root.getRight());
}
public static void main(String[]args){
treeNode root=new treeNode(10,null,null);
treeNode node1=new treeNode(8,null,null);
treeNode node2=new treeNode(7,null,null);
treeNode node3=new treeNode(1,null,null);
treeNode node4=new treeNode(6,null,null);
treeNode node5=new treeNode(4,null,null);
treeNode node6=new treeNode(5,null,null);
root.setLeft(node1);
root.setRight(node2);
node1.setLeft(node3);
node1.setRight(node4);
node2.setLeft(node5);
node2.setRight(node6);
preorder(root);
System.out.println();
midorder(root);
System.out.println();
afterorder(root);
System.out.println();
System.out.println(count(root));
}
}
参考:http://baike.baidu.com/link?url=hOiIuSHNNFntpOK_Nck9Sj5PW7ucmAvbBD6rIbdPA_AngZGdYpFZhX7IAHh-VUs6