美文网首页
👩‍💻二叉树查找过程

👩‍💻二叉树查找过程

作者: YI_YI_ | 来源:发表于2020-01-17 14:33 被阅读0次

🤨Step1 头文件的引入

image.png

😄 Step2 定义树节点结构

image.png

😏 Step3 声明树节点 和 声明树节点指针

image.png

😊 Step4 创建二叉树函数

image.png

😳 Step5 搜索二叉树中节点

image.png

🙄 Step6 主程序的编写

image.png

😜 Step7 调用搜索

image.png
附上代码
#include <iostream>
#include <stdlib.h>
using namespace std;

struct tree
{
    int data;
    struct tree *left, *right;
};

typedef struct tree node;
typedef node *btree;

// 本程序目的: 搜索节点
// 函数1 创建二叉树
btree create_tree(btree root, int val){
    // 定义变量
    btree newnode, current, backup = NULL;
    // 新的节点的创建
    newnode = (btree)malloc(sizeof(node));
    newnode->data = val;
    newnode->left = NULL;
    newnode->right = NULL;
    // 开始
    if (root == NULL)
    {
        root = newnode;
        return root;
    }
    else
    {
        for (current = root; current != NULL;)
        {
            backup = current;
            if (current->data > val)
                current = current->left;
            else
                current = current->right;
        }
        if (backup->data >val)
            backup->left = newnode;
        else
            backup->right = newnode;
    }
    return root;
}
// 函数2 搜索节点
btree search(btree ptr, int val){
    int i = 1;
    // 一直循环
    while (1)
    {
        if (ptr == NULL){
            return NULL;
        }
        // 统计一共找了几次
        if (ptr->data == val){
            cout << "共查找" << i << "次" << endl;
            // 找到之后就返回
            return ptr;
        }
        else if(ptr->data >val){
            ptr = ptr->left;
        }
        else{
            ptr = ptr->right;
        }
        i++;
    }
}
int main(){
    // 原始的查找
    // 在找到第一次之后,就退出了
    int arr[] = { 7, 1, 4, 2, 8, 13, 12, 11, 15, 9, 5,11 };
    int i = 0;
    int data;// 用来存储需要查找的值
    btree ptr = NULL;
    cout << "原始数组内容:" << endl;
    for (i = 0; i < 11; i++){
        ptr = create_tree(ptr, arr[i]);
        cout << "[" << arr[i] << "]";
    }
    cout << endl;

    cout << "请输入需要查找的值:" << endl;
    cin >> data;

    // 开始进行查找
    if ((search(ptr, data) != NULL))
        cout << "你要查找的值[:" << data << "] 有找到" << endl;
    else
        cout << "您要找的值,没有找到!" << endl;

    system("pause");
    return 0;
}

相关文章

  • 普通二叉树,AVL树,红黑树

    普通二叉树(以下简称二叉树) 在二叉树创建的过程中,如果遇到最坏的情况,即有序列去生成二叉树,会导致该二叉树在查找...

  • 算法复习-查找(2)-折半查找法

    折半查找法 折半查找要求线性表是有序的,即表中记录按关键字排序。 代码: ASL分析: 折半查找的过程可以用二叉树...

  • Java_二叉树用途

    赫夫曼树 赫夫曼树的构造 赫夫曼树构造过程 查找二叉树

  • 6. 二叉树创建-查找

    1. 递归方式实现二叉树的构建2. 树、二叉树、二叉查找树3. 二叉树排序树的理解4. 二叉查找树5. 二叉树的查找

  • 二叉树 堆 2019-04-17

    二叉树 实现一个二叉查找树,并且支持插入、删除、查找操作 实现查找二叉查找树中某个节点的后继、前驱节点 实现二叉树...

  • 《数据结构与算法》知识点(四)

    第七章 查找 顺序查找、折半查找、索引查找、分块查找是静态查找,动态查找有二叉排序树查找,最优二叉树查找,键树查找...

  • 数据结构——二叉树

    1.二分查找和判定树: 二分查找过程可用二叉树来描述:把当前查找区间的中间位置上的结点作为根,左子表和右子表中的结...

  • 👩‍💻二叉树查找过程

    ?Step1 头文件的引入 ? Step2 定义树节点结构 ? Step3 声明树节点 和 声明树节点指针 ? S...

  • 7天练|Day5:二叉树和堆

    关于二叉树和堆的7个必知必会的代码实现二叉树实现一个二叉查找树,并且支持插入、删除、查找操作实现查找二叉查找树中某...

  • 二叉查找树的查找和排序方法的实现

    定义二叉树 二叉查找树的查找和排序方法的实现

网友评论

      本文标题:👩‍💻二叉树查找过程

      本文链接:https://www.haomeiwen.com/subject/jmbczctx.html