Data Structures: Dictionaries and Balancing Trees PDF
Document Details

Uploaded by LuckyHeliodor1184
University of the West Indies
Tags
Summary
These are lecture notes on dictionaries and various balancing trees. The notes cover AVL trees, red-black trees and splay trees, including node insertion and deletion plus appropriate algorithms. The slides are for a data structures class at the University of the West Indies.
Full Transcript
18/03/2025 COMP2611 – Data Structures DICTIONARIES University of the West Indies 1 Balancing Trees Definition A Perfectly Balanced tree is one where the sub-trees of each node are of the same heigh...
18/03/2025 COMP2611 – Data Structures DICTIONARIES University of the West Indies 1 Balancing Trees Definition A Perfectly Balanced tree is one where the sub-trees of each node are of the same height. A Not Perfectly Balanced tree is one where, for at least one node, the heights of the sub-trees differ by at most 1 In order to balance binary trees, nodes must be re-positioned without altering the character of the tree. Two Methods: 1. Right Rotation 2. Left Rotation University of the West Indies 2 1 18/03/2025 Balancing Trees Right Rotation about a node Algorithm: 1. Move left child into Node’s position 2. Move right child of Node’s former left child to become the Node’s new left child. 3. Move Node to become the right-child of the Node’s former left child NB Right rotation can only be done on a node with a left child. University of the West Indies 3 Balancing Trees 52 is our “Node” Right Rotate about 52 52 40 61 31 49 55 79 29 38 University of the West Indies 4 2 18/03/2025 Balancing Trees 52 40 61 40 52 31 49 55 79 31 49 61 29 38 29 38 55 79 Node’s Left-child is elevated to Node’s position. University of the West Indies 5 Balancing Trees 40 52 40 52 31 49 61 31 49 61 55 79 29 38 55 79 29 38 Right child of Node’s former left child becomes Node’s Left- Child. University of the West Indies 6 3 18/03/2025 Balancing Trees 40 52 40 31 49 61 31 52 29 38 55 79 61 29 38 49 55 79 Node becomes former left child’s Right-Child. University of the West Indies 7 Balancing Trees Right-Rotating 52 52 40 40 31 52 61 29 38 49 61 31 49 55 79 55 79 29 38 Before rotation After rotation University of the West Indies 8 4 18/03/2025 Balancing Trees Left Rotation about a node Algorithm: 1. Move right child into Node’s position 2. Move left child of Node’s former right child to become the Node’s new right child. 3. Move Node to become the left-child of Node’s former right child. NB Left rotation can only be done on a node with a right child. University of the West Indies 9 Balancing Trees 52 is our “Node” Left Rotate about 52 52 40 61 31 49 55 79 29 38 University of the West Indies 10 5 18/03/2025 Balancing Trees 52 40 61 52 61 31 49 55 79 40 55 79 29 38 31 49 29 38 Node’s Right-child is elevated to Node’s position. University of the West Indies 11 Balancing Trees 52 61 40 55 79 52 61 31 49 40 79 55 29 38 31 49 29 38 Left child of Node’s former Right child becomes Node’s Right-Child. University of the West Indies 12 6 18/03/2025 Balancing Trees 52 61 61 40 79 55 52 79 31 49 40 55 29 38 31 49 29 38 Node becomes former Right child’s Left-Child. University of the West Indies 13 Balancing Trees Left-Rotating 52 61 52 52 79 40 61 40 55 31 49 55 79 49 31 29 38 29 38 Before rotation After rotation University of the West Indies 14 7 18/03/2025 AVL TREES University of the West Indies 15 AVL Trees A balanced binary search tree. Named after their inventors, Adelson-Velskii and Landis, First dynamically balanced trees to be proposed. Not perfectly balanced - pairs of sibling sub-trees may differ in height by at most 1 University of the West Indies 16 8 18/03/2025 AVL Trees Definition: An AVL tree is a binary search tree which has the following properties: 1. The sub-trees of every node differ in height by at most one. 2. Every sub-tree is an AVL tree. You need to be careful with this definition: It permits some apparently unbalanced trees! For example: University of the West Indies 17 AVL Trees AVL tree? Yes Examination shows that each left sub-tree has a height 1 greater than each right sub-tree. Note: A single violation could render the entire tree unbalanced! University of the West Indies 18 9 18/03/2025 AVL Trees AVL tree? No Sub-tree with root 8 has height 4 and sub- tree with root 18 has height 2. AVL violation at nodes 12 and 8 University of the West Indies 19 AVL Trees Nodes Insertion and Deletion v Somewhat more complex than the ordinary BST. v Requires extra attribute, called the balance factor to each node. v Balancing factor indicates whether the tree is left-heavy, balanced or right-heavy. Heavy = height of sub-tree is 1 greater than the opposite sub-tree. If the balance would be destroyed by an insertion or deletion, a rotation is performed to correct the balance. University of the West Indies 20 10 18/03/2025 AVL Trees Example: A new item added to the left subtree of node 1, causes its height to become 2 greater than 2's right sub-tree (shown in green). A right- rotation is performed to correct the imbalance. University of the West Indies 21 AVL TREES University of the West Indies 22 11 18/03/2025 AVL Tree class AVLNode { private: int data; int balance; public: AVLNode* left; AVLNode* right; AVLNode ( ) { right = left = NULL; balance = 0; } AVLNode ( int va l) { data = val; balancet = 0; right = left = 0; } void setData ( int val ) { data = val; } void setHeight ( int ht ) { balance = ht; } int getData ( ) { return data; } int getBalance ( ) { return balance;} }; University of the West Indies 23 AVL Tree class AVLTree // To Start… from BST… { private: AVLNode *root; void insertHelper(AVLNode **, int ); string preOrderHelper( AVLNode * ) const; string inOrderHelper( AVLNode * ) const; string postOrderHelper( AVLNode * ) const; void deleteNode(AVLNode *, int ); public: AVLTree(){ root = 0; }; void insert( int ); string preOrder ( ) const; string inOrder ( ) const; string postOrder ( ) const; string remove ( int ); void removeMin(); }; University of the West Indies 24 12 18/03/2025 AVL Tree class AVLTree // New stuff…To be added… { private: AVLNode* rotateRight ( AVLNode* ); AVLNode* rotateLeft ( AVLNode* ); AVLNode* rotateDoubleRight ( AVLNode* ); AVLNode* rotateDoubleLeft ( AVLNode* ); AVLNode* rotateLeftRight ( AVLNode* ); AVLNode* rotateRightLeft ( AVLNode* ); int calcHeight ( AVLNode* ); int calcBalance ( AVLNode* ); public: }; University of the West Indies 25 AVL Tree University of the West Indies 26 13 18/03/2025 RED-BLACK TREES University of the West Indies 27 Red-Black Trees Definition A binary search tree with one extra attribute for each node: the colour, which is either red or black. We also need to keep track of the parent of each node, so that a red-black tree's node structure would be maintained Properties: 1. Every node is either red or black. 2. Every leaf (NULL) is black. 3. The root node is black. 4. If a node is red, then both its children must be black. 5. Every simple path from a “root node” to a descendant leaf contains the same number of black nodes. University of the West Indies 28 14 18/03/2025 Red-Black Trees Properties: All external nodes have the same black depth, which is defined as the number of black ancestors minus 1. Implies that on any path from the root to a leaf, red nodes cannot be parents or children of other red nodes. However, any number of black nodes may appear in a sequence. University of the West Indies 29 Red-Black Trees A basic red-black tree Note colour of leaves may appear to violate properties definition University of the West Indies 30 15 18/03/2025 Red-Black Trees Basic red-black tree with the sentinel nodes added. Implementations of the red-black tree algorithms will usually include the sentinel nodes as a convenient means of flagging that you have reached a leaf node. They are the NULL black nodes of property definition. University of the West Indies 31 Red-Black Trees The number of black nodes on any path from, but not including, a node x to a leaf is called the black-height of that node. Additions and deletions from red-black trees could destroy the red- black property, so rotations are normally required to restore it. Maintaining a red-black tree as new nodes are added or deleted primarily involves recoloring and rotation. Algorithm for ADDITION is as follows: University of the West Indies 32 16 18/03/2025 Red-Black Trees 1. Create a new node n to hold the value. 2. If the tree is empty, make n the root. Otherwise, go left or right, as with normal insertion into a binary search tree. 3. If you pass through a node m with both its children red, colour those children black and re-colour m to red. 4. At the appropriate leaf, add n as a red child to its parent. 6. If either of the steps that adds red links creates 2 consecutive red nodes, colour the first red node black and it’s parent red then rotate about the parent node to create a black node with 2 red children. 7. If the root is not black, re-colour it black. University of the West Indies 33 Red-Black Trees Example Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 Recall rules: 1. Enter nodes as in a normal BST – new nodes are coloured RED. 2. If you pass a Black node with TWO RED children, the Black node should be re-coloured RED and its two RED children re-coloured BLACK. 3. After the node is added, if there are two consecutive Red nodes, re-colour the Red parent to Black and the grand-parent Red. If the parent is a right-child of the grand-parent, perform a left-rotation with the grand-parent. (If left-child, perform right-rotation).After the re-colouring and rotation, this will result in a sub-tree with a Black sub-root and two Red children. 4. If needed, repeat step 3. 5. Finally, the root must be coloured Black. University of the West Indies 34 17 18/03/2025 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION Enter key 1 1 1 New node entered as RED but changed to black because it is the root. Add key 2: 1 Nothing else to do here – the RB tree is fine 2 University of the West Indies 35 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION 1 Add key 3: 1 2 2 3 3 Two consecutive Red nodes with 2 and 3. We must recolour the parent (2) BLACK and the grand-parent black node (1) RED and rotate the grand-parent (1). 2 1 3 University of the West Indies 36 18 18/03/2025 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION 4 2 Add key 4: 2 1 3 1 3 4 Passing a Black node (2) with two red children (1 and 3), means we must change the black node (2) RED and its two children (1 and 3) to BLACK. 2 The root node (2) is then reset to 1 3 BLACK. 4 University of the West Indies 37 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION 2 Add key 5: 2 1 3 1 3 4 4 5 5 Two consecutive Red nodes with 4 and 5. We must rotate their parent (3) and recolour the parent (3) 2 RED and the first red node (4) black. 1 4 3 5 University of the West Indies 38 19 18/03/2025 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION 2 Add key 6: 2 1 4 6 4 6 1 3 5 3 5 2 Passing a Black node (4) with two red children, means we change the black node (4) RED and its 1 4 two children (3 and 5) BLACK. 3 5 6 University of the West Indies 39 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION Add key 7: 2 2 4 1 4 1 5 3 6 3 6 5 7 7 Two consecutive Red nodes with 6 and 7. We must rotate their parent (5) and recolour the parent (5) RED and the first red node (6) black. University of the West Indies 40 20 18/03/2025 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION Add key 8: 2 2 1 4 1 4 3 6 3 6 8 5 7 5 7 8 Passing a Black node (6) with two red children, means we change the Now we have TWO consecutive black node (6) RED and its two Red nodes with 4 and 6. We children (5 and 7) BLACK. must recolour 4 (black) and 2 (red) then rotate the grand- parent (2). University of the West Indies 41 Red-Black Trees Example: Enter the following keys into a Red-black tree: 1, 2, 3, 4, 5, 6, 7, 8 SOLUTION 4 2 6 1 4 2 3 5 7 3 6 1 8 5 7 Now we have TWO consecutive 8 Red nodes with 4 and 6. We must recolour 4 (black) and 2 (red) then rotate the grand- parent (2). University of the West Indies 42 21 18/03/2025 Red-Black Trees Handling Special Cases… 1 1 1 2 3 2 2 1 3 3 3 2 Step 1: Step 2: Step 3: Step 4: Special Case Regularize the Perform Perform special case re-colouring rotation University of the West Indies 43 Red-Black Trees Delete A Node: 1. Nodes are deleted as defined by the Binary Search Tree 2. Colour of deleted node is retained for its replacement. University of the West Indies 44 22 18/03/2025 SPLAY TREES University of the West Indies 45 Splay Trees Self-adjusting binary search trees. Represents an attempt to combine the benefits of an AVL tree with a simple random binary search tree. AVL trees are always balanced. However, this means continually keeping track of the height of the sub-trees at each node. Splay tree does not store height data. Instead every time we access a node, we move the accessed node to the root using AVL rotations (insertions may also be rotated to the root). Sound a bit crazy… University of the West Indies 46 23 18/03/2025 Splay Trees …In practice it will result in a tree which is NOT well balanced, but a tree with very fast access times. Highly unbalanced trees may be formed from the insertions, but each access will tend to move towards fixing the problem. Benefits: Given a good splaying algorithm, search times will be fast. As there is no work in storing heights a splay tree can approach (and sometimes improve on) an AVL tree. Nodes that are frequently accessed will be very near to the root. This means for applications that frequently access the same nodes, search times will be especially fast. University of the West Indies 47 Splay Trees How to Splay? The simplest approach is to rotate an accessed node all the way to the root using single rotations. Problem: Improves the situation for some nodes, others end up further away from the root. We can obtain a better overall tree shape if we use double rotations as well. How… University of the West Indies 48 24 18/03/2025 Splay Trees How to Splay? If the accessed node is the inside node of its parent we use a double rotation (zig-zag or zag-zig ), but if it is an outside node we use a new strategy again involving two single rotations (zig- zig or zag-zag) : Zig = Right-Rotation Zag = Left-Rotation University of the West Indies 49 Splay Trees Example: Zig-Zag X is our accessed node and we want to rotate it so that it becomes the root of the tree (or sub-tree as the case may be). This is the same as a left right double rotation in an AVL tree. University of the West Indies 50 25 18/03/2025 Splay Trees University of the West Indies 51 Splay Trees EXAMPLE Insert the following keys into a splay tree: 1, 2, 4, 3, 5 Solution: Insert 1: 1 2 Insert 2: 1 2 1 Insert 4: 2 4 1 4 2 1 University of the West Indies 52 26 18/03/2025 Splay Trees EXAMPLE Insert the following keys into a splay tree: 1, 2, 4, 3, 5 Solution: Insert 3: 4 4 3 2 4 3 2 1 3 2 1 1 University of the West Indies 53 Splay Trees EXAMPLE Insert the following keys into a splay tree: 1, 2, 4, 3, 5 Solution: Insert 5: 3 3 5 4 2 5 2 3 5 1 4 1 2 4 1 University of the West Indies 54 27