⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avltree.txt

📁 主要进行大规模的电路综合
💻 TXT
字号:
                             AVL Tree AlgorithmAn AVL Tree is a form of binary tree, however unlike a binary tree, theworst case scenario for a search is O(log n). The AVL data structureachieves this property by placing restrictions on the difference in heightbetween the sub-trees of a given node, and re-balancing the tree if itviolates these restrictions.  ------------------------------------------------------------------------AVL Tree Balance RequirementsThe complexity of an AVL Tree comes from the balance requirements itenforces on each node. A node is only allowed to possess one of threepossible states (or balance factors):Left-High (balance factor -1)The left-sub tree is one level taller than the right-sub treeBalanced (balance factor 0)The left and right sub-trees are both the same heightsRight-High (balance factor +1)The right sub-tree is one level taller than the left-sub tree.If the balance of a node becomes -2 (it was left high and a level was lostfrom the left sub-tree) or +2 (it was right high and a level was lost fromthe right sub-tree) it will require re-balancing. This is achieved byperforming a rotation about this node (see the section below on rotations).  ------------------------------------------------------------------------Inserting in an AVL TreeNodes are initially inserted into AVL Trees in the same manner as anordinary binary search tree (that is, they are always inserted as leafnodes). After insertion, however, the insertion algorithm for an AVL Treetravels back along the path it took to find the point of insertion, andchecks the balance at each node on the path. If a node is found that isunbalanced (that is, it has a balance factor of either -2 or +2), then arotation is performed (see the section below on rotations) based on theinserted nodes position relative to the node being examined (the unbalancednode).NB. There will only ever be at most one rotation required after an insertoperation.  ------------------------------------------------------------------------Deleting from an AVL TreeThe deletion algorithm for AVL Trees is a little more complex, as there areseveral extra steps involved in the deletion of a node. If the node is nota leaf node (that is, is has at least one child), then the node must beswapped with either it's in-order successor or predecessor (based onavailability). Once the node has been swapped we can delete it (and haveits parent pick up any children it may have - bear in mind that it willonly ever have at most one child). If a deletion node was originally a leafnode, then it can simply be removed.Now, as with the insertion algorithm, we traverse back up the path to theroot node, checking the balance of all nodes along the path. If weencounter an unbalanced node we perform an appropriate rotation to balancethe node (see the section below on rotations).NB. Unlike the insertion algorithm, more than one rotation may be requiredafter a delete operation, so in some cases we will have to continue back upthe tree after a rotation.  ------------------------------------------------------------------------AVL Tree RotationsAs mentioned previously, an AVL Tree and the nodes it contains must meetstrict balance requirements to maintain is O(log n) search capabilities.These balance restrictions are maintained using various rotation functions.Below is a diagrammatic overview of the four possible rotations that can beperformed on an unbalanced AVL Tree, illustrating the before and afterstates of an AVL Tree requiring the rotation.                                LL Rotation                    [An LL Rotation - before and after]                                RR Rotation                    [An RR Rotation - before and after]                                LR Rotation                    [An LR Rotation - before and after]                                RL Rotation                    [An RL Rotation - before and after]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -