二叉树的建立及遍历

#include<iostream>
#include<stack> 
#include<queue>
using namespace std;

typedef char ElemType;
typedef struct BtNode
{
	struct BtNode* leftchild;
	struct BtNode* rightchild;
	ElemType data;
}BtNode, * BinaryTree;

BtNode* Buynode()
{
	BtNode* s = (BtNode*)malloc(sizeof(BtNode));
	if (nullptr == s) exit(1);
	memset(s, 0, sizeof(BtNode));
	return s;
}

void PreOrder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		printf("%c ", ptr->data);
		PreOrder(ptr->leftchild);
		PreOrder(ptr->rightchild);
	}
}

void InOrder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		InOrder(ptr->leftchild);
		printf("%c ", ptr->data);
		InOrder(ptr->rightchild);
	}
}

void LastOrder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		LastOrder(ptr->leftchild);
		LastOrder(ptr->rightchild);
		printf("%c ", ptr->data);
	}
}
//str建立二叉树
BtNode* CreateBtStr(const char*& str)
{
	BtNode* s = nullptr;
	if (*str != '#')
	{
		s = Buynode();
		s->data = *str;
		s->leftchild = CreateBtStr(++str);
		s->rightchild = CreateBtStr(++str);
	}
	return s;
}

BtNode* CreateTreeStr(const char* str)
{
	if (nullptr == str || strlen(str) <= 0) return nullptr;
	else return CreateBtStr(str);

}
//前中序建立二叉树
int FindPos(const char* istr, int n,char ch)
{
	int pos = -1;
	for (int i = 0; i < n; ++i)
	{
		if (istr[i] == ch)
		{
			pos = i;
			break;
		}
	}
	return pos;
}
BtNode* CreateBtTreePI(const char* pstr, const char* istr, int n)
{
	BtNode *s = nullptr;
	if (n > 0)
	{
		s = Buynode();
		s->data = pstr[0];
		int pos = FindPos(istr, n, pstr[0]);
		if (pos == -1) exit(1);
		s->leftchild = CreateBtTreePI(pstr+1,istr,pos);
		s->rightchild = CreateBtTreePI(pstr+1+pos,istr+1+pos,n-pos-1);
	}
	return s;
}

BtNode* CreateBinaryTreePI(const char* pstr, const char* istr)
{
	int n = strlen(pstr);
	int m = strlen(istr);
	if (nullptr == pstr || nullptr == istr || n < 1 || m < 1 || n != m) return nullptr;
	else return CreateBtTreePI(pstr, istr, n);
}
//中后序建立二叉树
BtNode* CreateBtTreeIL(const char* istr, const char*lstr, int n)
{
	BtNode* s = nullptr;
	if (n > 0)
	{
		s = Buynode();
		s->data = lstr[n-1];
		int pos = FindPos(istr, n, lstr[n-1]);
		if (pos == -1) exit(1);
		s->leftchild = CreateBtTreeIL(istr,lstr,pos);
		s->rightchild = CreateBtTreeIL(istr+pos+1,lstr+pos,n-pos-1);
	}
	return s;
}
BtNode* CreateBinaryTreeIL(const char* istr, const char* lstr)
{
	int n = strlen(istr);
	int m = strlen(lstr);
	if (nullptr == istr || nullptr == lstr || n < 1 || m < 1 || n != m) return nullptr;
	else return CreateBtTreeIL(istr, lstr, n);
}
//二叉树非递归先序遍历
void NicePreOrder(BtNode* ptr)
{
	if (nullptr == ptr) return;
	stack<BtNode*> st;
	st.push(ptr);
	while (!st.empty())
	{
		ptr=st.top();
		st.pop();
		printf("%c ", ptr->data);
		if (ptr->rightchild != nullptr)
		{
			st.push(ptr->rightchild);
		}
		if (ptr->leftchild != nullptr)
		{
			st.push(ptr->leftchild);
		}
	}
	printf("\n");
}

