数据结构 二叉树【记录:二叉树的操作】

71 篇文章 12 订阅
41 篇文章 1 订阅

注:本文不做详细讲解,注释仅在代码段中,作个人记录使用

详解请看往日博客记录:博客类型为数据结构或算法

 

目录

一、二叉树结构

二、创建二叉树

三、插入,使之成为二叉搜索树

四、三大遍历

五、返回高度

六、层序遍历

七、重建二叉树


一、二叉树结构

typedef struct BNode
{
	int data;
	BNode* left;
	BNode* right;
}BNode,*Tree;

二、创建二叉树

Tree createTree()
{
	int value;
	cin >> value;
	if (value == -1)
	{
		return NULL;
	}
	BNode* n = (BNode*)malloc(sizeof(BNode));
	n->data = value;
	n->left = createTree();
	n->right = createTree();
	return n;
}

三、插入,使之成为二叉搜索树

void insertBST(Tree& t,int value)
{
	BNode* node = (BNode*)malloc(sizeof(BNode));
	node->data = value;
	node->left = NULL;
	node->right = NULL;

	if (t == NULL)
	{
		t = node;
	}
	else
	{
		BNode* temp = t;
		while (temp != NULL)
		{
			//进入左子树
			if(value<temp->data)
			{
				if (temp->left == NULL)
				{
					temp->left = node;
					return;
				}
				temp = temp->left;
			}

			//进入右子树
			if (value > temp->data)
			{
				if (temp->right == NULL)
				{
					temp->right = node;
					return;
				}
				temp = temp->right;
			}
		}
	}
}

四、三大遍历

//前序遍历
void preOrder(Tree t)
{
	if (t != NULL)
	{
		cout << t->data << " ";
		preOrder(t->left);
		preOrder(t->right);
	}
}

//中序遍历
void inOrder(Tree t)
{
	if (t != NULL)
	{
		inOrder(t->left);
		cout << t->data << " ";
		inOrder(t->right);
	}
}

//后序遍历
void postOrder(Tree t)
{
	if (t != NULL)
	{
		postOrder(t->left);
		postOrder(t->right);
		cout << t->data << " ";
	}
}

五、返回高度

//返回高度
int getLength(Tree t)
{
	if (t == NULL)
	{
		return 0;
	}
	int thisLength = 1;
	int leftLength = getLength(t->left);
	int rightLength = getLength(t->right);
	int maxLRLength = leftLength > rightLength ? leftLength : rightLength;
	int length = thisLength + maxLRLength;
	return length;
}

六、层序遍历

//层序遍历
//三个容器:一个结果向量,一个队列,一个当前层向量
void levelOrder(Tree t)
{
	if (t == NULL)
	{
		return;
	}
	//向量容器,用来存放层序遍历结果
	vector<int> result;
	//用队列存储结点
	queue<BNode*> q;
	//根结点入队
	q.push(t);
	//只要当前队列不为空
	while (!q.empty())
	{
		//用于存储当前遍历这一层的结点
		vector<int> temp;
		int n = q.size();
		for (int i = 0; i < n; i++)
		{
			BNode* cur = q.front();
			temp.push_back(cur->data);
			q.pop();
			if (cur->left != NULL)
			{
				q.push(cur->left);
			}
			if (cur->right != NULL)
			{
				q.push(cur->right);
			}
		}
		for (vector<int>::iterator it = temp.begin(); it != temp.end(); it++)
		{
			result.push_back(*it);
		}
	}
	for (vector<int>::iterator it = result.begin(); it != result.end(); it++)
	{
		cout << *it << " ";
	}
}

七、重建二叉树

//重建二叉树
Tree reBuild(vector<BNode*>pre, vector<BNode*>vin)
{
	int vinlen = vin.size();
	if (vinlen == 0)
	{
		return NULL;
	}
	vector<Tree>leftPre, leftVin,rightPre,rightVin;
	BNode* root = (BNode*)malloc(sizeof(BNode));
	root->data = pre[0]->data;
	int rootLocation = 0;
	for (int i = 0; i < vinlen; i++)
	{
		if (vin[i]->data == pre[0]->data)
		{
			rootLocation = i;
			break;
		}
	}
	for (int i = 0; i < rootLocation; i++)
	{
		leftPre.push_back(pre[i + 1]);
		leftVin.push_back(vin[i]);
	}
	for (int i = rootLocation + 1; i < vinlen; i++)
	{
		rightPre.push_back(pre[i]);
		rightVin.push_back(vin[i]);
	}
	root->left = reBuild(leftPre, leftVin);
	root->right = reBuild(rightPre, rightVin);
	return root;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值