AVL树的实现(源码)

来源:互联网 发布:gpa的算法 编辑:程序博客网 时间:2024/04/28 16:46
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

AVLTree

AVLTree.htm">http://www.staroceans.com/AVLTree.htm

A. First Edition
This is first edition of my AVL Tree and I never expect it takes so long as almost one week to finish! Of course
it is partly because of Christmas when I was continually interrupt by the earthly issues such as visiting a 
friend and wasting some meaningful time on meaningless matters. 
B.The problem
To write AVL tree on template basis and try to keep as much as possible of original BST frame work
because the code by Mr. Shaffer is very concise and compact! And efficiency is also a very important
issue here. As AVLTree has to store extra information than a BST, it is expected that we need to 
reduce as many "balancing operations" as possible. 
C.The idea of program 

By adding a little piece of code in "insert" method of BST, I actually try to do the keeping-balance

job along the "insert" operation from bottom-up which always keep each node up-to-date balanced. It

is believed by me the best solution to implement it. Unfortunately I have to keep the clue of the

path which leads to the newly-inserted node at leaf position. So, I was obliged to add one more

field in node---"inLeft" which is a boolean value indicating which side the path goes down to the

new leaf. Along with it is the "height" data which is the absolute height of a subtree rooted as

current node. I thought it is a cheating in pseudo code by using "balancing-factor" as instead of

using "real height" of each node. But after finishing the program with several big changes, I

realized I might do some stupid things on assuming that absolute height is necessary. Maybe in

second edition I should combine both "inLeft" and "height" together with "factor"----balancing

factor.

D.The major functions
1. bool insert(const Elem& e)
Do you expect that I might start from here? But no, I didn't change anything here. And it is only 
after I finished, I thought I can omit it even "inserthelp" is not virtual.
2. BinNode<Elem>* inserthelp(BinNode<Elem>*, const Elem&);
This function is almost same as original BST except I try to update the height after each insertion
which will go up from the inserted new leaf along path. And before that insertion, I placed a road
sign "inLeaf" to indicate which side the path takes.
3. void updateHeight(BinNode<Elem>*& subroot);
This is the key part of program where you update height first and then try to examine the balance
and try to keep it. It is the tricky part as I change the code many times. Finally I realized that
there are two big cases: a) The first root which is also the first node with factor 2/-2; b) The node
whose son node has factor of 2/-2; There are some extra conditions to examine the "first" in a) to 
make sure it is the "first". 
4. int getTreeHeight(BinNode<Elem>* subroot);
I resist to use recursive method because the field "height" is a short-cut.
5. BinNode<Elem>* singleRotate(BinNode<Elem>* parent, bool isRoot, bool left2right);
Don't forget to adjust balance after rotating and the sequence is important as you have to do it from
bottom-up.
6. BinNode<Elem>* doubleRotate(BinNode<Elem>* parent, bool isRoot, bool left2right);
I made it look simple by adding the "doDouble" and quite satisfy with it.
E.Further improvement
 
F.File listing
1. AVLTree.h
2. BinNode.h
3. BST.h
4. dictionary.h
5. Elem.h
6. AVLTree.cpp  (main)
 
file name: BinNode.h
// Binary tree node abstract classtemplate <class Elem> class BinNode {public:<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击