php二叉树查找节点,c++ 二叉树中搜索节点

创建了一个二叉树,希望搜索到给定info值的节点,

以下为二叉树的相关结构类型定义:

#include "stdio.h"

#include"stdlib.h"

#include "iostream"

using namespace std;

typedef char ELEMTYPE;

#define number 20;

struct BinTreeNode;

typedef struct BinTreeNode* PBinTreeNode;

struct BinTreeNode

{

ELEMTYPE info;

PBinTreeNode lchild;

PBinTreeNode rchild;

};

typedef struct BinTreeNode* PLNBinTree;

以下为创建二叉树算法:

root为已创建好的BinTreeNode类型节点,然后输入一个字符测试是否左子树/右子树为空,不为空则创建左子树/右子树

void constructBinTree(PLNBinTree root)//先根递归创建二叉树

{

if (root == NULL)

return;

char temp = '0';

cout << "输入左值";

cin >> temp;

if (temp == '#')

{

root->lchild = NULL;

}

else

{

root->lchild = new BinTreeNode;

root->lchild->info = temp;

}

cout << "输入右值";

cin >> temp;

if (temp == '#')

{

root->rchild = NULL;

}

else

{

root->rchild = new BinTreeNode;

root->rchild->info = temp;

}

constructBinTree(root->lchild);

constructBinTree(root->rchild);

}

以下为搜索算法:

PBinTreeNode find(PLNBinTree root, char x)//寻找x所在位置

{

if (root == NULL)

return 0;

if (root->info == x)

{

return root;

}

else return 0;

PBinTreeNode c;

c = find(root->lchild, x);

if(!c)

find(root->rchild, x);

return c;

}

以下为测试时结果:

0601c6cc8664a73154ac5894fa252d55.png

ff45bb561eb89d7d047fe1d6ea48d83c.png

76f63d67f30c91299b40226aa09bed22.png

找不出问题所在,求各位会的指教。谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值