写写 树的代码

#include <iostream>
#define MaxSize 100
using namespace std;
#define ElemType char

typedef struct tnode
{
    ElemType data;
    struct tnode* lchild,*rchild;
}BTNode;

void CreateBTree(BTNode* &bt,char *str)
{
    BTNode *st[MaxSize],*p = NULL;
    int top = -1,k,j=0;
    char ch;
    bt = NULL;
    ch = str[j];
    while(ch!='\0')
    {
        switch(ch)
        {
		case '(':top++;st[top] = p; k = 1;
            break;
		case ')':top--;break;
		case ',':k = 2; break;
		default:
            p = (BTNode*)malloc(sizeof(BTNode));
            p->data = ch;
			//cout<<"数据: "<<ch<<endl;
            p->lchild = NULL;
            p->rchild = NULL;
            if(bt == NULL)
            {
                bt = p;
            }
            else
            {
                switch(k)
                {
				case 1:st[top]->lchild = p;
                    break;
				case 2:
                    st[top]->rchild = p;
                    break;
                }
               
            }

        }
		j++;
		ch = str[j];
    }
}

void DispBtree(BTNode *bt)
{
    if(bt!=NULL)
    {
        cout<<bt->data;
        if(bt->lchild!=NULL||bt->rchild!=NULL)
        {
            cout<<"(";
            DispBtree(bt->lchild);
            if(bt->rchild!=NULL)
            {
                cout<<","; //左子树访问完,就要访问右子树
            }
            DispBtree(bt->rchild);
            printf(")"); //右子树访问完 -->递归 与分治是有着很密切的联系
        }
    }
}

//树的高度
int BTHeat(BTNode *bt)
{
	int leafn,rightn;
	if(bt == NULL)
	{
		return 0; //递归出口
	}
	else
	{
		leafn = BTHeat(bt->lchild);//--》递归函数
		rightn = BTHeat(bt->rchild);
		return leafn>rightn?(leafn+1):(rightn+1);//因为左子树 右子树 根节点那层必须加起来
		//所以比较后都进行+1
	}
}
//树的结点数
int BTNodeCount(BTNode *bt)
{
	static int x; //自己定义一个静态变量 和树上有点不一样 只要递归思想一想就可以-->编译器默认0
	if(bt == NULL)//递归从整体出发 --》 比喻:树的结点总数= 左子树 + 右子树 +1 ; 
	{             //课本上书就是按照这个思想
		return 0; //空子树为0结点 --》这个也是树的递归的出口--》也必须防止空树
		//我这里相当于用中跟遍历一样的 -》这里正好用到整个模型。
		//相对根(每个子树看成一个独立的树,递归借助分治来来玩-》分治又借助递归来玩)
	}
	x++;
	BTNodeCount(bt->lchild);
	BTNodeCount(bt->rchild);
	return x;
}
//树的结点数 书上的代码

int NodeCout(BTNode *bt)
{
	int num1,num2;
	if(bt == NULL)
	{
		return 0;
	}
	else
	{
		num1 = NodeCout(bt->lchild); //求子子树的结点个数
		num2 = NodeCout(bt->rchild); //右子子树的借点个数
		return num1+num2+1;
	}
}
//求树的叶子节点
int BTleafCount(BTNode *bt)
{//代码貌似不是很好符合递归思想 ,完全是借用遍历的
	static int n = 0; 
	if(bt == NULL)
	{
		return 0; //递归出口
	}
	//这里和我上面差不多。只要加一个判断就可以
	if(bt->lchild == NULL && bt->rchild==NULL)
	{
		n++;
	}
	BTleafCount(bt->lchild); //这里遍历所有书 --》貌似不是很好符合递归的思想
	BTleafCount(bt->rchild);
	return n;

}

//树上的思路:
/*	| 0  当 b = NULL 
f(b)= 1 当 b 为子节点
	| f(b->lchild)+f(b->rchild)  其他情况
*/

int LeafCount(BTNode *bt)
{
	int num1, num2;
	if(bt == NULL)
	{
		return 0; //递归出口
	}

	if(bt->lchild== NULL && bt->rchild == NULL)
	{
		return 1; //递归出口
	}
	num1 = LeafCount(bt->lchild); //递归函数
	num2 = LeafCount(bt->rchild); //递归只要
	return num1+num2;
}
int main()
{
	BTNode *bt;
	CreateBTree(bt,"A(B(D,E(G,H)),C(,F(I)))");
	//DispBtree(bt);
	//cout<<"树的深度 "<<BTHeat(bt);
	//cout<<"树的结点个数"<<BTNodeCount(bt);
	//cout<<"树的结点个数"<<NodeCout(bt);
	//cout<<"树的叶子节点"<<BTleafCount(bt);
	//cout<<"树的叶子结点"<<LeafCount(bt);
    return 0;
}

//树主要思想就是递归
//递归确实一门艺术。。。。。。。。。

 

posted @ 2012-04-18 22:16  小鱼儿c  阅读(487)  评论(0编辑  收藏  举报