二叉查找树常用小程序

BinaryTree.h

struct BSTreeNode
{
	int m_nValue;         // value of node
	BSTreeNode *m_pLeft;  // left child of node
	BSTreeNode *m_pRight; // right child of node
};

// 创建二元查找树
void addBSTreeNode(BSTreeNode * & pCurrent, int value);

void deleteBSTreeNode(BSTreeNode * &root);

void PrintMidOrderRecur(BSTreeNode *root);

void PrintPreOrderRecur(BSTreeNode *root);

void PrintPosOrderRecur(BSTreeNode *root);

//迭代的前序遍历
void PrintPreOrderIter(BSTreeNode * root);

//迭代的中序遍历
void PrintMidOrderIter(BSTreeNode * root);

unsigned int GetDepthOfTree(BSTreeNode * root);

// 2 怎样从顶部开始逐层打印二叉树结点数据
void PrintByLayer(const BSTreeNode *root);

//3.如何判断一棵二叉树是否是平衡二叉树
bool JudgeAVLTree(const BSTreeNode * root);

//7.怎样编写一个程序,把一个有序整数数组放到二叉树中?
void WriteSortedArrayToTree(const int arrData[], const int nkLength, BSTreeNode *&root);

//9.求二叉树的镜像
void GetMirrotTree(BSTreeNode *root);
void GetMirrotTreeIter(BSTreeNode *root);

void FindePath( BSTreeNode *root, const int nkValue);

BinaryTree.cpp

#pragma once
#include "BinaryTree.h"
#include <iostream>
#include <stack>
#include <queue>
using namespace std;

// 创建二元查找树
void addBSTreeNode(BSTreeNode * & pCurrent, int value)
{
	if (NULL == pCurrent)
	{
		BSTreeNode * pBSTree = new BSTreeNode();
		pBSTree->m_pLeft = NULL;
		pBSTree->m_pRight = NULL;
		pBSTree->m_nValue = value;
		pCurrent = pBSTree;	
	}
	else 
	{
		if ((pCurrent->m_nValue) > value)
			addBSTreeNode(pCurrent->m_pLeft, value);
		else if ((pCurrent->m_nValue) < value)
			addBSTreeNode(pCurrent->m_pRight, value);
		else
		{  /*cout<<"重复加入节点"<<endl; */ }
	}
}

void deleteBSTreeNode(BSTreeNode * &root)
{
	if (NULL == root)
		return;
	deleteBSTreeNode(root->m_pLeft);
	deleteBSTreeNode(root->m_pRight);
	delete root;
	root = NULL;
}

void PrintMidOrderRecur(BSTreeNode *root)
{
	if (NULL == root)
		return;
	PrintMidOrderRecur(root->m_pLeft);
	cout<<root->m_nValue<<" ";
	PrintMidOrderRecur(root->m_pRight);
}

void PrintPreOrderRecur(BSTreeNode *root)
{
	if (NULL == root)
		return;
	cout<<root->m_nValue<<" ";
	PrintPreOrderRecur(root->m_pLeft);
	PrintPreOrderRecur(root->m_pRight);
}

void PrintPosOrderRecur(BSTreeNode *root)
{
	if (NULL == root)
		return;
	PrintPosOrderRecur(root->m_pLeft);
	PrintPosOrderRecur(root->m_pRight);
	cout<<root->m_nValue<<" ";
}

//迭代的前序遍历
void PrintPreOrderIter(BSTreeNode * root)
{
	if (NULL == root)
		return;
	cout<<root->m_nValue<<" ";
	stack<BSTreeNode*> stack_node;
	if(NULL != root->m_pRight)
		stack_node.push(root->m_pRight);
	if(NULL != root->m_pLeft)
		stack_node.push(root->m_pLeft);

	while (0 != stack_node.size())
	{
		BSTreeNode *node = stack_node.top();
		cout<<node->m_nValue<<" ";
		stack_node.pop();
		if(NULL != node->m_pRight)
			stack_node.push(node->m_pRight);
		if(NULL != node->m_pLeft)
			stack_node.push(node->m_pLeft);
	}
	cout<<endl;
}

void PrintMidOrderIter( BSTreeNode * root )
{
	if (NULL == root)
		return;
	stack<BSTreeNode*> stack_node;
	BSTreeNode *pTemp;
	while(NULL != root || !stack_node.empty())
	{
		while(NULL != root)
		{
			stack_node.push(root);
			root = root->m_pLeft;
		}
		pTemp = stack_node.top();
		stack_node.pop();
		cout<<pTemp->m_nValue<<" ";
		root = pTemp->m_pRight;
	}
	cout<<endl;
}

