zoukankan      html  css  js  c++  java
  • 二叉树的查找

    实际上跟我上一篇博文重了,这篇多加了查找


    上一篇文章

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    typedef struct node
    {
        struct node *leftchild;
        struct node *rightchild;
        char data;
    } bitreenode,*bitree;
    void createTree(bitree &T)
    {
        char ch;
        cin>>ch;
        if(ch=='#')T=NULL;
        else
        {
            T=new node;
            T->data=ch;
            createTree(T->leftchild);
            createTree(T->rightchild);
        }
    }
    void preTraverse(bitree &T)
    {
        if(T==NULL)return ;
        cout<<T->data;
        preTraverse(T->leftchild);
        preTraverse(T->rightchild);
    }
    void intraverse(bitree &T)
    {
        if(T==NULL)return ;
        intraverse(T->leftchild);
        cout<<T->data;
        intraverse(T->rightchild);
    
    }
    void posttraverse(bitree &T)
    {
        if(T==NULL)return ;
        posttraverse(T->leftchild);
        posttraverse(T->rightchild);
        cout<<T->data;
    }
    
    node* TreeFindNode(bitree &treeNode,char data)//二叉树的查找
    {
        node *ptr;//这里可以换成bool类型或者int类型的,也就是说返回值不一定要用指针,这仅仅是个标志而已
        if(treeNode==NULL)
        {
            return NULL;
        }
        else
        {
            if(treeNode->data==data)
            {
                return treeNode;
            }
            else        //分别向左右子树查找
            {
                if(ptr=TreeFindNode(treeNode->leftchild,data))  //左子树递归查找
                {
                    return ptr;
                }
                else if(ptr=TreeFindNode(treeNode->rightchild,data))         //右子树递归查找
                {
                    return ptr;
                }
                else
                {
                    return NULL;
                }
            }
        }
    }
    //比如像下面这样
    /*
    int TreeFindNode(bitree &treeNode,char data)//二叉树的查找
    {
        if(treeNode==NULL)
        {
            return 0;
        }
        else
        {
            if(treeNode->data==data)
            {
                return 1;
            }
            else        //分别向左右子树查找
            {
                if(TreeFindNode(treeNode->leftchild,data))  //左子树递归查找
                {
                    return 1;
                }
                else if(TreeFindNode(treeNode->rightchild,data))         //右子树递归查找
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    }*/
    int main()
    {
        int i,j,k;
        node *T;
        createTree(T);
        cout<<endl;
        preTraverse(T);
        cout<<endl;
        intraverse(T);
        cout<<endl;
        posttraverse(T);
        if(TreeFindNode(T,'X')!=NULL)printf("
    YES
    ");
        else printf("
    NO
    ");
        return 0;
    }
    /*
    AB#D##C#E##
    */
    


  • 相关阅读:
    在FreeBSD中pkg包管理器使用实例
    租了一台华为云耀云服务器,却直接被封公网ip,而且是官方的bug导致!
    Java8 Stream对集合的一些应用
    idea 编码UTF-8 设置
    Java RSA非对称加密算法实现
    分库分表 策略 临时
    springboot+dubbo + zookeeper 案例
    跟着华为,学数字化转型(6):一把手工程
    跟着华为,学数字化转型(5):数据保护和业务决策
    跟着华为,学数字化转型(4):数据采集
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554839.html
Copyright © 2011-2022 走看看