数据结构和算法2之递归

树和图

树非线性的是n个节点组成的集合

根节点    一个节点拥有的子树的数量称为节点的度

一个节点的(level)反映了这个几点处于树种的位置

 约定根节点的层数为0 其他节点的层数都是其父节点的层数加1 

树中链接各节点之间的边线称为边 同一个节点的几个子节点称为兄弟节点

不属于一个父节点在同一层的称为堂兄第节点

度数为m的有序树称为m叉树

二叉树

在I层上,最多有2*2...!个2

完全二叉树在最后一层少叶子

满二叉树2的n次方减-1个

一个满二叉树不一定是完全二叉树

一个完全二叉树一定是一个满二叉树

顺序存储(只适合于满二叉树和完全二叉树)

 

 1 **树的节点
 2  * @author 95319
 3  *
 4  */
 5 public class TreeNode {
 6     private Object nodevalue;
 7     private TreeNode left,right;
 8     public TreeNode() {
 9         this(null,null,null);
10     }
11     public TreeNode(Object nodevalue, TreeNode left, TreeNode right) {
12         
13         this.nodevalue = nodevalue;
14         this.left = left;
15         this.right = right;
16     }
17     public TreeNode(Object o) {
18         this(o,null,null);
19     }
20     public boolean isleaf(){
21         if(this.left==null&&this.right==null){
22             return true;        
23         }
24         return false;
25     }
26     @Override
27     public String toString() {
28         if (this.nodevalue==null) {
29             return null;
30         }
31         String result="(节点:"+nodevalue.toString();
32         if (left!=null) {
33             result +="左孩子:"+left.toString();
34         }
35         if (right!=null) {
36             result +="左孩子:"+right.toString();
37         }
38         result +=")";
39         return result;
40     }
二叉树结点

 

 1 public class BinaryTree {
 2     public TreeNode root;
 3 
 4     public BinaryTree() {
 5         root=null;
 6     }
 7 
 8     public BinaryTree(TreeNode root) {
 9         
10         this.root = root;
11     }
12     public boolean isEmpty(){
13         if (root==null) {
14             return true;
15         }
16         return false;
17     }
18     public String toString() {        
19         return root.toString();
20     }
实现二叉树

二叉树遍历包括先序(左中右) 中序(中左右) 后序(左右中)

 

 图是一种重要的非线性集合

图的存储结构

邻接矩阵1 有权图· 2 无权图 求最短路径最经典算法

图的遍历

深度优先搜素

从某一顶点出发直到不能访问为止从新从那节点重新访问其他没访问过的节点

广度优先搜索(先被访问的顶点的邻接点 先 于后被访问的顶点的邻接点)

先搜索离某一点比较近的节点

posted on 2015-09-10 14:20  Longevity  阅读(213)  评论(0编辑  收藏  举报