代码改变世界

实用算法实现-第 5 篇 二分查找树

2011-10-12 12:26  myjava2  阅读(182)  评论(0编辑  收藏  举报

5.1    二分查找树算法

《算法导论》中,二分查找树的伪代码如下:

ITERATIVE-TREE-SEARCH(x,k)

1  while x ≠ NIL and k ≠ key[x]

2      do if k < key[x]

3            thenx ← left[x]

4            elsex ← right[x]

5  return x

TREE-INSERT(T,z)

1  y ← NIL

2  x ← root[T]

3  while x ≠ NIL

4       do y ← x

5          ifkey[z] < key[x]

6             thenx ← left[x]

7             elsex ← right[x]

8  p[z] ← y

9  if y = NIL

10      thenroot[T] ← z

11      else if key[z] < key[y]

12              thenleft[y] ← z

13              elseright[y] ← z

5.1.1   实例

PKU JudgeOnline, 2418, Hardwood Species.

5.1.2   问题描述

输入一串树木的名字,统计树木在所有树木中所占的百分比。

5.1.3   输入

RedAlder

Ash

Aspen

Basswood

Ash

Beech

YellowBirch

Ash

Cherry

Cottonwood

Ash

Cypress

RedElm

Gum

Hackberry

WhiteOak

Hickory

Pecan

HardMaple

WhiteOak

SoftMaple

RedOak

RedOak

WhiteOak

Poplan

Sassafras

Sycamore

BlackWalnut

Willow

5.1.4   输出

Ash13.7931

Aspen3.4483

Basswood3.4483

Beech3.4483

BlackWalnut 3.4483

Cherry3.4483

Cottonwood3.4483

Cypress3.4483

Gum3.4483

Hackberry3.4483

HardMaple 3.4483

Hickory3.4483

Pecan3.4483

Poplan3.4483

RedAlder 3.4483

RedElm 3.4483

RedOak 6.8966

Sassafras3.4483

SoftMaple 3.4483

Sycamore3.4483

WhiteOak 10.3448

Willow3.4483

YellowBirch 3.4483

5.1.5   程序


5.2    实例

PKU JudgeOnline, 2503, Babelfish.

PKU JudgeOnline, 2418, Hardwood Species.

PKU JudgeOnline, 1002, Exponentiation.

本文章欢迎转载,请保留原始博客链接http://blog.csdn.net/fsdev/article