//二叉树非递归中序遍历
void NiceInOrder(BtNode* ptr)
{
	if (nullptr == ptr) return;
	stack<BtNode*> st;
	while (!st.empty()||ptr!=nullptr)
	{
		while (ptr != nullptr)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top();
		st.pop();
		printf("%c ", ptr->data);
		ptr = ptr->rightchild;
	}
	printf("\n");
}

//二叉树非递归后序遍历
void NiceLastOrder(BtNode* ptr)
{
	if (nullptr == ptr) return;
	stack<BtNode*> st;
	BtNode* tag = nullptr;
	while (!st.empty() || ptr != nullptr)
	{
		while (ptr != nullptr)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top();
		st.pop();
		if (ptr->rightchild == nullptr || ptr->rightchild == tag)
		{
			printf("%c ", ptr->data);
			tag = ptr;
			ptr = nullptr;
		}
		else
		{
			st.push(ptr);
			ptr = ptr->rightchild;
		}
	}
	printf("\n");
}
//二叉树非递归中序遍历2
struct StkNode
{
	BtNode* pnode;
	int popnum;
};
void NiceInOrder_2(BtNode* ptr)
{
	if (nullptr == ptr)return;
	stack<StkNode> st;
	st.push(StkNode{ ptr,0 });
	while (!st.empty())
	{
		StkNode node = st.top();
		st.pop();
		if (++node.popnum == 2)
		{
			printf("%c ", node.pnode->data);
			if (node.pnode->rightchild != nullptr)
			{
				st.push(StkNode{ node.pnode->rightchild,0 });
			}
		}
		else
		{
			st.push(node);
			if (node.popnum == 1 && node.pnode->leftchild != nullptr)
			{
				st.push(StkNode{ node.pnode->leftchild,0 });
			}
		}
	}
	printf("\n");
}
//二叉树非递归后序遍历2
void NicePastOrder_2(BtNode* ptr)
{
	if (nullptr == ptr)return;
	stack<StkNode> st;
	st.push(StkNode{ ptr,0 });
	while (!st.empty())
	{
		StkNode node = st.top();
		st.pop();
		if (++node.popnum == 3)
		{
			printf("%c ", node.pnode->data);
		}
		else
		{
			st.push(node);
			if (node.popnum == 1 && node.pnode->leftchild != nullptr)
			{
				st.push(StkNode{node.pnode->leftchild,0});
			}
			else if(node.popnum == 2 && node.pnode->rightchild != nullptr)
			{
				st.push(StkNode{ node.pnode->rightchild,0 });
			}
		}
	}
	printf("\n");
}

//二叉树层次遍历
void LevelOrder(BtNode* ptr)
{
	if (nullptr == ptr) return;
	queue<BtNode*> qu;
	qu.push(ptr);
	while (!qu.empty())
	{
		ptr = qu.front();
		qu.pop();
		printf("%c ", ptr->data);
		if (ptr->leftchild != nullptr)
		{
			qu.push(ptr->leftchild);
		}
		if (ptr->rightchild != nullptr)
		{
			qu.push(ptr->rightchild);
		}
	}
	printf("\n");
}
//获得二叉树节点个数
int GetSize(BtNode* ptr)
{
	if (nullptr == ptr) return 0;
	else return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) + 1;
}
//获得二叉树深度
int Max(const int a, const int b)
{
	return a > b ? a : b;
}
int GetDepth(BtNode* ptr)
{
	if (nullptr == ptr) return 0;
	else return Max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}
