二叉树遍历、查找、判段满二叉树、完全二叉树(C++)

定义节点

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

typedef char ElemType;
typedef struct BtNode   // BinaryTreeNode
{
	BtNode* leftchild;
	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;
}

创建二叉树

int FindIs(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* CreatePI(const char* pr, const char* is, int n)
{
	BtNode* s = nullptr;
	if (n > 0)
	{
		s = Buynode();
		s->data = pr[0]; // 
		int pos = FindIs(is, n, pr[0]);
		if (pos == -1) exit(1);
		s->leftchild = CreatePI(pr + 1, is, pos);
		s->rightchild = CreatePI(pr+pos+1,is+pos+1,n-pos-1);
	}
	return s;
}
//根据给定的前序遍历和中序遍历结果创建二叉树
BtNode* CreateTreePI(const char* pr, const char* is)
{
	if (pr == nullptr || is == nullptr) return nullptr;
	int pn = strlen(pr);
	int in = strlen(is);
	if (pn != in) return nullptr;

	return CreatePI(pr, is, pn);
}

BtNode* CreateIP(const char* is, const char* ps, int n)
{
	BtNode* s = nullptr;
	if (n > 0)
	{
		s = Buynode();
		s->data = ps[n - 1];
		int pos = FindIs(is, n, ps[n - 1]);
		if (pos == -1) exit(1);
		s->leftchild = CreateIP(is, ps, pos);
		s->rightchild = CreateIP(is + pos + 1, ps + pos, n - pos - 1);
	}
	return s;
}
//根据给定的中序遍历和后续遍历结果创建二叉树
BtNode* CreateTreeIP(const char* is, const char* ps)
{
	if (is == nullptr || ps == nullptr) return nullptr;
	int in = strlen(is);
	int pn = strlen(ps);
	if (in != pn) return nullptr;
	return CreateIP(is, ps, in);
}

int main()
{
	BinaryTree root = nullptr;
	char pr[] = { "ABCDEFGH" };
	char is[] = { "CBEDFAGH" };
	char ps[] = { "CEFDBHGA" };
	//root = CreateTreePI(pr, is);
	root = CreateTreeIP(is, ps);
	NicePreOrder(root);
	NiceInOrder(root);
	NicePastOrder(root);
	return 0;
}

递归遍历二叉树

1.前序遍历

void PreOrder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		cout << ptr->data << " ";
		PreOrder(ptr->leftchild);
		PreOrder(ptr->rightchild);
	}
}

2.中序遍历

void InOrder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		InOrder(ptr->leftchild);
		cout << ptr->data << " ";
		InOrder(ptr->rightchild);
	}
}

3.后序遍历

void PastOrder(BtNode* ptr)
{
	if (ptr != nullptr)
	{
		PastOrder(ptr->leftchild);
		PastOrder(ptr->rightchild);
		cout << ptr->data << " ";
	}
}

非递归遍历二叉树

1.前序遍历

void NicePreOrder(BtNode* ptr)
{
	if (ptr == nullptr) return;
	std::stack<BtNode*> st;
	st.push(ptr);
	while (!st.empty())
	{
		ptr = st.top(); st.pop();
		cout << ptr->data << " ";
		if (ptr->rightchild != nullptr)
		{
			st.push(ptr->rightchild);
		}
		if (ptr->leftchild != nullptr)
		{
			st.push(ptr->leftchild);
		}
	}
	cout << endl;
}

2.中序遍历

void NiceInOrder(BtNode* ptr)
{
	if (ptr == nullptr) return;
	std::stack<BtNode*> st;
	while(ptr != nullptr || !st.empty())
	{ 
		while (ptr != nullptr)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top(); st.pop();
		cout << ptr->data << " ";
		ptr = ptr->rightchild;
    }
	cout << endl;
}

3.后序遍历

void NicePastOrder(BtNode* ptr)
{
	if (ptr == nullptr) return;
	BtNode* tag = nullptr;
	std::stack<BtNode*> st;
	while (ptr != nullptr || !st.empty())
	{
		while (ptr != nullptr)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top(); st.pop();
		if (ptr->rightchild == nullptr || ptr->rightchild == tag)
		{ 
			cout << ptr->data << " ";
			tag = ptr;
			ptr = nullptr;
		}
		else
		{
			st.push(ptr);
			ptr = ptr->rightchild;
		}
	}
	cout << endl;
}

4.层次遍历