unsigned int GetDepthOfTree( BSTreeNode * root )
{
	if (NULL == root)
		return 0;
	unsigned int nLeftDepth  = GetDepthOfTree(root->m_pLeft);
	unsigned int nRightDepth = GetDepthOfTree(root->m_pRight);
	return 1 + max(nLeftDepth,nRightDepth);
}

void PrintByLayer( const BSTreeNode *root )
{
	if (NULL == root)
		return;
	
	queue<const BSTreeNode *> queue_node;
	queue_node.push(root);
	
	const BSTreeNode *temp;
	while(!queue_node.empty())
	{
		temp = queue_node.front();
		queue_node.pop();
		cout<<temp->m_nValue;
	
		if (NULL != temp->m_pLeft)
			queue_node.push(temp->m_pLeft);
		if (NULL != temp->m_pRight)
			queue_node.push(temp->m_pRight);
	}
}

void WriteSortedArrayToTree( const int arrData[], const int nkLength, BSTreeNode *&root )
{
	if (NULL == arrData || nkLength <= 0)
		return;
	if (NULL == root)
	{
		root = new BSTreeNode;
		root->m_nValue = arrData[nkLength/2];
		root->m_pLeft = NULL;
		root->m_pRight = NULL;
	}
	WriteSortedArrayToTree(arrData,nkLength/2,root->m_pLeft);
	WriteSortedArrayToTree(arrData + nkLength/2 + 1, (nkLength + 1)/2 - 1,root->m_pRight);
}

bool JudgeAVLTree(const BSTreeNode * root )
{
	if (NULL == root)
		return true;

	int nDepthLeft = GetDepthOfTree(root->m_pLeft);
	int nDetthRight= GetDepthOfTree(root->m_pRight);
	
	if (abs(nDetthRight - nDepthLeft) > 1)
		return false;

	return JudgeAVLTree(root->m_pLeft) && JudgeAVLTree(root->m_pRight);
}

void GetMirrotTree( BSTreeNode *root )
{
	if (NULL == root)
		return;
	BSTreeNode *pTemp  = root->m_pLeft;
	root->m_pLeft = root->m_pRight;
	root->m_pRight = pTemp;
	GetMirrotTree(root->m_pLeft);
	GetMirrotTree(root->m_pRight);

}


void GetMirrotTreeIter( BSTreeNode *root )
{
	if (NULL == root)
		return;
	stack<BSTreeNode *> stack_node;
	stack_node.push(root);
	while(!stack_node.empty())
	{
		BSTreeNode * r1 = stack_node.top();
		stack_node.pop();
		BSTreeNode *temp = r1->m_pLeft;
		r1->m_pLeft = r1->m_pRight;
		r1->m_pRight = temp;
		if (NULL != r1->m_pLeft)
			stack_node.push(r1->m_pLeft);
		if (NULL != r1->m_pRight)
			stack_node.push(r1->m_pRight);
	}
}

 int currentSum=0;
 vector<int> path;
 int puth_size = path.size();
void FindePath(  BSTreeNode *root, const int nkValue )
{
	if (NULL != root)
	{
		path.push_back(root->m_nValue);
		currentSum += root->m_nValue;
		puth_size = path.size();
		bool isLeaf=(root->m_pLeft==NULL)&&(root->m_pRight==NULL);
		if (isLeaf&& nkValue == currentSum)
		{
			for (int i=0;i<path.size();i++)
				cout<<path[i]<<" ";
			cout<<endl;
		}
		if (NULL != root->m_pLeft)
		{
			FindePath(root->m_pLeft,nkValue);
		}
		if (NULL != root->m_pRight)
		{
			FindePath(root->m_pRight,nkValue);
		}
		currentSum -= root->m_nValue;
		path.pop_back();
	}
}




main.cpp

#pragma once
#include <iostream>
#include "BinaryTree.h"
#include <assert.h>


// 1.二叉树三种周游(traversal)方式:  OK

// 2.怎样从顶部开始逐层打印二叉树结点数据 OK

// 3.如何判断一棵二叉树是否是平衡二叉树 OK

// 5.如何不用递归实现二叉树的前序/后序/中序遍历? OK

// 6.在二叉树中找出和为某一值的所有路径 OK

// 7.怎样编写一个程序,把一个有序整数数组放到二叉树中? OK

// 4.求二叉树的镜像  OK

//http://www.sunhongfeng.com/2010/11/bintree_pre_in_pos/

using namespace std;

