zoukankan      html  css  js  c++  java
  • 二叉排序树

     二叉排序树,包括建树的整个过程,还有他的数据结构,应仔细体会。
    // 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;
    }



  • 相关阅读:
    MySQL客户端mysqladmin命令
    13 Linux磁盘管理
    12 Linux软件管理
    11 Linux压缩打包
    09 Linux输入输出
    08 LinuxACL控制
    07 Linux特殊权限
    06 Linux基本权限
    05 Linux用户管理
    04 Linux文件编辑
  • 原文地址:https://www.cnblogs.com/zhanglanyun/p/2056682.html
Copyright © 2011-2022 走看看