二叉树基本算法实现:[二叉树构造,左右子树交换,key值查找]

 ABC##DE#G##F###

#include <iostream>
#include <string>
using namespace std;
string str;
int i;//全局变量
struct BinaryTree
{
	char val;
	struct BinaryTree* lchild, * rchild;
	BinaryTree(char c) :val(c), lchild(NULL), rchild(NULL) {}//构造函数
};
BinaryTree* createTree() {//递归建树
	char c = str[i++];//i是全局变量,所以函数里面的i等于是直接调用的全局i。str也是。str初始化在main函数中
	if (c == '#') return NULL;
	//因为给的是前序序列,遍历顺序是中(根)->前(左)->后(右),所以先创空间,再依次递归左右子树
	BinaryTree* root = new BinaryTree(c);//空间创建
	root->lchild = createTree();//递归
	root->rchild = createTree();//递归
	return root;
}

void SwapTree(BinaryTree* root)//以根结点为参数,交换每个结点左右子树(前序递归)
{
	//思路:考虑用递归进行
	BinaryTree* temp = root->lchild;//临时变量存左孩子的值
	root->lchild = root->rchild;//交换
	root->rchild = temp;//完成了一轮交换
	if (root->lchild)//此时如果左孩子不为空
		SwapTree(root->lchild);//递归调用该函数,开始以左孩子为根结点,进行上述操作,一直递归到底【左孩子为空时】
	if (root->rchild)//此时如果右孩子不为空
		SwapTree(root->rchild);//递归调用该函数,开始以右孩子为根结点,进行上述操作,一直递归到底【右孩子为空时】,函数结束
}

int Find(char key, BinaryTree* root)//查找值为key的结点,并且输出该结点的所有祖先结点
{
	if (!root)  return 0;
	if (root->val == key)    return 1;
	//如果子树中可以找到匹配值 那么此节点肯定是祖先结点
	if (Find(key,root->lchild) || Find(key,root->rchild))
	{
		cout<< "<-"<<root->val;
		return 1;
	}
	return 0;
}
void inOrderTraversal(BinaryTree* root) {//中序遍历,顺序是:前->中->后
	if (!root) return;//到空了,就返回
	inOrderTraversal(root->lchild);
	cout << root->val << " ";
	inOrderTraversal(root->rchild);
}
int main() {
	cout << "请输入二叉树:" << endl;
	cin >> str;
	i = 0;
	BinaryTree* root = createTree();//二叉树构建
	cout << "请输入想查找的key值:(char)" << endl;
	char key;
	cin >> key;
	if (!Find(key, root))//如果没找到
	{
		cout << "不存在该key值!!" << endl;
	}
	cout << endl;
	cout << "交换前,中序遍历测试:" << endl;
	inOrderTraversal(root);
	cout << "开始交换左右子树......" << endl;
	SwapTree(root);
	cout << "交换左右子树完成!" << endl;

	//测试:
	cout << "交换后,中序遍历测试:" << endl;
	inOrderTraversal(root);
	return 0;
}

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当然,我很乐意为您提供帮助!以下是一个简单的C语言实现的二叉树查找算法示例: ``` #include<stdio.h> #include<stdlib.h> struct node { int key; struct node *left, *right; }; struct node *newNode(int item) { struct node *temp = (struct node *)malloc(sizeof(struct node)); temp->key = item; temp->left = temp->right = NULL; return temp; } struct node* insert(struct node* node, int key) { if (node == NULL) return newNode(key); if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); return node; } struct node *search(struct node* root, int key) { if (root == NULL || root->key == key) return root; if (root->key < key) return search(root->right, key); return search(root->left, key); } int main() { struct node *root = NULL; root = insert(root, 50); insert(root, 30); insert(root, 20); insert(root, 40); insert(root, 70); insert(root, 60); insert(root, 80); int x = 60; struct node* result = search(root, x); if(result == NULL){ printf("%d does not exist in the tree!", x); }else{ printf("%d exists in the tree!", result->key); } return 0; } ``` 在此示例中,我们创建了一个节点结构体,其中包含一个键和左右节点指针。 `insert` 函数插入新节点,而 `search` 函数搜索以找到特定键。此外,我们还使用 `newNode` 函数来创建新节点,以及使用 `main` 函数为二叉搜索添加一些节点并搜索特定的。 祝您编程愉快!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值