一.查找函数
这里形参有两个,一个是根节点地址,还有一个是查找的值,若根节点地址为空,返回空,若找到了数据,返回root(递归中的return是返回到上一层,这里是将址传到上一层),另外再设置两个指针变量接收递归的值,防止重复遍历消耗内存,如果找到了,一直返回到函数结束,没找到则不执行if的返回结果,最后如果遍历完整棵树都没找到值则return NULL;
我自己做了一个图方便理解如下:
例如:我要找的值是9,首先从根节点开始找,没找到则开始遍历左子树,1的左孩子2也不是,继续找2的左子树,2的左孩子4也不是,继续找4的左子树.......直到遍历到NULL,返回NULL值到以8为节点的循环的ret1,此时8的左子树已经遍历完了,开始找8的右子树,为空,返回到ret2,此时返回NULL到4的ret1,再找4的ret2.......不断回溯到1的左子树找完,接着像找左子树那样递归遍历右子树,找到9后层层返回到函数结束。(如果在左子树找到了层层返回到函数结束就不用遍历右子树了)
BTNode* TreeSeek(BTNode* root, int x)
{
if (root == NULL)
return NULL;
if (root->data == x)
return root;
BTNode* ret1 = TreeSeek(root->left, x);
if (ret1)
return ret1;
BTNode* ret2 = TreeSeek(root->right, x);
if (ret2)
return ret2;
return NULL;
}
二.开始测试
测试时,先把二叉树的节点结构体构建好:
typedef struct Tree
{
int data;
struct Tree* left;
struct Tree* right;
}BTNode;
需要注意的是,这里结构体成员包含自结构体指针变量,不能和typedef定义的BTNode重名,否则会疯狂报错。
方法一:
这里建树建议用函数封装进行动态内存开辟,建堆的方式,代码如下:
BTNode* BuyNode(int x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL)
{
perror("malloc fail");
return NULL;
}
node->data = x;
node->left = NULL;
node->right = NULL;
return node;
}
BTNode* CreatBinaryTree()
{
BTNode* node1 = BuyNode(1);
BTNode* node2 = BuyNode(2);
BTNode* node3 = BuyNode(3);
BTNode* node4 = BuyNode(4);
BTNode* node5 = BuyNode(5);
BTNode* node6 = BuyNode(6);
BTNode* node7 = BuyNode(6);
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
node5->right = node7;
return node1;
}
主函数:
int main()
{
BTNode* root = CreatBinaryTree();
BTNode* ret = TreeSeek(root, 5);
printf("%d\n", ret->data);
return 0;
}
方法二:
如果嫌麻烦的话,可以直接在主函数里建堆,不用malloc动态内存开辟空间,直接在栈上建树。
代码如下:
int main()
{
BTNode s1;
BTNode s2;
BTNode s3;
s1.left = &s2;
s1.right = &s3;
s1.data = 1;
s2.data = 2;
s3.data = 3;
s2.left = NULL;
s2.right = NULL;
s3.left = NULL;
s3.right = NULL;
BTNode* ret = TreeSeek(&s1, 3);
printf("%d\n", ret->data);
return 0;
}
这里注意有一个常犯的错误:建树不能写出如下的代码
这里的BTNode*是结构体指针,没有指向任何内容,可以认为是空壳,实际上查无此人,不能直接使用,需要指向具体的结构体空间,因此可以指向动态内存开辟的空间或者直接建立的结构体空间:BTNode s1;
三.总测试代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Tree
{
int data;
struct Tree* left;
struct Tree* right;
}BTNode;
BTNode* BuyNode(int x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL)
{
perror("malloc fail");
return NULL;
}
node->data = x;
node->left = NULL;
node->right = NULL;
return node;
}
BTNode* CreatBinaryTree()
{
BTNode* node1 = BuyNode(1);
BTNode* node2 = BuyNode(2);
BTNode* node3 = BuyNode(3);
BTNode* node4 = BuyNode(4);
BTNode* node5 = BuyNode(5);
BTNode* node6 = BuyNode(6);
BTNode* node7 = BuyNode(6);
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
node5->right = node7;
return node1;
}
BTNode* TreeSeek(BTNode* root, int x)
{
if (root == NULL)
return NULL;
if (root->data == x)
return root;
BTNode* ret1 = TreeSeek(root->left, x);
if (ret1)
return ret1;
BTNode* ret2 = TreeSeek(root->right, x);
if (ret2)
return ret2;
return NULL;
}
int main()
{
BTNode* root = CreatBinaryTree();
BTNode* ret = TreeSeek(root, 5);
printf("%d\n", ret->data);
return 0;
}