树的全部应用

树的遍历以及树的线索化

  • 创建结点
  • 中序递归遍历
  • 后序递归遍历
  • 先序递归遍历输出二叉树的叶子结点
  • 中序遍历输出二叉树的叶子结点
  • 后序遍历输出二叉树的叶子结点
  • 统计叶子结点的数目
  • 分治计算叶子结点
  • 求二叉树的高度
  • 先序遍历二叉树的非递归
  • 中序遍历二叉树的非递归
  • 后序遍历二叉树的非递归
  • 先序线索化二叉树
  • 后序线索化二叉树
  • 中序线索化二叉树

创建结点

#pragma region 创建树的结点
typedef struct BitNode Node;
typedef char datatype;
typedef struct BitNode
{
	Node* lchild;
	Node* rchild;
	datatype Data;
	int ltag;
	int rtag;
};
#pragma endregion```

# 先序递归遍历

```cpp
#pragma region 先序递归遍历
void PreIterativeOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	cout << root->Data << endl;
	PreIterativeOrder(root->lchild);
	PreIterativeOrder(root->rchild);
}
#pragma endregion

中序递归遍历

#pragma region 中序递归遍历
void InIterativeOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	Node* tmpnode = NULL;
	tmpnode = root;
	InIterativeOrder(tmpnode->lchild);
	cout << tmpnode->Data << endl;
	InIterativeOrder(tmpnode->rchild);
}
#pragma endregion

后序递归遍历

#pragma region 后序递归遍历
void PostIterativeOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	Node* tmpnode = root;
	PostIterativeOrder(root->lchild);
	PostIterativeOrder(root->rchild);
	cout << tmpnode->Data << endl;
}
#pragma endregion

先序递归遍历输出二叉树的叶子结点

#pragma region 先序遍历输出二叉树中的叶子结点
void PreOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	if (root->lchild == NULL&& root->rchild == NULL)
	{
		cout << root->Data << endl;
	}
	PreOrder(root->lchild);
	PreOrder(root->rchild);
}
#pragma endregion

中序遍历输出二叉树的叶子结点

#pragma region 中序遍历输出二叉树叶子结点
void InOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	InOrder(root->lchild);
	if (root->lchild == NULL && root->rchild == NULL)
	{
		cout << root->Data << endl;
	}
	InOrder(root->rchild);
}
#pragma endregion

后序遍历输出二叉树的叶子结点

#pragma region 后序遍历输出叶子结点
void PostOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	PostOrder(root->lchild);
	PostOrder(root->rchild);
	if (root->lchild == NULL && root->rchild == NULL)
	{
		cout << root->Data << endl;
	}
}
#pragma endregion

统计叶子结点的数目

#pragma region 统计叶子结点数目
int countLeave = 0;
void CountLeave(Node* root)
{
	/*后序遍历实现叶子结点访问*/
	if (root == NULL)
	{
		return;
	}
	CountLeave(root->lchild);
	CountLeave(root->rchild);
	if (root->lchild == NULL && root->rchild == NULL)
	{
		countLeave++;
	}
}
#pragma endregion

分治计算叶子结点

#pragma region 分治计算叶子结点
int CountDivideConquer(Node* root)
{
	int Leafcount ;
	Node* tmpnode = NULL;
	tmpnode = root;
	if (root = NULL)//根结点为空
	{
		Leafcount = 0;
	}
	else if (tmpnode->lchild == NULL && tmpnode->rchild == NULL)
	{/*为叶子结点*/
		Leafcount = 1;
	}
	else
	{/*根结点包含一个以上的叶子结点*/
		Leafcount = CountDivideConquer(root->lchild) + CountDivideConquer(root->rchild);
	}
	return Leafcount;
}
#pragma endregion

求二叉树的高度

#pragma region 求二叉树的高度
int TreeDepth(Node* root)
{
	int heightLeft,heighttRight ,MaxHeight;
	if (root == NULL)
	{
		heightLeft = 0;
		heighttRight = 0;
		MaxHeight = heighttRight + heightLeft;
	}
	else
	{
		Node* tmpnode = NULL;
		tmpnode = root;
		heightLeft = TreeDepth(root->lchild);
		heighttRight = TreeDepth(root->rchild);
		MaxHeight = heightLeft > heighttRight ? heightLeft : heighttRight;
	}
	return MaxHeight;
}
#pragma endregion

先序遍历二叉树的非递归

#pragma region 先序遍历二叉树的非递归
void NIPreOrder(Node* root)
{
	if (root == NULL)
	{
		cout << "根结点为空" << endl;
		return;
	}
	stack<Node*> stk;
	Node* tmpnode = NULL;
	tmpnode = root;
	while (tmpnode !=NULL || !stk.empty())
	{
		if (tmpnode!=NULL)
		{
			if (tmpnode->rchild != NULL)
			{
				stk.push(tmpnode->rchild);
			}
			cout << tmpnode->Data << endl;
			tmpnode = tmpnode->lchild;
		}
		else
		{
			if (!stk.empty())
			{
				tmpnode = stk.top();
				stk.pop();
			}
		}

	}
}
#pragma endregion

中序遍历二叉树的非递归

#pragma region 中序遍历二叉树的非递归
void INInOrderTree(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	Node* tmpnode = NULL;
	tmpnode = root;
	stack<Node*> stk;
	while (tmpnode!=NULL || !stk.empty())
	{
		if (tmpnode!=NULL)
		{
			stk.push(tmpnode);
			tmpnode = tmpnode->lchild;
		}
		else
		{
			/*左子树为空*/
			if (!stk.empty())
			{
				tmpnode = stk.top();
				stk.pop();
				cout << tmpnode->Data << endl;
				tmpnode = tmpnode->rchild;
			}
		}
	}
}
#pragma endregion

后序遍历二叉树的非递归

#pragma region 后序遍历二叉树的非递归
void INPostOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	stack<Node*> stk;
	Node* tmpnode = NULL;
	Node* previous = NULL;
	tmpnode = root;
	while (tmpnode !=NULL || !stk.empty())
	{
		if (tmpnode !=NULL)
		{
			stk.push(tmpnode);
			tmpnode = tmpnode->lchild;
		}
		else
		{
			if (!stk.empty())
			{
				tmpnode = stk.top();
				stk.pop();
				if (tmpnode->rchild == NULL||previous == tmpnode->rchild)
				{
					cout << tmpnode->Data << endl;
					previous = tmpnode;
					tmpnode = NULL;
				}
				else
				{
					stk.push(tmpnode);//这一步表明根结点是最后访问的 所以需要再次入栈
					tmpnode = tmpnode->rchild;
				}
				
				
				
			}
		}

	}

}
#pragma endregion

先序线索化二叉树

#pragma region 先序线索二叉树
Node* previous = NULL;
void PreCoreOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	Node* tmpnode = NULL;
	tmpnode = root;
	if (tmpnode->lchild == NULL)
	{
		tmpnode->lchild = previous;
		tmpnode->ltag = 1;
	}
	if (previous->rchild == NULL && previous!=NULL)
	{
		previous->rchild = tmpnode;
		previous->rtag = 1;
	}
	previous = tmpnode;
	if (tmpnode->ltag ==0)
	{
		PreCoreOrder(tmpnode->lchild);
	}
	if (tmpnode->rtag == 0)
	{
		PreCoreOrder(tmpnode->rchild);
	}
}
#pragma endregion

后序线索化二叉树

#pragma region 后序线索化
void PostCuleOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	Node* tmpnode = NULL;
	tmpnode = root;
	PostCuleOrder(tmpnode->lchild);
	PostCuleOrder(tmpnode->rchild);
	if (tmpnode->lchild == NULL)
	{
		tmpnode->lchild = previous;
		tmpnode->ltag = 1;
	}
	if (previous->rchild == NULL && previous!=NULL)
	{
		previous->rchild = tmpnode;
	}
	previous = tmpnode;
}
#pragma endregion

中序线索化二叉树

#pragma region 中序线索二叉树
void InCuleOrder(Node* root)
{
	if (root == NULL)
	{
		return;
	}
	Node* tmpnode = NULL;
	tmpnode = root;
	InCuleOrder(tmpnode->lchild);
	if (tmpnode->lchild == NULL)
	{
		tmpnode->lchild = previous;
		tmpnode->ltag = 1;
	}
	if (previous->rchild == NULL && previous !=NULL)
	{
		previous->rchild = tmpnode;
		previous->rtag = 1;
	}
	previous = tmpnode;

	InCuleOrder(tmpnode->rchild);
}
#pragma endregion

你可能感兴趣的:(算法,数据结构)