zoukankan      html  css  js  c++  java
  • 第四周笔记

    #include<stdio.h>
    #include<stdlib.h>
    //树的定义(结点定义)
    typedef char DataType;
    typedef struct Node {
         DataType  data;
         struct  Node  *lchild;
         struct  Node  *rchild;                   
    } BiNode,*BiTree;

    //创建树的二叉链表(递归)
    void CreateBiTree(BiTree *bt)
    {
        char ch;
        ch = getchar();
        if(ch=='.') *bt=NULL;
        else
        {
            *bt= (BiNode *)malloc(sizeof(BiNode));
            (*bt)->data=ch;
             CreateBiTree(&((*bt)->lchild)); //生成左子树
             CreateBiTree(&((*bt)->rchild)); //生成右子树
        }
    }

    //输出二叉树的元素(先序)
    void Print(BiTree bt)
    {
        if(bt==NULL)    
            return;
        else
        {
            printf("%c ", bt->data);
            Print(bt->lchild);
            Print(bt->rchild);
        }

    }

    int Search(BiTree T, DataType key)
    {
        int L,R;
        if(T==NULL)//若是空树,则说明没有找到,得到0
            return 0;
        else//若不是空树
        {
            if(T->data==key)//若在根结点处找到,则得到层次1
                return 1;
            else//若在根结点处没有找到
                L=Search(T->lchild, key);//往左子树找
                if(L!=0)//若在左子树中找到,则层次为左子树中的层次+1
                    return L+1;
                else
                {
                    R=Search(T->rchild, key);//往右子树查找
                    if(R!=0)
                        return R+1;
                    else
                        return 0;
                }
                Search(T->lchild,key);//遍历左子树
                Search(T->rchild,key);//遍历右子树
        }
    }

    //主函数
    void main()
    {
        char ch;
        BiTree T;
        printf("请输入结点: ");
        CreateBiTree(&T);
        Print(T);
        printf("二叉树为: ");
        getchar();

    printf(" %d",Search(T, 'E'));
    }

  • 相关阅读:
    python第八课
    python第七课
    python第六课
    python第五课
    Python基础30类-内置函数实现迭代器协议
    Python基础29类-内置函数(__format__,__slots__,__doc__,__module__,__del__,__call__)
    Python基础28类-内置函数(__getattribute__,__getitem__,__setitem__.__delittem__)
    Python基础27类-包装、组合方式授权、判断对象类型的方法
    Python基础26类-内置函数__setattr__,__getattr__,__delattr__
    Python基础25类-反射
  • 原文地址:https://www.cnblogs.com/juyuanyuan/p/12891498.html
Copyright © 2011-2022 走看看