C++1.编写算法, 统计二叉树中度为2的结点的个数。2.编写算法,判断二叉树中e是否是e1和e2的祖先 。3.已知二叉树中序遍历序列和后序遍历序列,编写算法建立二叉树。4.编写算法求出

题目要求:1.编写算法,  统计二叉树中度为2的结点的个数。

2.编写算法,判断二叉树中e是否是e1和e2的祖先 。

3.已知二叉树中序遍历序列和后序遍历序列,编写算法建立二叉树。

4.编写算法求出二叉树中第k层结点的个数。(深度遍历和层次遍历两种方法实现)

 

实现代码:

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

typedef char ElemType;

struct node
{
	ElemType data;			
	struct node* lchild, * rchild;
};

class BTNode
{
	node* root;
public:
	BTNode(){ root = NULL; };
	BTNode(char* str);
	BTNode(char* post, char* in, int n);
	~BTNode();
	friend ostream& operator<<(ostream& o, BTNode &b);
	int count_two();
	int count_two(node* &b);
	node* FindNode(ElemType x);
	node* FindNode(node*& b, ElemType x);
	bool ancestor(ElemType e, ElemType e1, ElemType e2);
	node* CreateBT2(char* post, char* in, int n);
	int count_level_queue(int level);
	int count_level_deep(int level);
};

struct SqQueue
{
	node* data[MaxSize];				
	int front, rear;						
};		

void InitQueue(SqQueue*& q)				
	q = (SqQueue*)malloc(sizeof(SqQueue));
	q->front = q->rear = 0;
}

void DestroyQueue(SqQueue*& q)			
{
	free(q);
}

bool QueueEmpty(SqQueue* q)				
{
	return(q->front == q->rear);
}

bool enQueue(SqQueue*& q, node* e)		
{
	if ((q->rear + 1) % MaxSize == q->front)	
		return false;
	q->rear = (q->rear + 1) % MaxSize;
	q->data[q->rear] = e;
	return true;
}

int Count(SqQueue*& q)					 
{
	return (q->rear - q->front + MaxSize) % MaxSize;
}

bool deQueue(SqQueue*& q, node*& e)	
{
	if (q->front == q->rear)				
		return false;
	q->front = (q->front + 1) % MaxSize;
	e = q->data[q->front];
	return true;
}
int LevelOrder1(node* b, int level)		 
{
	node* p;
	SqQueue* qu;
	InitQueue(qu);					
	int curl = 1, cnt;					 
	enQueue(qu, b);					
	while (curl<level)			
	{
		cnt = Count(qu);	
		for (int i = 0; i < cnt; i++)		
		{
			deQueue(qu, p);				
			if (p->lchild != NULL)		
				enQueue(qu, p->lchild);
			if (p->rchild != NULL)		
				enQueue(qu, p->rchild);
		}
		curl++;
	}
	cnt = Count(qu);
	return cnt;
}

BTNode::BTNode(char* str)
{
	node* St[MaxSize], * p = NULL;
	int top = -1, k, j = 0;
	char ch;
	root = 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 = (node*)malloc(sizeof(node));
			p->data = ch; p->lchild = p->rchild = NULL;
			if (root == NULL)                    	 	
				root = p;
			else  								
			{
				switch (k)
				{
				case 1:St[top]->lchild = p; break;
				case 2:St[top]->rchild = p; break;
				}
			}
		}
		j++;
		ch = str[j];
	}
}

void DestroyBTree(node*& b)
{
	if (b != NULL)
	{
		DestroyBTree(b->lchild);
		DestroyBTree(b->rchild);
		free(b);
	}
}

BTNode::~BTNode()
{
	DestroyBTree(root);
}

int BTNode::count_two(node* &b)
{
	if (b == NULL) 
		return 0;
	else if ((b->lchild != NULL) && (b->rchild != NULL)) 
		return (1 + count_two(b->lchild) + count_two(b->rchild));
	else
		return (count_two(b->lchild) + count_two(b->rchild));
}

int BTNode::count_two()
{
	if (root == NULL) 
		return 0;
	else if ((root->lchild != NULL) && (root->rchild != NULL)) 
		return (1 + count_two(root->lchild) + count_two(root->rchild));
	else
		return (count_two(root->lchild) + count_two(root->rchild));
}

ostream& operator<<(ostream& o, node *&b)
{
	if (b != NULL)
	{
		o<<b->data;
		if (b->lchild != NULL || b->rchild != NULL)
		{
			o<<"(";						
			operator<<(o, b->lchild);				
			if (b->rchild != NULL) 
				o<<",";	
			operator<<(o, b->rchild);				
			o<<")";						
		}
	}
	return o;
}

ostream& operator<<(ostream& o, BTNode &b)
{
	operator<<(o, b.root);
	return o;
}

node* BTNode::FindNode(node* &b, ElemType x)
{
	node* p;
	if (b == NULL)
		return NULL;
	else if (b->data == x)
		return b;
	else
	{
		p = FindNode(b->lchild, x);
		if (p != NULL)
			return p;
		else
			return FindNode(b->rchild, x);
	}
}

node* BTNode::FindNode(ElemType x)
{
	return FindNode(root,x);
}

bool BTNode::ancestor(ElemType e, ElemType e1, ElemType e2)
{
	node* pe, * pe1, * pe2;
	pe = FindNode(e);
	pe1= FindNode(pe,e1);
	pe2 = FindNode(pe, e2);
	if (pe1 != NULL && pe2 != NULL)
		return true;
	return false;
}

int BTNode::count_level_queue(int level)
{
	return LevelOrder1(root, level);
}

int getLevel(node *b, int k) 
{
	if (b == NULL || k < 1) {
		return 0;
	}
	if (k == 1) {
		return 1;
	}
	return getLevel(b->lchild, k - 1) + getLevel(b->rchild, k - 1);
}

int BTNode::count_level_deep(int level)
{
	return getLevel(root, level);
}

BTNode::BTNode(char* post, char* in, int n)
{
	root = CreateBT2(post, in, n);
}

node* BTNode::CreateBT2(char* post, char* in, int n)
{
	node* s;
	char r, * p;
	int k;
	if (n <= 0) 
		return NULL;
	r = *(post + n - 1);							
	s = (node*)malloc(sizeof(node));		
	s->data = r;
	for (p = in; p < in + n; p++)				
		if (*p == r)
			break;
	k = p - in;									
	s->lchild = CreateBT2(post, in, k);			
	s->rchild = CreateBT2(post + k, p + 1, n - k - 1);	
	return s;
}

int main()
{
	char c[20] = "A(B(D(,G)),C(E,F))";
	BTNode b(c);
	cout << "b=" << b << endl;
	cout <<"度为2的结点数量为:"<< b.count_two() << endl;
	cout << "e=C,e1=B,e2=F" << endl;
	cout << "e";
	if (!b.ancestor('C', 'B', 'F'))
		cout << "否";
	cout << "是e1,e2的祖先" << endl;
	char post[20] = "GDBEFCA", in[20] = "DGBAECF";
	int i;
	BTNode B(post, in, 7);
	cout << "已知中序遍历序列:";
	for (i = 0; in[i] != 0; i++)
		cout << in[i];
	cout << " ,后序遍历序列:";
	for (i = 0; post[i] != 0; i++)
		cout << post[i];
	cout  << "创建B=" << B << endl;
	cout << "(深度遍历)第二层的结点个数为:" << b.count_level_deep(2) << endl;
	cout << "(层次遍历)第三层的结点个数为:" << b.count_level_queue(3) << endl;
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值