二叉树数据结构实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25244495/article/details/82797838
#include<iostream>
#include<string.h>
#include<vector>
#include <queue>
using namespace std;

struct BiNode
{
	int val;
	BiNode *left;
	BiNode *right;
	BiNode(int v,BiNode *l = nullptr,BiNode *r = nullptr)
	{
		val = v;
		left = l;
		right = r;
	}
};

//实现二叉排序树
void Insert(BiNode* &p,int val)
{
	if(p == nullptr)
		p = new BiNode(val);
	else if(p->val > val)
		Insert(p->left,val);
	else if (p->val < val)
		Insert(p->right,val);
	else
	{
		//相同元素的情况
	}
}



void PreOrder(BiNode *p)
{
	if (p != nullptr)
	{
		cout << p->val << " ";
		PreOrder(p->left);
		PreOrder(p->right);
	}
}



void InOrder(BiNode* p)
{
	if (p != nullptr)
	{
		InOrder(p->left);
		cout << p->val << " ";
		InOrder(p->right);
	}
}

void PostOrder(BiNode *p)
{
	if(p != nullptr)
	{
		PostOrder(p->left);
		PostOrder(p->right);
		cout << p->val << " ";
	}
}

//队列实现层序遍历
void LayerOrder(BiNode *p)
{
	queue<BiNode*> bnQueue;
	if (p != nullptr)
	{
		bnQueue.push(p);
		while(!bnQueue.empty())
		{
			BiNode* curNode = bnQueue.front();
			cout << curNode->val << " ";
			bnQueue.pop();

			if(curNode->left != nullptr)
				bnQueue.push(curNode->left);
			if(curNode->right != nullptr)
				bnQueue.push(curNode->right);
		}
	}
}

BiNode* findNode(BiNode *p,int val)
{
	if(nullptr == p) return nullptr;
	if(val == p->val) return p;
	BiNode* curP = findNode(p->left,val);
	if(nullptr != curP) return curP;
	else return findNode(p->right,val);
}	

BiNode* FindMinNode(BiNode* p)
{
	if(nullptr == p) return nullptr;

	while(nullptr != p->left)
	{
		p = p->left;
	}
	return p;
}

//删除某一个元素 
bool Delete(BiNode * &p, int val)
{
	if(nullptr == p) return false;

	if (val != p->val)
	{
		if(Delete(p->left,val))
			return true;
		else
			return Delete(p->right,val);
	}
	else
	{	
		if (nullptr == p->left && nullptr == p->right)//没有子节点,直接删除
		{
			delete p;
			p = nullptr;
		}
		else if(nullptr != p->left && nullptr != p->right)//两个子节点
		{
			BiNode* minNode = FindMinNode(p->right);
			p->val = minNode->val;
			Delete(p->right,p->val);
			//错误做法, 因为minNode是新建的副本,更改无效
// 			if(nullptr != minNode->right)
// 				minNode = minNode->right;
		}
		else//1个子节点
		{
            BiNode * temp;
			if(nullptr != p->left)
            {
				temp = p->left;
                delete p;//防止内存泄漏
                p = temp;
            }
			else
            {
				temp = p->right;
                delete p;
                p = temp;
            }
		}		
		return true;
	}

}

int size(BiNode *p)
{
	if(p == nullptr)
		return 0;
	else return size(p->left) + size(p->right) + 1;
}


int height(BiNode * p)
{
	if(nullptr == p)
		return 0;
	else if(nullptr == p->left && nullptr == p->right)
		return 1;
	else
		return max(height(p->left),height(p->right)) + 1;
}

void Destroy(BiNode * &p)//不是引用也能销毁,delete p是释放指针指向的内存,并不是指针本身所占有的内存
{
	if (p->left != nullptr)
		Destroy(p->left);
	if (p->right != nullptr)
		Destroy(p->right);
	if(p != nullptr)
		delete p;
}

int main() 
{
	int a[] = {5,1,2,3,4,6,7,8,9,10};
	BiNode *parent = nullptr;
	for (int i=0;i<10;i++)
	{
		Insert(parent,a[i]);
	}

	PreOrder(parent);
	cout << endl;
	InOrder(parent);
	cout << endl;
	PostOrder(parent);
	cout << endl;
	LayerOrder(parent);
	cout << endl;

	int h = height(parent);

	BiNode* p9 = findNode(parent,9);

	Delete(parent,5);
	InOrder(parent);
	cout << endl;

	Destroy(parent);

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_25244495/article/details/82797838