求以孩子兄弟链表为存储结构的树深度

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/MJ_Lee/article/details/96466402

1. 题目:
如题;
2. 代码:

typedef struct CSNode{
	ElemType data;
	struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;

int Height(CSTree bt){
	int hc,hs;
	if(!bt)
		return 0;
	hc=Height(bt->firstchild);				//第一女子树高
	hs=Height(bt->nextsibling);				//兄弟树高
	if(hc+1>hs)
		return hc+1;
	return hs;
}

3. 知识点:

孩子兄弟表示法

展开阅读全文

的链接存储结构 儿子-兄弟 问题

05-24

运行时显示Debug Assertion FailedrnExpression :_CrtIsValidHeapPointer(pUserData)rn这是数据结构书里的大概程序 rn搞不明白怎么回事rnrnrnrn[code=C/C++]rn#include rn#include rnrntemplate rnclass TreeNodern rn rn// friend class Tree; // 树类rnpublic:rn T data;rn TreeNode *firstChild,*nextBrother ;rn// 构造函数rnTreeNode(T value = 0,TreeNode * L= NULL,TreeNode * R = NULL):data(value),firstChild(L),nextBrother(R) rn;rntemplate rnclass Treernrn private:rnTreeNode *root,*current ; // 根结点和当前指针rn//函数成员:7rnvoid PreOrder(TreeNode *p,ostream & out);rnint Find(TreeNode *p,T target);rnvoid DelSubtree(TreeNode *p);rnint FindFather(TreeNode *t,TreeNode *p);rn public :rnTree(TreeNode * r=NULL,TreeNode * c=NULL):root(r),current(c) // 构造函数rn~Tree()PreDelSubtree(root);rnvoid data()cout<data;rnint FirstChild();rnint NextBrother();rnint Find(T target);rnint Father();rnvoid DelSubtree();rnvoid PreDelSubtree(TreeNode *p);rnvoid InsertChild(int value);rnint DeleteChild(int i);rn;rnrntemplate rnint Tree :: FirstChild()rnrn if(current!=NULL&&current->firstChild!=NULL)rn current = current->firstChild ;rn return 1 ; rn current = NULL ;rn return 0 ;rn rntemplate rnint Tree :: NextBrother()rnrn if(current != NULL && current->nextBrother != NULL)rn rn current = current->nextBrother ;rn return 1 ;rn rn current = NULL ;rn return 0 ;rn rn rntemplate rnint Tree::Father ( ) rnrn // 在树中寻找当前结点的父结点,使之成为当前结点rn TreeNode *p = current, *t;rn if ( current == NULL || current == root )rn current = NULL; return 0; rn t = root; rn return FindFather ( t, p ); rn rntemplate rnint Tree :: FindFather(TreeNode *t,TreeNode *p)rn TreeNode *q = t->firstChild ;rn while(q != NULL && q != p)rn if(FindFather(q,p)==1) return 1;rn q = q->nextBrother ; rn if(q == p)rn current = t ; return 1 ; rn else return 0 ;rn rnrntemplate rnint Tree::Find(TreeNode * p,T target)rnrn if(t->data = = target)rn current = t ; return 1 ; rn elsern rn TreeNode *p = t->firstChild ;rn while(p != NULL && Find(p,target)!=1)rn p = p->nextBrother ; rn if (p!=NULL ) return 1;rn rn return 0 ;rn rnrntemplate rnint Tree::Find(T target)rnrn if(IsEmpty())return 0;rn return Find(root,target);rnrnrntemplate rnvoid Tree ::DelSubtree()rnrn if (current= = root)rn root=NULL;rn TreeNode *q=current;rn current=NULL;rn PreDelSubtree(q);rnrntemplaternvoid Tree :: PreDelSubtree(TreeNode *p)rnrn int i=FindFather(root,p);rn if (i)rn rn if(current->firstChild==p)rn rn current->firstChild=p->nextBrother;rn rn else rn rn TreeNode * q=current->firstChild;rn while (q->nextBrother!=p)rn q=q->nextBrother;rn q->nextBrother=p->nextBrother;rn rn rn DelSubtree(p); rnrnrntemplaternvoid Tree::DelSubtree(TreeNode *p)rnrn if(p != NULL)rn rn TreeNode *q = p->firstChild,*next ;rn while(q != NULL)rn rn next = q->nextBrother ; rn DelSubtree(q); rn q = next ; rn rn delete p ;rn rnrnrntemplate rnvoid Tree::InsertChild(int value)rnrn TreeNode * n=new TreeNode(value);rn TreeNode * p=current->firstChild;rn TreeNode * q=NULL;rn int a;rn cout<<"插入当前节点作为它的第"<nextBrother;rn if(i==a-1)rn rn if(q==NULL)//插入作为第一个节点rn rn n->nextBrother=p;rn current->firstChild=n;rn rn elsern rn n->nextBrother=p;rn q->nextBrother=n;rn rn rn elsern rn cout<<"不能创建该节点!"<rnint Tree::DeleteChild(int i)rnrn TreeNode * q=NULL;rn TreeNode * p=current->firstChild;rn for(int a=1;p!=NULL,anextBrother;rn if(i==a)rn rn if(q==NULL)//删除第一个节点rn rn current->firstChild=p->nextBrother;rn DelSubtree(p);rn rn elsern rn q->nextBrother=p->nextBorther;rn DelSubtree(p);rn rn rn elsern rn cout<<"不能删除该节点!"< g(7);rn TreeNode f(6,NULL,&g);rn TreeNode e(5);rn TreeNode d(4,NULL,&e);rn TreeNode c(3,&f);rn TreeNode b(2,&d,&c);rn TreeNode a(1,&b);*/rn TreeNode c(3);rn TreeNode b(2,NULL,&c);rn TreeNode a(1,&b,NULL);rn Tree t(&a,&a);rn //cout< 论坛

没有更多推荐了,返回首页