void Test7(const int arrData[],const int nkLength) 
{
	cout<<"把一下数组写二叉树中"<<endl;
	for (int i = 0;i < nkLength; ++i)
		cout<<arrData[i]<<" ";
	BSTreeNode *root =  NULL;

	WriteSortedArrayToTree(arrData,nkLength,root);
	cout<<endl<<"中序遍历结果:"<<endl;
	PrintMidOrderRecur(root);
	cout<<endl;
	
	cout<<"深度为 : "<<GetDepthOfTree(root)<<endl;
	deleteBSTreeNode(root);
}
void Test3()
{
	BSTreeNode *root = NULL;
	addBSTreeNode(root,5);
	addBSTreeNode(root,2);
	addBSTreeNode(root,3);
	addBSTreeNode(root,1);
	addBSTreeNode(root,4);
	addBSTreeNode(root,6);
	addBSTreeNode(root,8);
	addBSTreeNode(root,7);

	if (JudgeAVLTree(root) != true)
		cout<<"判断正确,该树不是平衡树"<<endl;

	deleteBSTreeNode(root);
	addBSTreeNode(root,5);
	addBSTreeNode(root,4);
	addBSTreeNode(root,3);
	addBSTreeNode(root,7);
	addBSTreeNode(root,6);
	addBSTreeNode(root,8);
	if (JudgeAVLTree(root) == true)
		cout<<"判断正确,该树是平衡树"<<endl;
	deleteBSTreeNode(root);
}

void TestGetMirrotTree()
{
	BSTreeNode *root = NULL;
	addBSTreeNode(root,5);
	addBSTreeNode(root,2);
	addBSTreeNode(root,3);
	addBSTreeNode(root,1);
	addBSTreeNode(root,4);
	addBSTreeNode(root,6);
	addBSTreeNode(root,8);
	addBSTreeNode(root,7);
	cout<<endl;
	cout<<"原二叉树前序遍历:"<<endl;
	PrintPreOrderRecur(root);
	GetMirrotTree(root);
	cout<<endl<<"镜像树的前序遍历"<<endl;
	PrintPreOrderRecur(root);
	cout<<endl<<endl;
	deleteBSTreeNode(root);
}

void TestGetMirrotTreeIter()
{
	BSTreeNode *root = NULL;
	addBSTreeNode(root,5);
	addBSTreeNode(root,2);
	addBSTreeNode(root,3);
	addBSTreeNode(root,1);
	addBSTreeNode(root,4);
	addBSTreeNode(root,6);
	addBSTreeNode(root,8);
	addBSTreeNode(root,7);
	cout<<endl;
	cout<<"原二叉树前序遍历:"<<endl;
	PrintPreOrderRecur(root);
	GetMirrotTreeIter(root);
	cout<<endl<<"镜像树的前序遍历"<<endl;
	PrintPreOrderRecur(root);
	cout<<endl<<endl;
	deleteBSTreeNode(root);
}

//5 如何不用递归实现二叉树的前序/后序/中序遍历?
void Test5(BSTreeNode *root)
{
	cout<<"1.二叉树三种周游(traversal)方式"<<endl;
	cout<<"递归前序遍历"<<endl;
	PrintPreOrderRecur(root);
	cout<<endl<<"递归中序遍历"<<endl;;
	PrintMidOrderRecur(root);
	cout<<endl<<"递归后序遍历"<<endl;;
	PrintPosOrderRecur(root);
	cout<<endl;
	
	cout<<endl<<"如何不用递归实现二叉树的前序/后序/中序遍历"<<endl;
	cout<<"迭代前序遍历"<<endl;
	PrintPreOrderIter(root);

	cout<<"迭代中序遍历"<<endl;
	PrintMidOrderIter(root);
}
int main()
{
	BSTreeNode *root = NULL;
	addBSTreeNode(root,7);
	addBSTreeNode(root,2);
	addBSTreeNode(root,6);
	addBSTreeNode(root,3);
	addBSTreeNode(root,1);
	addBSTreeNode(root,4);
	addBSTreeNode(root,5);
	addBSTreeNode(root,8);
	
	Test5(root);         //遍历
	FindePath(root,15);  //打印出和与输入整数相等的所有路径
	
	//cout<<GetDepthOfTree(root)<<endl;

	cout<<endl<<"2.怎样从顶部开始逐层打印二叉树结点数据"<<endl;
	PrintByLayer(root);
	cout<<endl<<endl;

	cout<<endl<<"3.如何判断一棵二叉树是否是平衡二叉树"<<endl;
	Test3();


	cout<<endl<<endl<<"7.怎样编写一个程序,把一个有序整数数组放到二叉树中?"<<endl;
	int arrData1[] = {1,2,3,4};
	Test7(arrData1,4);
	int arrData2[]= {1,2,3,4,5,6,7,8,9,10};
	Test7(arrData2,10);

	deleteBSTreeNode(root);

	TestGetMirrotTree();
	TestGetMirrotTreeIter();
	return 0;
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/dyingfair/article/details/79372630
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