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 ...
随机推荐
- Servlet 的基本架构
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
- JavaSE学习总结第27天_反射 & 设计模式 & JDK5、7、8新特性
27.01 反射_类的加载概述和加载时机 类的加载:当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载:就是指将class文件读 ...
- PHP创建定义数组
$array = array(); $array["key"] = "values"; ?> 在PHP中声明数组的方式主要有两种:1.用arr ...
- Java "JSON中无分隔符日期字符串处理"
Json 中日期类型数据处理,服务端传输的日期没有分隔符,一般格式就两种,[20151212121212]即yyyyMMddhhmmss和[121212]hhmmss import java.text ...
- poj 3273 Monthly Expence 简单二分
/** 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大 ...
- eclipse编译错误
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error AGE ...
- Sql:查看数据库表和表结构的语句
T-sql 显示表结构和字段信息的sql语句: exec sp_help tablename; ~~使用存储过程 sp_help 显示数据库包含哪些表的sql语句: use yourDBname;se ...
- php命名空间及和autoload结合使用问题。
在讨论如何使用命名空间之前,必须了解 PHP 是如何知道要使用哪一个命名空间中的元素的.可以将 PHP 命名空间与文件系统作一个简单的类比.在文件系统中访问一个文件有三种方式: 相对文件名形式如foo ...
- Struts2 学习笔记15 Struts标签 part1
来说一下Struts标签,之前我们也很多地方用到了,还是来总结一下. 首先是property标签. <li>property:<s:property value="user ...
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...