
Construct Optimal Binary Search Tree by Using Greedy Algorithm
Chun Shi
1, a
, Ming Zhao
1
, Chunyu Li
1, b
, Chunlei Lin
1
and Zhengjie Deng
1
1
School of Information Science & Technology, Hainan Normal University, Haikou, Hainan 571127,
China
a
shichun@hainnu.edu.cn,
b
lichunyu_hn@126.com
Keywords: Binary search tree; Greedy algorithm; Huffman tree; Efficiency; Complexity
Abstract. Focus on some constructions of binary tree, there are many methods to resolve this
problem. With analyzes between binary search tree and Huffman tree, we introduce information
retrieval issue and compare the Huffman tree with optimal binary search tree. And we further
present a method that use greedy algorithm to construct binary search tree and use C++ to realize
method. Experimental provides some conclusions that greedy algorithm is more efficiency than
dynamic programming algorithm.
使用贪心算法构建最优二叉查找树方法
石春
1,a
,赵明
1
,李春雨
1,b
,林春雷
1
,邓正杰
1
1.海南师范大学 信息科学技术学院,海南 海口 571127
摘要:重点研究了二叉树的构成方法。在分析了二进制搜索树和哈夫曼树各自的特点,针对
信息检索问题引入哈夫曼树与最优二叉搜索树。提出了一种采用贪心算法构造二进制搜索树,
并使用 C++程序设计语言实现该方法。实验结果表明,贪心算法比动态规划算法效率更高。
关键词:二叉查找树;哈夫曼树;贪心算法;效率;复杂性
1. 引言
随着计算机技术的发展信息检索已经成为人们查找资源的重要方法,如何提高查找效率就成
为急需解决的问题。现在的信息检索技术分为两大类:链式和树型结构,但是树形存储结构
比线性存储结构更加灵活方便, 这就是现在好多数据检索中经常用树形存储结构检索的原
因。目前构造树形结构进行检索的方法主要有: 二叉排序树、平衡二叉树、红黑树、最佳二
叉排序树[1-10],这些树均只考虑结点在树中的深度,不考虑结点的查找概率,即: 在所有
结点的查找概率相同的前提下构造二叉排序树。这些树是在所有结点的查找概率均相等的情
况下完成的。若在相同的概率下查找,那么二叉排序树的平均比较次数是最少的。因为只考
虑结点的深度。如果所给各结点的查找概率不同,如何构造一棵最优的二叉查找树?
传统的方法都是用动态规划构造最优二叉查找树,但是效率低下且容易产生内存溢出。基于
此原因,本文提出了一种使用贪心算法构建最优二叉查找树的方法。
2. 问题分析
事先给定
维的向量
1 2 3 n
(s ,s ,s ,.....s )
为向量中的元素
且
既
为二叉树中查
找成功的概率。它具有如下性质:
(1) 如果
为空集,则二叉树为一棵空树;
(2) 若
非空,则存储在每个结点中的元素大于其左子树中任一结点所存储的元素,于其右
子树中任一结点所存储的元素;
International Conference on Education, Management and Computer Science (ICEMC 2016)
© 2016. The authors - Published by Atlantis Press