树的Z型打印

树的Z型打印
判断是否为满二叉树
将中序遍历改为双向链表

#include<stdio.h>
#include<iostream>
#include<assert.h>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define END '#'
typedef  char Elemtype;
typedef struct BtNode
{
	BtNode* leftchild;
	BtNode* rightchild;
	Elemtype data;
}BtNode,*BinaryTree;
int FindPos(const char* is, int n, Elemtype val)
{
	int pos = -1;
	for (int i = 0; i < n; ++i)
	{
		if (is[i] == val)
		{
			pos = i;
			break;
		}
	}
	return pos;
}
BtNode* BuyNode()
{
	BtNode* s = (BtNode*)malloc(sizeof(BtNode));
	if (s == NULL) exit(1);
	memset(s, 0, sizeof(BtNode));
	return s;
}
BtNode* CreateIP(const char* is, const char* Is, int n)
{
	BtNode* s = NULL;
	if (n > 0)
	{
		s = BuyNode();
		s->data = Is[n - 1];
		int pos = FindPos(is, n, Is[n - 1]);
		s->leftchild = CreateIP(is, Is, pos);
		s->rightchild = CreateIP(is + pos + 1, Is + pos, n - pos - 1);
	}
	return s;
}
BtNode* CreateBTIP(const char* is, const char* Is, int n)//中序后序创建
{
	if (is == NULL || Is == NULL || n < 1)return NULL;
	else return CreateIP(is, Is, n);
}
void PrintZ(BtNode* ptr)
{
	if (ptr == NULL)return;
	stack<BtNode*> sta;
	stack<BtNode*> stb;
	sta.push(ptr);
	while (!sta.empty() || !stb.empty())
	{
		while (!sta.empty())
		{
			ptr = sta.top(); sta.pop();
			cout << ptr->data << " ";
			if (ptr->leftchild != NULL)
			{
				stb.push(ptr->leftchild);
			}
			if (ptr->rightchild != NULL)
			{
				stb.push(ptr->rightchild);
			}
			
		}
		while (!stb.empty())
		{
			ptr = stb.top(); stb.pop();
			cout << ptr->data << " ";
			if (ptr->rightchild != NULL)
			{
				sta.push(ptr->rightchild);
			}if (ptr->leftchild != NULL)
			{
				sta.push(ptr->leftchild);
			}
			
		}
	}
	cout << endl;
}
int Getsize(BtNode* ptr)
{
	if (ptr == NULL)return 0;
	else return Getsize(ptr->leftchild) + Getsize(ptr->rightchild) + 1;
}
int GetDepth(BtNode* ptr)
{
	if (ptr == NULL)return 0;
	else return max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}
bool IsFullBiTree(BtNode* ptr)//验证是否是满二叉树
{
	if (ptr == NULL)return true;
	bool res = true; 
	queue<BtNode*> qu;
	qu.push(ptr);
	int n= 1;
	while (!qu.empty())
	{
		if (n != qu.size())
		{
			res = false;
			break;
		}
		int i = 0;
		while (i < n && !qu.empty())
		{
			ptr = qu.front(); qu.pop();
			++i;
			if (ptr->leftchild != NULL)qu.push(ptr->leftchild);
			if (ptr->rightchild != NULL)qu.push(ptr->rightchild);
		}
		if (i < n)
		{
			res = false;
			break;
		}
		n += n;
	}
	return res;
}
bool IsComBinTree(BtNode* ptr)//判断是否是完全二叉树
{
	bool res = true;
	if (ptr == NULL)return true;
	queue<BtNode*> qu;
	
	qu.push(ptr);
	while (!qu.empty())
	{
		ptr = qu.front();qu.pop();
		if (ptr == NULL)break;
		qu.push(ptr->leftchild);
		qu.push(ptr->rightchild);
	}
	while (!qu.empty())
	{
		ptr = qu.front(); qu.pop();
		if (ptr !=NULL)
		{
			res = false;
			break;
		}
	}
	return res;
}
BtNode* NiceList(BtNode* ptr)//中序改造而来的中序遍历成双向链表
{
	if (ptr == NULL)return NULL;
	BtNode* head = NULL;
	BtNode* pre = NULL;
	stack<BtNode*> st;
	while (ptr != NULL || !st.empty())
	{
		while (ptr != NULL)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top();
		st.pop();
		if (head == NULL)
		{
			head = ptr;
			pre = ptr;
		}
		else
		{
			pre->rightchild = ptr;
			ptr->leftchild = pre;
			pre = ptr;
		}
		
		ptr = ptr->rightchild;
	}
	return head;
}
void PreOrder(BtNode* ptr)//先序
{
	if (ptr != NULL)
	{
		printf("%c", ptr->data);
		PreOrder(ptr->leftchild);
		PreOrder(ptr->rightchild);
	}
}
int main()
{
	const char ps[] = "ABCDEFGH";
	const char is[] = "CBEDFAGH";
	const char ls[] = "CEFDBHGA";
	int n = sizeof(ps) / sizeof(ps[0])-1;//strlen(ps)
	BinaryTree root = NULL;
	root = CreateBTIP(is, ls, n);
	PreOrder(root); 
	PrintZ(root);
	root = NiceList(root);
	
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值