设计一个非递归算法 从一棵二叉树中查找出所有节点的最大值并返回

 我来答
百度网友af84c79
2012-10-08 · TA获得超过209个赞
知道小有建树答主
回答量:306
采纳率:0%
帮助的人:199万
展开全部
用广度搜索
创建队列
创建初始MAX值 0
将树跟放入队列
将树根取出队列和MAX对比 比MAX大则替换MAX
然后将取出节点的左右子节点放入队列
如上遍历队列
依次类推
最后得出MAX值
百度网友e3120544d
2012-10-08 · TA获得超过621个赞
知道小有建树答主
回答量:274
采纳率:100%
帮助的人:121万
展开全部
请参考:
// Test6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX 100
#define EMPTY 0xFFFF

struct stack
{
int data[MAX];
int top;
}Stack;

struct Tree
{
char data;
struct Tree *left;
struct Tree *right;
};

struct queue
{
//char data[MAX];
struct Tree *data[MAX];
int front;
int rear;
}Queue;

//栈中top所指向的保留为空
void Initial();
void PushStack(int iData);
int PopStack();

//队列中rear所指向的保留为空
void InsertQueue(struct Tree *iData);
struct Tree * DeleteQueue();

void CreateTree(struct Tree **root);
void PrintTree(struct Tree *r, int order);
void TransLevel(struct Tree *r);

struct Tree * TransLevel_MaxNum(struct Tree *r);

int main(int argc, char* argv[])
{

Initial();
struct Tree *root=NULL;
CreateTree(&root);
printf("\n层次遍历:");
//TransLevel(root);
struct Tree *p =TransLevel_MaxNum(root);
printf("\nMax char is :%c\n",p->data);
return 0;
}

void Initial()
{
int i=0;
struct Tree t={0,NULL,NULL};
for (i=0; i<MAX; i++)
{
Stack.data[i] = 0;
Queue.data[i] = &t;
}
Stack.top = 0;

Queue.front = 0;
Queue.rear = 0;

}

void PushStack(int iData)
{
if (Stack.top>=MAX-1)
{
printf("Stack is full!\n");
return ;
}
Stack.data[Stack.top] = iData;
Stack.top = Stack.top+1;
}
int PopStack()
{
if (Stack.top == 0)
{
printf("Stack is empty!\n");
return EMPTY;
}
int temp=0;
Stack.top = Stack.top-1;
temp = Stack.data[Stack.top];
return temp;

}
void InsertQueue(struct Tree *t)
{
if (Queue.front == (Queue.rear+1)%MAX )
{
printf("\nQueue is full\n");
return ;
}
Queue.data[Queue.rear] = t;
Queue.rear = (Queue.rear+1)%MAX;
}
struct Tree * DeleteQueue()
{
// struct Tree t={0,NULL,NULL};
if (Queue.front == Queue.rear)
{
printf("\nQueue is empty\n");
return NULL;
}
struct Tree *temp=NULL;

temp = Queue.data[Queue.front];
Queue.front = (Queue.front+1)%MAX;
return temp;
}

void CreateTree(struct Tree **root)
{
char ch;
ch = getchar();
if (ch == '#')
{
return ;
}
*root = (struct Tree *)malloc(sizeof(struct Tree));
(*root)->data = ch;
(*root)->left = NULL;
(*root)->right = NULL;
CreateTree(&(*root)->left);
CreateTree(&(*root)->right);
}

void PrintTree(struct Tree *r,int order)
{
if (r == NULL)
{
return ;
}
switch(order)
{
case 0:printf("%c",r->data);
PrintTree(r->left, order);
PrintTree(r->right,order);
break;
case 1:PrintTree(r->left,order);
printf("%c",r->data);
PrintTree(r->right,order);
break;
case 2:PrintTree(r->left,order);
PrintTree(r->right,order);
printf("%c",r->data);
break;
}
}

void TransLevel(struct Tree *r)
{
if (r == NULL)
{
return ;
}
InsertQueue(r);
struct Tree *pTree=NULL;
while ( (pTree=DeleteQueue()) != NULL )
{
printf("%c",pTree->data);
if (pTree->left != NULL)
{
InsertQueue(pTree->left);
}
if (pTree->right != NULL)
{
InsertQueue(pTree->right);
}
}
}

struct Tree * TransLevel_MaxNum(struct Tree *r)
{
if (r == NULL)
{
return NULL;
}
struct Tree *temp=r;
int maxNum = r->data;
InsertQueue(r);
struct Tree *pTree=NULL;
while ( (pTree=DeleteQueue()) != NULL )
{
printf("%c",pTree->data);
if (pTree->data > maxNum)
{
maxNum = pTree->data;
temp = pTree;
}
if (pTree->left != NULL)
{
InsertQueue(pTree->left);
}
if (pTree->right != NULL)
{
InsertQueue(pTree->right);
}
}
return temp;
}

//其中栈的代码为练习所用,非必要代码
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式