二叉树的基本操作

这篇博客主要介绍了二叉树的一些基本操作,包括先序、中序、后序遍历的递归和非递归实现,以及计算树的高度和左子树的数量。这些内容源于作者的实际工作需求,旨在自我提醒和巩固相关知识。
摘要由CSDN通过智能技术生成

由于岗位实践需要,故写了一个树的基本操作,包括先,中,后序递归和非递归,计算高度,计算左子树个数,无其他用处,警示自己而已 .. 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stack>
using namespace std;

// tree node
typedef struct TreeNode {
	char data;
	struct TreeNode* lChild;
	struct TreeNode* rChild;
} Node;

int n;
stack<Node*> s;

// 先序 create tree
void createTree(Node **t, char *c)
{
	n++;
	if(n > strlen(c) - 1) return;
	if(c[n] == '0') *t = NULL;
	else 
	{
		(*t) = (Node*)malloc(sizeof(Node));
		(*t)->data = c[n];
		createTree(&(*t)->lChild, c);
		createTree(&(*t)->rChild, c);
	}
}
// 先序递归访问
void preVisit(Node *t)
{
	if(t)
	{
		printf("%c", t->data);
		preVisit(t->lChild);
		preVisit(t->rChild);
	}
}
// 中序递归访问
void midVisit(Node *t)
{
	if(t)
	{
		midVisit(t->lChild);
		printf("%c", t->data);
		midVisit(t->rChild);
	}
}
// 后序递归访问
void lastVisit(Node *t)
{
	if(t)
	{
		lastVisit(t->lChild);
		lastVisit(t->rChild);
		printf("%c", t->data);
	}
}


// 先序非递归访问
void uPre(Node* head)
{
	while(!s.empty()) s.pop();
	while(head || !s.empty())
	{
		while(head)
		{
			printf("%c", head->data);
			s.push(head);
			head = head->lChild;
		}
		if(!s.empty())
		{
			head = s.top();
			s.pop();
			head = head->rChild;
		}
	}
}

// 中序非递归访问
void uMid(Node* head)
{
	while(!s.empty()) s.pop();
	while(head || !s.empty())
	{
		while(head)
		{
			s.push(head);
			head = head->lChild;
		}
		if(!s.empty())
		{	
			head = s.top();
			printf("%c", head->data);
			s.pop();
			head = head->rChild;
		}
	}

}
// 后序非递归访问
void uLast(Node* head)
{
	Node *p = head;
	head = NULL;
	while(!s.empty()) s.pop();
	do
	{
		while(p)
		{
			s.push(p);
			p = p->lChild;
		}
		p = s.top();
		p = p->rChild;
		if(head == p || p == NULL)
		{
			head = s.top();
			s.pop();
			printf("%c", head->data);
			p = NULL;
		}
	}while(!s.empty());
}
// 计算高度
int calH(Node* head)
{
	if(!head) return 0;
	int left = calH(head->lChild);
	int right = calH(head->rChild);
	int h;
	if(left > right) {
		h = left + 1;
	}
	else 
	{
		h = right + 1;
	}
	return h;
}

// 计算左子叶子个数
int calLeftChild(Node *t)
{
	if(!t) return 0;
	else if(t->lChild && !t->lChild->lChild && !t->lChild->rChild) return 1;
	else return calLeftChild(t->lChild) + calLeftChild(t->rChild);
}

int main()
{
	char a[1000];
	int c;
	scanf("%d", &c);
	while(c--)
	{
		n = -1;
		scanf("%s", a);
		Node *head;
		createTree(&head, a);
		preVisit(head);
		putchar('\n');
		midVisit(head);
		putchar('\n');
		lastVisit(head);
		putchar('\n');
		printf("非递归先序\n");
		uPre(head);
		putchar('\n');
		printf("\n非递归中序\n");
		uMid(head);
		putchar('\n');
		printf("\n非递归后序\n");
		uLast(head);
		putchar('\n');
		printf("\n%d\n", calH(head));
		printf("%d\n", calLeftChild(head));
	}
	return 0;
}

数据结构很久前就不写了 生疏的很 ... 那个汗 ...

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值