//判断二叉树是否为满二叉树
bool IsFullBinaryTree(BtNode* ptr)
{
	bool ret = true;
	if (nullptr == ptr) return ret;
	queue<BtNode*> qua;
	queue<BtNode*> qub;
	int i = 0;
	int n = 1;
	qua.push(ptr);
	while (!qua.empty() || !qub.empty())
	{
		for (i = 0; i < n && !qua.empty(); ++i)
		{
			ptr = qua.front();
			qua.pop();
			if (ptr->leftchild != nullptr)
			{
				qub.push(ptr->leftchild);
			}
			if (ptr->rightchild != nullptr)
			{
				qub.push(ptr->rightchild);
			}
		}
		if (i < n)
		{
			ret = false;
			break;
		}
		if (qub.empty()) break;
		n += n;
		for (i = 0; i < n && !qub.empty(); ++i)
		{
			ptr = qub.front();
			qub.pop();
			if (ptr->leftchild != nullptr)
			{
				qua.push(ptr->leftchild);
			}
			if (ptr->rightchild != nullptr)
			{
				qua.push(ptr->rightchild);
			}
		}
		if (i < n)
		{
			ret = false;
			break;
		}
		if (qua.empty()) break;
		n += n;
	}
	return ret;
}
//判断二叉树是否为完全二叉树
bool IsCompBinaryTree(BtNode* ptr)
{
	bool ret = true;
	if (nullptr == ptr) return ret;
	queue<BtNode*> qu;
	qu.push(ptr);
	while (!qu.empty())
	{
		ptr = qu.front();
		qu.pop();
		if (nullptr == ptr) break;
		qu.push(ptr->leftchild);
		qu.push(ptr->rightchild);
	}
	while (!qu.empty())
	{
		ptr = qu.front();
		qu.pop();
		if (ptr != nullptr)
		{
			ret = false;
			break;
		}
	}
	return ret;
}
//判断二叉树是否是BST树
//判断二叉树是否是平衡二叉树
//判断二叉树是否是对称二叉树
//判断二叉树是否是另一棵树的子树
//判断两个二叉树是否相同
int main()
{
	printf("str建立二叉树:\n");
	const char* str = "ABC##DE##F##G#H##";
	BinaryTree root = CreateBtStr(str);
	PreOrder(root);
	printf("\n");
	InOrder(root);
	printf("\n");
	LastOrder(root);
	printf("\n");

	printf("前中序建立二叉树:\n");
	const char* pstr = "ABCDEFGH";
	const char* istr = "CBEDFAGH";
	BinaryTree root1 = CreateBinaryTreePI(pstr, istr);
	PreOrder(root1);
	printf("\n");
	InOrder(root1);
	printf("\n");
	LastOrder(root1);
	printf("\n");

	printf("中后序建立二叉树:\n");
	//const char* istr = "CBEDFAGH";
	const char* lstr = "CEFDBHGA";
	BinaryTree root2 = CreateBinaryTreeIL(istr,lstr);
	PreOrder(root2);
	printf("\n");
	InOrder(root2);
	printf("\n");
	LastOrder(root2);
	printf("\n");

	printf("二叉树非递归先序遍历\n");
	NicePreOrder(root);

	printf("二叉树非递归中序遍历\n");
	NiceInOrder(root);

	printf("二叉树非递归后序遍历\n");
	NiceLastOrder(root);

	printf("二叉树非递归中序遍历2\n");
	NiceInOrder_2(root);

	printf("二叉树非递归后序遍历2\n");
	NicePastOrder_2(root);

	printf("二叉树层次遍历\n");
	LevelOrder(root);

	int a = GetSize(root);
	printf("二叉树节点个数为:%d\n",a);

	int b=GetDepth(root);
	printf("二叉树深度为:%d\n",b);

	bool ret = IsFullBinaryTree(root);
	printf("此二叉树root是否为满二叉树:%d\n", ret);

	const char* istr1 = "DBEAFCG"; 
	const char* lstr1 = "DEBFGCA";
	BinaryTree root3 = CreateBinaryTreeIL(istr1, lstr1);
	bool ret1 = IsFullBinaryTree(root3);
	printf("此二叉树root3是否为满二叉树:%d\n", ret1);

	bool ret2 = IsCompBinaryTree(root);
	printf("此二叉树root是否为完全二叉树:%d\n", ret2);

	bool ret3 = IsCompBinaryTree(root3);
	printf("此二叉树root3是否为完全二叉树:%d\n", ret3);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值