题目链接:http://poj.org/problem?id=1002

思路分析:先对输入字符进行处理,转换为标准形式;插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> struct TreeNode;
typedef char ElementType[];
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
struct TreeNode
{
int Count;
ElementType Element;
SearchTree Left;
SearchTree Right;
}; void StrToNum( char *str , char * Tel );
SearchTree MakeEmpty( SearchTree T );
SearchTree Insert( ElementType X, SearchTree T );
void PrintTel( SearchTree T );
int flag = ; int main()
{
int n;
char Str[], Tel[];
SearchTree T = NULL; menset( Tel, , sizeof(Tel) ); scanf( "%d", &n );
for ( int i = ; i < n; ++i )
{
scanf( "%s", Str );
StrToNum( Str, Tel );
T = Insert( Tel, T );
} PrintTel( T );
if ( flag == )
printf( "No duplicates.\n" ); return ;
} void PrintTel( SearchTree T )
{
if ( T != NULL )
{
PrintTel( T->Left );
if ( T->Count > )
{
printf( "%s %d\n", T->Element, T->Count );
flag = ;
}
PrintTel( T->Right );
}
} SearchTree MakeEmpty( SearchTree T )
{
if ( T != NULL )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
} SearchTree Insert( ElementType X, SearchTree T )
{
if ( T == NULL )
{
T = ( Position )malloc( sizeof( struct TreeNode ) );
if ( T == NULL )
{
printf( "Out of space" );
return NULL;
}
else
{
strcpy( T->Element, X );
T->Left = T->Right = NULL;
}
}
else
if ( strcmp(X, T->Element) < )
T->Left = Insert( X, T->Left );
else
if ( strcmp(X, T->Element) > )
T->Right = Insert( X, T->Right); return T;
} void StrToNum( char *str , char * Tel )
{
int i, j; for ( i = j = ; str[i] != '\0'; i++ )
{
if ( str[i] == '-' );
else
if ( '' <= str[i] && str[i] <= '' )
Tel[j++] = str[i];
else
if ( str[i] < 'Q' )
Tel[j++] = ( str[i] - 'A' ) / + + '';
else
Tel[j++] = ( str[i] - 'A' - ) / + + ''; if ( j == )
Tel[j++] = '-';
}
}

Poj 1002 487-3279(二叉搜索树)的更多相关文章

  1. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  2. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  3. POJ 2309 BST(二叉搜索树)

    思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...

  4. 【二叉搜索树】poj 1577 Falling Leaves

    http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...

  5. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  6. POJ 1577 Falling Leaves(二叉搜索树)

    思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个  二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  10. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. ADF 项目创建流程

    ADF 项目创建流程: 1.首先建好应用 2.创建model,UI 3.创建EO,VO,AO, VL 4.设置EO的属性 5.新建lov 6.设置VO的View Accessors,并设置Attrib ...

  2. ubuntu之iptables

    1.更新iptables并立即生效: a.保存当前设置:iptables-save > /etc/iptables.up.rules b.修改iptables规则: 例如: -I INPUT - ...

  3. button 变成圆

      btn.layer.cornerRdius = width/2.0;btn.layer.maskToBounds = width/2.0:   

  4. PHP创建定义数组

    $array = array();      $array["key"] = "values";  ?> 在PHP中声明数组的方式主要有两种:1.用arr ...

  5. .net format 中 大括号{}处理

    1.string string.format(string format,object arg0) 错误:因为方法中使用{n}做占位符号了,所以其他需要括号{}的地方,就需要{{}}

  6. Linux下 保存 git账号密码

    一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...

  7. shell学习-读取输入

    功能:读取输入,打印:如果长度小于MINLEN,那么输出空格. #!/bin/bash # paragraph-space.sh # Insert a blank line between parag ...

  8. 关于block 用法

    Block  Apple 在C, Objective-C, C++加上Block這個延申用法.目前只有Mac 10.6 和iOS 4有支援.Block是由一堆可執行的程式組成,也可以稱做沒有名字的Fu ...

  9. LintCode-三数之和

    题目描述: 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 注意事项 在三元组(a, b, c),要求a <= b <= c ...

  10. Oracle 11g透明网关连接Sqlserver 2000

    一.环境 公司网站系统使用的是IIS + Oracle 但公司某系统使用的是Sqlserver 2000, 但其数据需要做成报表放到网站上,为简化编程,使用Oracle做透明网关,定期从Sqlserv ...