数据结构与程序设计(27).ppt
《数据结构与程序设计(27).ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计(27).ppt(62页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、数据结构与程序设计(27)教师:鲍钰12/28/20221数据结构与程序设计 教师:鲍钰Height Balance:AVL TreesnDefinition:nAn AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees.12/28/20222数据结构与程序设计 教师:鲍钰Height Balan
2、ce:AVL TreesnWith each node of an AVL tree is associated a balance factor that is left higher,equal,or right higher according,respectively,as the left subtree has height greater than,equal to,or less than that of the right subtree.12/28/20223数据结构与程序设计 教师:鲍钰Height Balance:AVL TreesnIn drawing diagram
3、s,we shall show a left-higher node by/,a node whose balance factor is equal by,and a right-higher node by.12/28/20224数据结构与程序设计 教师:鲍钰Height Balance:AVL Trees12/28/20225数据结构与程序设计 教师:鲍钰Height Balance:AVL Trees12/28/20226数据结构与程序设计 教师:鲍钰Height Balance:AVL Trees12/28/20227数据结构与程序设计 教师:鲍钰Height Balance:AVL
4、 TreesnWe employ an enumerated data type to record balance factors:enum Balance_factor left_higher,equal_height,right_higher;nAVL nodes are structures derived from binary search tree nodes with balance factors included.12/28/20228数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesn#include Binary_node.cppntempl
5、ate nstruct AVL_node:public Binary_node n/additional data member:nBalance_factor balance;n/constructors:nAVL_node();nAVL_node(const Record&x);n/overridden virtual functions:nvoid set_balance(Balance_factor b);nBalance_factor get_balance()const;n;12/28/20229数据结构与程序设计 教师:鲍钰Height Balance:AVL TreesnAVL
6、_node:AVL_node()nleft=NULL;nright=NULL;nbalance=equal_height;nntemplate nAVL_node:AVL_node(const Record&x)ndata=x;nleft=NULL;nright=NULL;nbalance=equal_height;n12/28/202210数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nvoid AVL_node:set_balance(Balance_factor b)nnbalance=b;nntemplate nBalance_fac
7、tor AVL_node:get_balance()constnnreturn balance;n12/28/202211数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesnenum Balance_factor left_higher,equal_height,right_higher;ntemplate nstruct Binary_node n/data members:nEntry data;nBinary_node*left;nBinary_node*right;n/constructors:nBinary_node();nBinary_node(cons
8、t Entry&x);n/virtual methods:nvirtual void set_balance(Balance_factor b);nvirtual Balance_factor get_balance()const;n;12/28/202212数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nBinary_node:Binary_node()nleft=NULL;nright=NULL;nntemplate nBinary_node:Binary_node(const Entry&x)ndata=x;nleft=NULL;nri
9、ght=NULL;n12/28/202213数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nvoid Binary_node:set_balance(Balance_factor b)nnntemplate nBalance_factor Binary_node:get_balance()constnnreturn equal_height;n12/28/202214数据结构与程序设计 教师:鲍钰Height Balance:AVL TreesnWe often invoke these methods(set_balance,get_bal
10、ance)through pointers to nodes,such as left-get_balance().But left could(for the compiler)point to any Binary_node,not just to an AVL_node,and these methods are declared only for AVL node.nWe therefore declare the Binary_node versions of set_balance and get_balance as virtual methods,selected at run
11、 time.12/28/202215数据结构与程序设计 教师:鲍钰Insertions into an AVL tree12/28/202216数据结构与程序设计 教师:鲍钰Insertions into an AVL tree12/28/202217数据结构与程序设计 教师:鲍钰Insertions into an AVL tree12/28/202218数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesn#include Search_tree.cppntemplate nclass AVL_tree:public Search_tree npublic:nEr
12、ror_code insert(const Record&new_data);nError_code remove(Record&old_data);12/28/202219数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesnprivate:/Add auxiliary function prototypes here.nError_code avl_insert(Binary_node*&sub_root,const Record&new_data,bool&taller);nvoid rotate_left(Binary_node*&sub_root);nvoi
13、d rotate_right(Binary_node*&sub_root);nvoid right_balance(Binary_node*&sub_root);nvoid left_balance(Binary_node*&sub_root);n/add for removenError_code avl_remove(Binary_node*&sub_root,Record&new_data,bool&shorter);nbool right_balance2(Binary_node*&sub_root);nbool left_balance2(Binary_node*&sub_root)
14、;n;12/28/202220数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nError_code AVL_tree:insert(const Record&new_data)n/*Post:If the key of new data is already in the AVL tree,a code of duplicate_error is returned.Otherwise,a code of success is returned and the Record new data is inserted into the tree
15、in such a way that the properties of an AVL tree are preserved.nUses:avl_insert.*/nnbool taller;/Has the tree grown in height?nreturn avl_insert(root,new_data,taller);n12/28/202221数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nError_code AVL_tree:avl_insert(Binary_node*&sub_root,nconst Record&new
16、_data,bool&taller)nnError_code result=success;nif(sub_root=NULL)nsub_root=new AVL_node(new_data);ntaller=true;n nelse if(new_data=sub_root-data)nresult=duplicate_error;ntaller=false;n 12/28/202222数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesnelse if(new_data data)/Insert in left subtree.nresult=avl_insert
17、(sub_root-left,new_data,taller);nif(taller=true)nswitch(sub_root-get_balance()/Change balance factors.ncase left_higher:nleft_balance(sub_root);ntaller=false;/Rebalancing always shortens the tree.nbreak;ncase equal_height:nsub_root-set_balance(left_higher);nbreak;ncase right_higher:nsub_root-set_bal
18、ance(equal_height);ntaller=false;nbreak;n n 12/28/202223数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesh+1hcase left_higherleft_balance(sub_root)hhcase equal_heighttaller=truehcase right_higherh+1sub_root12/28/202224数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesnelse /Insert in right subtree.nresult=avl_insert(sub
19、_root-right,new_data,taller);nif(taller=true)nswitch(sub_root-get_balance()ncase left_higher:nsub_root-set_balance(equal_height);ntaller=false;nbreak;ncase equal_height:nsub_root-set_balance(right_higher);nbreak;ncase right_higher:nright_balance(sub_root);ntaller=false;/Rebalancing always shortens t
20、he tree.nbreak;n n nreturn result;n 12/28/202225数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesh+1hcase left_higherhhcase equal_heighttaller=truehcase right_higherright_balance(sub_root);h+1sub_root12/28/202226数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nvoid AVL_tree:rotate_left(Binary_node*&sub_root)
21、n/*Pre:sub_root points to a subtree of the AVL tree.This subtree has a nonemptynright subtree.nPost:sub_root is reset to point to its former right child,and the former sub_rootnnode is the left child of the new sub_root node.*/nnif(sub_root=NULL|sub_root-right=NULL)/impossible casesncout WARNING:pro
22、gram error detected in rotate left endl;nelse nBinary_node*right_tree=sub_root-right;nsub_root-right=right_tree-left;nright_tree-left=sub_root;nsub_root=right_tree;n n12/28/202227数据结构与程序设计 教师:鲍钰12/28/202228数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nvoid AVL_tree:rotate_right(Binary_node*&sub_
23、root)n/*Pre:sub_root points to a subtree of the AVL tree.This subtree has a nonemptynleft subtree.nPost:sub_root is reset to point to its former left child,and the former sub_rootnnode is the right child of the new sub_root node.*/nnif(sub_root=NULL|sub_root-left=NULL)/impossible casesncout WARNING:
24、program error detected in rotate right endl;nelse nBinary_node*left_tree=sub_root-left;nsub_root-left=left_tree-right;nleft_tree-right=sub_root;nsub_root=left_tree;n n12/28/202229数据结构与程序设计 教师:鲍钰Height Balance:AVL Treesntemplate nvoid AVL_tree:right_balance(Binary_node*&sub_root)n/*Pre:sub root point
25、s to a subtree of an AVL tree,doubly unbalanced on the right.nPost:The AVL properties have been restored to the subtree.nUses:Methods of struct AVL node;functions rotate_right,rotate_left.*/nnBinary_node*&right_tree=sub_root-right;nswitch(right_tree-get_balance()ncase right_higher:/single rotation l
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序设计 27
限制150内