• 二叉排序树


     二叉排序树,包括建树的整个过程,还有他的数据结构,应仔细体会。
    // 2418-二叉排序树.cpp : 定义控制台应用程序的入口点。


    #include
    "stdafx.h"

    #include
    <iostream>
    using namespace std;

    struct node
    {
    node
    *left;
    node
    *right;
    double num;
    char str[59];
    }
    *head;//这里建了头指针
    double sum=0;

    void insert(char str[])
    {
    node
    *p1,*p2;
    p1
    =head;
    int t;
    while(p1!=NULL)
    {
    p2
    =p1;
    t
    =strcmp(p1->str,str);
    if(t==0)
    {
    p1
    ->num++;
    return;

    }
    else if(t<0)
    {
    p1
    =p1->right;
    continue;
    }
    else
    {
    p1
    =p1->left;
    continue;
    }
    }
    node
    * add=new node;
    add
    ->num=1;
    add
    ->left=add->right=NULL;
    strcpy(add
    ->str,str);
    if(t>0)
    p2
    ->left=add;
    else
    p2
    ->right=add;
    return ;

    }



    void pprint(node *p)
    {
    if(p!=NULL)
    {
    pprint(p
    ->left);
    printf(
    "%s %.4f\n",p->str,(p->num)*100/sum);
    pprint(p
    ->right);
    }

    }
    int main()
    {
    char str[59];

    head=new node;//这里是新建了个节点,并让头指针指向他
    head
    ->left=NULL;
    head
    ->right=NULL;
    head
    ->num=1;
    sum
    =1;
    gets(str);
    strcpy(head
    ->str,str);
    head
    ->num=1;

    while(gets(str)!=NULL)
    {
    sum
    ++;
    insert(str);
    }
    pprint(head);
    system(
    "pause");
    return 0;
    }



  • 相关阅读:
    css问题
    前端性能优化整理
    算法之排序
    浅谈webpack优化
    js设计模式
    事件模型
    浏览器缓存
    ucGUI 12864 从打点起
    ucGUI例程收藏
    Qt 自动搜索串口号列表
  • 原文地址:https://www.cnblogs.com/zhanglanyun/p/2056682.html
Copyright © 2020-2023  润新知