Download presentation
Presentation is loading. Please wait.
1
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf (ab_sarraf@aut.ac.ir) Search Structures
2
Dept. of Computer Eng. & IT, Amirkabir University of Technology Overview Trie Balanced BST AVL Red Black Tree
3
Dept. of Computer Eng. & IT, Amirkabir University of Technology Trie Special case of tree Applicable when Key C can be decomposed into a sequence of subkeys C 1,C 2, … C n Redundancy exists between subkeys Approach Store subkey at each node Path through trie yields full key C3C3 C1C1 C2C2 C4C4 C3C3
4
Dept. of Computer Eng. & IT, Amirkabir University of Technology Useful for searching strings String decomposes into sequence of letters Example “ART” “A” “R” “T” Can be very fast Less overhead than hashing May reduce memory Exploiting redundancy May require more memory Explicitly storing substrings S A R TE “ART”
5
Dept. of Computer Eng. & IT, Amirkabir University of Technology Types of Trie Standard Single character per node Compressed Eliminating chains of nodes Compact Stores indices into original string(s) Suffix Stores all suffixes of string
6
Dept. of Computer Eng. & IT, Amirkabir University of Technology Standard Trie Approach Each node (except root) is labeled with a character Children of node are ordered (alphabetically) Paths from root to leaves yield all input strings
7
Dept. of Computer Eng. & IT, Amirkabir University of Technology Standard Trie Example For strings { a, an, and, any, at }
8
Dept. of Computer Eng. & IT, Amirkabir University of Technology Standard Trie Example For strings { bear, bell, bid, bull, buy, sell, stock, stop }
9
Dept. of Computer Eng. & IT, Amirkabir University of Technology Standard Tries Node structure Value between 1…m Reference to m children Array or linked list Example Class Node { Letter value; // Letter V = { V 1, V 2, … V m } Node child[ m ]; }
10
Dept. of Computer Eng. & IT, Amirkabir University of Technology Standard Tries Efficiency Uses O(n) space Supports search / insert / delete in O(d m) time For n total size of strings indexed by trie d length of the parameter string m size of the alphabet
11
Dept. of Computer Eng. & IT, Amirkabir University of Technology Insert words into trie Each leaf stores occurrences of word in the text
12
Dept. of Computer Eng. & IT, Amirkabir University of Technology Compressed Trie
13
Dept. of Computer Eng. & IT, Amirkabir University of Technology AVL An AVL Tree is a binary search tree such that: for every internal node v, the heights of the children of v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes.
14
Dept. of Computer Eng. & IT, Amirkabir University of Technology Height of an AVL Tree Property: The height of an AVL tree storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. n(1) = 1 and n(2) = 2 For h > 2, an AVL tree of height h contains at least a root node, o ne AVL subtree of height h-1, and one AVL subtree of height h-2, so n(h) = 1 + n(h-1) + n(h-2) Since n(h-1) > n(h-2), we have n(h) > 2n(h-2), and so n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(h-6), … (by induction), n(h) > 2 i n(h-2i) Solving the base case we get n(h) > 2 h/2-1 h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) 3 4 n(1) n(2)
15
Dept. of Computer Eng. & IT, Amirkabir University of Technology Insertion in an AVL Tree Insertion is as in a binary search tree: always done by expanding an external node 44 1778 325088 4862 54 44 1778 325088 4862 before insertion after insertion Example: insert 54 imbalance
16
Dept. of Computer Eng. & IT, Amirkabir University of Technology Imbalance after Insertion Let w be the inserted node, z be the first unbalanced ancestor of w, y be the child of z with higher height (must be an ancestor of w), x be the child of y with higher height (must be an ancestor of w; can be equal to x). 44 1778 325088 4862 54 w x y z
17
Dept. of Computer Eng. & IT, Amirkabir University of Technology Trinode Restructuring Assign names a, b, c to nodes x, y, z according to inorder traversal. Perform the rotations needed to make b the topmost node of the three. b=y a=z c=x T0T0 T1T1 T2T2 T3T3 b=y a=z c=x T0T0 T1T1 T2T2 T3T3 case 1: single rotation
18
Dept. of Computer Eng. & IT, Amirkabir University of Technology Trinode Restructuring b=y c=z a=x T3T3 T2T2 T1T1 T0T0 b=y a=x c=z T0T0 T1T1 T2T2 T3T3 symmetric case
19
Dept. of Computer Eng. & IT, Amirkabir University of Technology Trinode Restructuring c=y b=x a=z T0T0 T1T1 T2T2 T3T3 b=x c=ya=z T0T0 T1T1 T2T2 T3T3 case 2: double rotation
20
Dept. of Computer Eng. & IT, Amirkabir University of Technology Trinode Restructuring a=y b=x c=z T3T3 T2T2 T1T1 T0T0 b=x c=za=y T0T0 T1T1 T2T2 T3T3 symmetric case
21
Dept. of Computer Eng. & IT, Amirkabir University of Technology Insertion Example 88 44 17 78 32 50 48 62 2 4 1 1 2 2 3 1 54 1 T 0 T 1 T 2 T 3 x y z unbalanced......balanced T 1
22
Dept. of Computer Eng. & IT, Amirkabir University of Technology Removal in an AVL Tree Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent w may cause an imbalance. 44 17 783250 88 48 62 54 44 17 7850 88 48 62 54 Example: delete 32 w
23
Dept. of Computer Eng. & IT, Amirkabir University of Technology 44 17 7850 88 48 62 54 w x y z Imbalance after Removal Let w be the parent of the removed node, z be the first unbalanced ancestor of w, y be the child of z with higher height (is now not an ancestor of w), x be the child of y with higher height, if heights are different, or the child of y on the same side as y, if heights are equal.
24
Dept. of Computer Eng. & IT, Amirkabir University of Technology Rebalancing after a Removal Assign names a, b, c to nodes x, y, z according to inorder traversal. Perform rotations to make b the topmost of the three. As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached. 44 17 7850 88 48 62 54 w c=x b=y a=z 44 17 78 5088 48 62 54
25
Dept. of Computer Eng. & IT, Amirkabir University of Technology Repeated Rebalancing 44 1778 325088 4862 54 25 14 93 44 1778 325088 4862 54 25 93 w=z y x
26
Dept. of Computer Eng. & IT, Amirkabir University of Technology Repeated Rebalancing 44 25 78 325088 4862 54 25 93 17
27
Dept. of Computer Eng. & IT, Amirkabir University of Technology Running Times for AVL Trees Finding a value takes O(log n) height of tree is O(log n), no restructures needed Insertion takes O(log n) initial find is O(log n) single restructuring up the tree, maintaining heights is O(log n) Removal takes O(log n) initial find is O(log n) (repeated) restructuring up the tree, maintaining heights is O(log n)
28
Dept. of Computer Eng. & IT, Amirkabir University of Technology Red-Black Trees Red-black trees: Binary search trees augmented with node color Operations designed to guarantee that the height h = O(lg n) First: describe the properties of red-black trees Then: prove that these guarantee h = O(lg n) Finally: describe operations on red-black trees
29
Dept. of Computer Eng. & IT, Amirkabir University of Technology Red-Black Properties The red-black properties: 1. Every node is either red or black 2.Every leaf (NULL pointer) is black Note: this means every “real” node has 2 children 3.If a node is red, both children are black Note: can’t have 2 consecutive reds on a path 4.Every path from node to descendent leaf contains the same num ber of black nodes 5.The root is always black
30
Dept. of Computer Eng. & IT, Amirkabir University of Technology Red-Black Trees Put example on board and verify properties: 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same num ber of black nodes 5. The root is always black black-height: # black nodes on path to leaf Label example with h and bh values
31
Dept. of Computer Eng. & IT, Amirkabir University of Technology A Red-Black Tree with NULLs shown Black-Height of the tree (the root) = 3 Black-Height of node “X” = 2 X
32
Dept. of Computer Eng. & IT, Amirkabir University of Technology A Red-Black Tree with Black-Height = 3
33
Dept. of Computer Eng. & IT, Amirkabir University of Technology Black Height of the tree? Black Height of X? X
34
Dept. of Computer Eng. & IT, Amirkabir University of Technology Height of Red-Black Trees What is the minimum black-height of a node with height h? A: a height-h node has black-height h/2 Theorem: A red-black tree with n internal nodes has height h 2 lg(n + 1) How do you suppose we’ll prove this?
35
Dept. of Computer Eng. & IT, Amirkabir University of Technology RB Trees: Proving Height Bound Prove: n-node RB tree has height h 2 lg(n+1) Claim: A subtree rooted at a node x contains at least 2 bh(x) - 1 internal nodes Proof by induction on height h Base step: x has height 0 (i.e., NULL leaf node) What is bh(x)?
36
Dept. of Computer Eng. & IT, Amirkabir University of Technology RB Trees: Proving Height Bound Prove: n-node RB tree has height h 2 lg(n+1) Claim: A subtree rooted at a node x contains at least 2 bh(x) - 1 internal nodes Proof by induction on height h Base step: x has height 0 (i.e., NULL leaf node) What is bh(x)? A: 0 So…subtree contains 2 bh(x) - 1 = 2 0 - 1 = 0 internal nodes (TRUE)
37
Dept. of Computer Eng. & IT, Amirkabir University of Technology RB Trees: Proving Height Bound Inductive proof that subtree at node x contains at least 2 bh(x) - 1 internal nodes Inductive step: x has positive height and 2 children Each child has black-height of bh(x) or bh(x)-1 (Why?) The height of a child = (height of x) - 1 So the subtrees rooted at each child contain at least 2 bh(x) - 1 - 1 internal nodes Thus subtree at x contains (2 bh(x) - 1 - 1) + (2 bh(x) - 1 - 1) + 1 = 22 bh(x)-1 - 1 = 2 bh(x) - 1 nodes
38
Dept. of Computer Eng. & IT, Amirkabir University of Technology RB Trees: Proving Height Bound Thus at the root of the red-black tree: n 2 bh(root) - 1(Why?) n 2 h/2 - 1(Why?) lg(n+1) h/2(Why?) h 2 lg(n + 1)(Why?) Thus h = O(lg n)
39
Dept. of Computer Eng. & IT, Amirkabir University of Technology RB Trees: Worst-Case Time So we’ve proved that a red-black tree has O(lg n) height Corollary: These operations take O(lg n) time: Minimum(), Maximum() Successor(), Predecessor() Search() Insert() and Delete(): Will also take O(lg n) time But will need special care since they modify tree
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.