用C实现树的遍历

C语言写树的遍历

//对树进行底层遍历时使用了队列的结构
 
基础类型:
typedef enum {FALSE = 0,TRUE = 1} Bool;
typedef enum {VOERFLOW = -2,UNDERFLOW = -1, ERROR = 0,OK = 1} Status;
 
树的二叉链表结点定义:
typedef struct Node{
	char data;
	struct Node *firstchild,*nextbrother;
}Node,*TreeNode;
 
//实现队列基本操作的函数原型表
void InitQueue(Queue *Q);				//初始化队列
Bool IsEmpty(Queue Q);					//判断队列是否为空,若是则返回TRUE,否则返回FALSE
void EnQueue(Queue *Q,TreeNode p);		//元素入队列
void DeQueue(Queue *Q,TreeNode *p);		//元素出队列
 
//函数代码
Status LevelTraverser(TreeNode root)
{
	/*层序遍历树,树采用孩子-兄弟表示法,root是树根结点的指针*/
	Queue tmpQ;
	TreeNode ptr,brotherptr;
	
	if( !root )
		return ERROR;
	
	InitQueue(&tmpQ);
	EnQueue(tmpQ,root);
	brotherptr = root->nextbrother;
	
	while(brotherptr)
	{
		EnQueue(&tmpQ,brotherptr);		
		brotherptr=brotherptr->nextbrother;
	}
	while(!IsEmpty(tmpQ))
	{
		DeQueue(&tmpQ,&ptr);
		printf("%c\t",ptr->data);
		
		if(!ptr->firstchild) continue;
		EnQueue(&tmpQ,ptr->firstchild);
		brotherptr =ptr->brotherptr->nextbrother;
		
	while(brotherptr)
	{
			EnQueue(&tmpQ,brotherptr);
			brotherptr = brotherptr->nextbrother;
	}
	}
	return OK;
}

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
的层序遍可以使用队列来实现,具体步骤如下: 1. 首先将根节点入队。 2. 当队列非空时,取出队首节点,并将其值输出。 3. 将该节点的左子节点和右子节点(如果存在)入队。 4. 重复步骤2和3,直到队列为空。 以下是C语言实现代码: ```c #include <stdio.h> #include <stdlib.h> // 节点结构体 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 队列结构体及相关操作 struct QueueNode { struct TreeNode *node; struct QueueNode *next; }; struct Queue { struct QueueNode *front; struct QueueNode *rear; }; void enqueue(struct Queue *queue, struct TreeNode *node) { struct QueueNode *newNode = (struct QueueNode*)malloc(sizeof(struct QueueNode)); newNode->node = node; newNode->next = NULL; if (queue->rear == NULL) { queue->front = queue->rear = newNode; } else { queue->rear->next = newNode; queue->rear = newNode; } } struct TreeNode* dequeue(struct Queue *queue) { if (queue->front == NULL) { return NULL; } struct QueueNode *node = queue->front; struct TreeNode *result = node->node; queue->front = node->next; if (queue->front == NULL) { queue->rear = NULL; } free(node); return result; } // 层序遍函数 void levelOrder(struct TreeNode* root) { if (root == NULL) { return; } struct Queue queue = {NULL, NULL}; enqueue(&queue, root); while (queue.front != NULL) { struct TreeNode *node = dequeue(&queue); printf("%d ", node->val); if (node->left != NULL) { enqueue(&queue, node->left); } if (node->right != NULL) { enqueue(&queue, node->right); } } } // 测试代码 int main() { // 构建一棵测试 struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = 1; root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->val = 2; root->left->left = NULL; root->left->right = NULL; root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->val = 3; root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->left->val = 4; root->right->left->left = NULL; root->right->left->right = NULL; root->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->right->val = 5; root->right->right->left = NULL; root->right->right->right = NULL; // 输出层序遍结果 levelOrder(root); return 0; } ``` 输出结果为:1 2 3 4 5。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值