• 建立、遍历二叉树(二叉链表)


    建立二叉树: 

    由于先序、中序、后序遍历的任何一个遍历结果单独都不能唯一确定一颗二叉树,因此不能直接使用其中任何一个遍历结果来构造二叉树(原因是不能确定左右子树的大小(节点数),或者说不知道子树的结束位置)

    虽然先序、中序、后序遍历任意两种遍历结果相结合都可以唯一确定一颗二叉树,但是很不方便。

    所以:只要输入为‘#’就表示该树为空,称为扩展二叉树             例输入:ABD##E##C#F##

    二叉链表存储:

    struct node
    {
        char data;
        struct node *L_tree;
        struct node *R_tree;
    };
    struct node *creat_tree()//建立二叉树
    {
        char ch;
        ch = getchar();
        struct node *Tree;
        Tree = (struct node *)malloc(sizeof(struct node));
        if(ch == '#')
            Tree = NULL;
        else
        {
            Tree->data   = ch;//输入本节点信息
            Tree->L_tree = creat_tree();//建左子树
            Tree->R_tree = creat_tree();
        }
        return Tree;
    }
      

    递归先序遍历:

    void preorder_first(struct node *tree)//先序遍历
    {
        if(tree != NULL)
        {
            cout << tree->data << " ";
            preorder_first(tree->L_tree);
            preorder_first(tree->R_tree);
        }
    }

    递归中序遍历:

    void preorder_second(struct node *tree)//中序遍历
    {
        if(tree != NULL)
        {
            preorder_second(tree->L_tree);
            cout << tree->data << " ";
            preorder_second(tree->R_tree);
        }
    }

    递归后序遍历:

    void preorder_third(struct node *tree)//后序遍历
    {
        if(tree != NULL)
        {
            preorder_third(tree->L_tree);
            preorder_third(tree->R_tree);
            cout << tree->data << " ";
        }
    }

    求树的深度:tree=NULL 时高度为0。不为空时当前tree高度是其左右子树高度的最大值+1,左右子树也是二叉树,规模变小求解一样用递归

    int high(struct node *tree)//数的深度
    {
        if(tree == NULL)
            return 0;
        else
            return (max(high(tree->L_tree),high(tree->R_tree))+1);
    }

    求树中节点数:

    void nodecount(struct node *tree)//节点个数
    {
        if(tree != NULL)
        {
            n++;//先序遍历计数
            nodecount(tree->L_tree);
            //n++;//中序遍历计数
            nodecount(tree->R_tree);
            //n++;//后序遍历计数
        }
    }
    

     求树中叶节点数:

    void ye_node(struct node *tree)//叶节点的个数
    {
        if(tree != NULL)
        {
            if(tree->L_tree == NULL && tree->R_tree == NULL)
            {
                n1++;
            }
            ye_node(tree->L_tree);
            ye_node(tree->R_tree);
        }
    }
    

     求树中度为 2 的节点个数:

    void have_two_node(struct node *tree)//度为2的节点的个数
    {
        if(tree != NULL)
        {
            if(tree->L_tree != NULL && tree->R_tree != NULL)
            {
                n2++;
            }
            have_two_node(tree->L_tree);
            have_two_node(tree->R_tree);
        }
    }
    

      

  • 相关阅读:
    关于xml的使用。
    Input标签文件上传,使用详解
    webpack 集成 Typescript && Less
    ionic3 多级联动城市选择插件 ion-multi-picker
    ionic2(3) 密码键盘组件 ionic2-pincode-input 使用
    ionic3 图片(轮播)预览 ionic-gallary-modal组件使用方法
    ios打包,通过Xcode生成ipa文件
    php常见的验证方法
    php 时间转化为刚刚、几秒前、几分前、几天前等等,友好时间提示
    ionic node-sass安装或编译失败:MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”
  • 原文地址:https://www.cnblogs.com/ldy-miss/p/6044443.html
Copyright © 2020-2023  润新知