Poj 1002 487-3279(二叉搜索树)
题目链接: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(二叉搜索树)的更多相关文章
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- POJ 2309 BST(二叉搜索树)
思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...
- 【二叉搜索树】poj 1577 Falling Leaves
http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...
- 二叉搜索树 POJ 2418 Hardwood Species
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- [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 ...
随机推荐
- web 开发规范
好久没整理博文了 啰嗦两句 转载至新浪...... 于在未开启cleartype的情况下,一些中文字体在非偶数字号下的显示效果欠佳,所以一般建议使用12.14.16.18.22px等偶数字号.也就 ...
- URAL 1297 Palindrome 后缀数组
D - Palindrome Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- Struts学习之集成Ajax
转自:http://blog.csdn.net/hanxuemin12345/article/details/38782213 一,引题 1,Json数据格式简介 JSON是脱离语言的理想的数据交换格 ...
- Linux C网络编程学习笔记
Linux C网络编程总结报告 一.Linux C 网络编程知识介绍: 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户端:(client) 在网络程序中, ...
- iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等.配合UINavigationController的导 ...
- Ubuntu安装配置TFTP服务
tftpd-hpa 是一个功能增强的TFTP服务器.它提供了很多TFTP的增强功能,它已经被移植到大多数的现代UNIX系统. 1.安装 sudo apt-get install tftpd-hpa t ...
- cout输出各种进制
cout使用: int main(){ int a=10; cout<<"Dec:"<<a<<endl; cout<<hex&l ...
- Noip2007提高组总结
两道基础题,后两题比较麻烦,算法想出来后,还是一些细枝末节的问题,需要特别注意,感觉Noip的题目质量还是挺高的,每做一套,都感觉会有大大小小不同的收获,就要月考了,最后把07年的题目总结一下,算是这 ...
- HDU 1001 Sum Problem
/* 注意可以是负整数,而且在过程中会超过int,所以要用longlong */ #include <cstdio> int main() { long long n; while (sc ...
- 提高IOS开发效率的常用网站、开源类库及工具
时间过得很快,学习iOS也已经2年左右了.在这里整理一下,在平台平常开发过程中使用比较多的开源类库.网站与工具吧! 一.网站: UI网站: 1.https://www.cocoacontrols.co ...