惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

Sonui – 魔帆博客

【记录】使用Golang开发腾讯云函数踩的坑 | 魔帆博客 【笔记】Hydro二次开发总结 前端篇(一) | 魔帆博客 Linux 报错Certificate verification failed: The certificate is NOT trusted. | 魔帆博客 Python 数字大小写转换 | 魔帆博客 Windows10 系统盘开启 Bitlocker | 魔帆博客 【源码】打猎游戏 html 纯原生代码 | 魔帆博客 VS Code C/C++ 环境配置 | 魔帆博客 【技巧】干掉第三方抢票!如何以最快的速度抢到火车票 | 魔帆博客 【记录】Deepin 桌面无限转圈(风火轮) | 魔帆博客 easySQLite使用帮助 | 魔帆博客 【软件】QQ坦白说发送者查看 --已凉 | 魔帆博客 手把手教你重装原版纯净Windows系统-魔帆出品 | 魔帆博客 【软件】XML文本解析器 | 魔帆博客 【软件】Mac OS环境下最强虚拟机软件--Parallels Desktop | 魔帆博客 【镜像】Mac OS X El Capitan 10.11 (15B42) 懒人版安装镜像 支持变色龙和Clvoer引导 | 魔帆博客 【技术向】如何获取指定QQ在线状态 | 魔帆博客 QQ广告屏蔽器 | 魔帆博客 【开源】自己写的机器人插件源码部分开源 | 魔帆博客 5月20号真的是潘金莲毒死武大郎的日子么?? | 魔帆博客
C/C++ 二叉树 | 魔帆博客
Sonui · 2022-03-30 · via Sonui – 魔帆博客

查找二叉树有一个特性

对于所有的节点,都满足左子树上的所有节点都比自己的小,而右子树上的所有节点都比自己大
————《挑战程序设计竞赛第二版》P78

#include <iostream>
#include <stdlib.h>

using namespace std;

typedef struct TreeNode
{
    int data;
    struct TreeNode *LT, *RT;
}TreeNode, *TreeList;

//创建一个叶子节点
TreeNode* creatTree(int pData){
    TreeNode *tree = (TreeNode*)malloc(sizeof(TreeNode));
    tree->data = pData;
    tree->LT = NULL;
    tree->RT = NULL;
    return tree;
}

int addLeaf(TreeList &node, int deep){
    //传参需要注意,二叉树是指针类型的,节点本身就是一个指针:*node。所以需要二级指针才能改变二叉树的内容
    //TreeNode *node = NULL;
    char input;
    cin >> input;
    if(input != '#'){
        node = creatTree((int)input);
        addLeaf(node->LT, deep + 1);
        addLeaf(node->RT, deep + 1);
    } else{
        node = NULL;
    }
    return 1;
}

int treeInfo(TreeNode *tree, int deep){
    cout << "deep: " << deep << ", data: " << (char)tree->data << endl;
    if (tree->LT != NULL) treeInfo(tree->LT, deep + 1);
    if (tree->RT != NULL) treeInfo(tree->RT, deep + 1);
    return 0;
}

TreeNode *findValue(TreeNode *node, int value){
    if (node == NULL) return NULL;
    if (node->data == value) {
        cout << "ok, find it." << endl;
        return node;
    }
    else
        return findValue(node->data < value ? node->RT : node->LT, value);
    return NULL;
}

int main(){
    char data;
    TreeList tree, temp;
    addLeaf(tree, 0);
    cout << "start out info:" << endl;
    treeInfo(tree, 0);
    cout << "end out info" << endl;
    cout << "input a num to find: ";
    cin >> data;
    temp = findValue(tree, data);
    if (!temp) cout << "no find it." <<endl;
    else cout << "find in " << &temp << ", data: " << (char)temp->data << endl;

}