void NiceLevelOrder(BtNode* ptr)
{
	if (ptr == nullptr) return;
	std::queue<BtNode*> qu;
	qu.push(ptr);
	while (!qu.empty())
	{
		BtNode* p = qu.front(); qu.pop();
		cout << p->data << " ";
		if (p->leftchild != nullptr)
		{
			qu.push(p->leftchild);
		}
		if (p->rightchild != nullptr)
		{
			qu.push(p->rightchild);
		}
	}
	cout << endl;
}

5.Z字型遍历

void NiceZLOrder(BtNode* ptr)
{
	if (ptr == nullptr) return;
	std::stack<BtNode*> sta,stb;
	sta.push(ptr);
	while (!sta.empty() || !stb.empty())
	{
		while (!sta.empty())
		{
			ptr = sta.top(); sta.pop();
			cout << ptr->data << " ";
			if (ptr->leftchild != nullptr) stb.push(ptr->leftchild);
			if (ptr->rightchild != nullptr) stb.push(ptr->rightchild);
		}
		while (!stb.empty())
		{
			ptr = stb.top(); stb.pop();
			cout << ptr->data << " ";
			if (ptr->rightchild != nullptr) sta.push(ptr->rightchild);
			if (ptr->leftchild != nullptr) sta.push(ptr->leftchild);
		}	
	}
	cout << endl;
}

获取二叉树结点个数

int GetSize(BtNode* ptr)
{
	if (ptr == nullptr) 
		return 0;
	else 
		return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) + 1;
}

获取二叉树深度

int GetDepth(BtNode* ptr)
{
	if (ptr == nullptr)
		return 0;
	else 
		return std::max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}	
int GetNiceDepth(BtNode* ptr)
{
	int sum = 0;
	if (ptr == nullptr) return sum;
	std::queue<BtNode*> qu;
	qu.push(ptr);
	while (!qu.empty())
	{
		int n = qu.size();
		while (n--)
		{
			ptr = qu.front(); qu.pop();
			if (ptr->leftchild != nullptr) qu.push(ptr->leftchild);
			if (ptr->rightchild != nullptr) qu.push(ptr->rightchild);
		}
		sum += 1;
	}
	return sum;

}

判断是否是满二叉树

bool Is_Full(BtNode* ptr)
{
	int he = 1;
	bool res = true;
	if (ptr == nullptr) return res;
	std::queue<BtNode*> qu;
	qu.push(ptr);
	while (!qu.empty())
	{
		int n = qu.size();
		if (n != he)
		{
			res = false;
			break;
		}
		while (n--)
		{
			ptr = qu.front(); qu.pop();
			if (ptr->leftchild != nullptr) qu.push(ptr->leftchild);
			if (ptr->rightchild != nullptr) qu.push(ptr->rightchild);
		}
		he += he;
	}
	return res;
}

判断是否是完全二叉树

bool Is_Comp(BtNode* ptr)
{
	bool res = true;
	if (ptr == nullptr) return res;
	std::queue<BtNode*> qu;
	qu.push(ptr);
	while (!qu.empty())
	{
		ptr = qu.front(); qu.pop();
		if (ptr == nullptr) break;
		qu.push(ptr->leftchild);
		qu.push(ptr->rightchild);
	}
	while (!qu.empty())
	{
		ptr = qu.front(); qu.pop();
		if (ptr != nullptr)
		{
			res = false;
			break;
		}
	}
	return res;
}

查找值

BtNode* FindValue(BtNode* ptr, ElemType val)
{
	if (ptr == nullptr || ptr->data == val)
	{
		return ptr;
	}
	else
	{
		BtNode* p = FindValue(ptr->leftchild, val);
		if (p == nullptr)
		{
			p = FindValue(ptr->rightchild, val);
		}
		return p;
	}
}

查找直接双亲结点

BtNode* Parent(BtNode* ptr, BtNode* child)
{
	if (ptr == nullptr || ptr->leftchild == child 
		|| ptr->rightchild == child)
	{
		return ptr;
	}
	else
	{
		BtNode* p = Parent(ptr->leftchild, child);
		if (p == nullptr)
		{
			p = Parent(ptr->rightchild, child);
		}
		return  p;
	}
}
BtNode* FindParent(BtNode* ptr, BtNode* child)
{
	if (ptr == nullptr || child == nullptr || ptr == child)
	{
		return nullptr;
	}
	else
	{
		return Parent(ptr, child);
	}
}

图论

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值