Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition.

Slides:



Advertisements
Similar presentations
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Advertisements

Outline Scapegoat Trees ( O(log n) amortized time)
A balanced life is a prefect life.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Binary Search Trees1 Part-F1 Binary Search Trees   
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
Advanced Data Structures and Implementation Top-Down Splay Trees Top-Down Splay Trees Red-Black Trees Red-Black Trees Top-Down Red Black Trees Top-Down.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Balanced Search Trees Modified from authors’ slides.
COMP9024: Data Structures and Algorithms
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Search Trees.
Binary Search Trees < > =
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees < > = Binary Search Trees
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Ch. 8 Priority Queues And Heaps
Binary Search Trees < > =
Binary Search Trees < > =
Binary and Binomial Heaps
Binary Search Trees < > =
HEAPS.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Priority Queues Supports the following operations. Insert element x.
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Advanced Data Structures By: Nikhil Bhargava

Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition. –Insertion, Deletion and Search. –Performance. Binary Heaps. Binary Heaps. Binomial Trees. Binomial Trees. Binomial Heaps. Binomial Heaps. Red-Black Trees Red-Black Trees –Definition. –Addition and Deletion. Skip Lists. Skip Lists. 1/14/2016 Copyright © All Rights Reserved by author2

Introduction What’s the need of ADT? What’s the need of ADT? Simply store everything in a big array. Simply store everything in a big array. Be happy Be happy Problem Problem –Search for a number k in a set of N numbers Solution Solution –Store numbers in an array of size N. –Iterate through array until find k. –Number of checks Best Case : 1 (K=15) Best Case : 1 (K=15) Worst case : N (k=27) Worst case : N (k=27) Average Case : N/2 Average Case : N/2 1/14/2016 Copyright © All Rights Reserved by author3

Introduction Better solution Better solution –Store numbers in a Binary Search Tree (BST). –Search tree until find k. –Number of checks. Best case: 1 (k=15) Best case: 1 (k=15) Worst case: log 2 N (k=27) Worst case: log 2 N (k=27) Average case: (log 2 N) / 2 Average case: (log 2 N) / 2 1/14/2016 Copyright © All Rights Reserved by author4

Analysis What’s the difference b/w both the sols. What’s the difference b/w both the sols. –(N) vs. (log 2 N). 1/14/2016 Copyright © All Rights Reserved by author5

Analysis Assume Assume –N=1,000,000,000 1 billion (Walmart transactions in 100 days) 1 billion (Walmart transactions in 100 days) –1 GHz processor=10 9 cycles per second. Solution 1 (10 cycles per check) Solution 1 (10 cycles per check) –Worst case: 1 billion checks = 10 seconds Solution 2 (100 cycles per check) Solution 2 (100 cycles per check) –Worst case: 30 checks = seconds 1/14/2016 Copyright © All Rights Reserved by author6

Moral of the story!!! Choosing most appropriate data structure makes the system efficient, fast and cost effective. Choosing most appropriate data structure makes the system efficient, fast and cost effective. Appropriate data structures ease design and improve performance. Appropriate data structures ease design and improve performance. Designing appropriate data structure and finding associated algorithms for a problem is a major challenge. Designing appropriate data structure and finding associated algorithms for a problem is a major challenge. 1/14/2016 Copyright © All Rights Reserved by author7

Binary Search Tree (BST) A BST is a binary tree storing keys (or key- value entries) at its internal nodes and satisfying the following property: A BST is a binary tree storing keys (or key- value entries) at its internal nodes and satisfying the following property: –Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) ≤ key(v) ≤ key(w) External nodes do not store items. External nodes do not store items. 1/14/2016 Copyright © All Rights Reserved by author8

Binary Search Tree (BST) An in-order traversal of a binary search trees visits the keys in increasing order. An in-order traversal of a binary search trees visits the keys in increasing order. 1/14/2016 Copyright © All Rights Reserved by author9

BST – Search Operation To search for a key k, we trace a downward path starting at the root, To search for a key k, we trace a downward path starting at the root, The next node visited depends on the outcome of the comparison of k with the key of the current node. The next node visited depends on the outcome of the comparison of k with the key of the current node. If we reach a leaf, the key is not found and we return null. If we reach a leaf, the key is not found and we return null. 1/14/2016 Copyright © All Rights Reserved by author10

BST – Search Operation Algorithm TreeSearch(k, v) if T.isExternal (v) return v if k < key(v) return TreeSearch(k, T.left(v)) else if k = key(v) return v else { k > key(v) } return TreeSearch(k, T.right(v)) 1/14/2016 Copyright © All Rights Reserved by author11

BST – Search Operation For e.g find (4) For e.g find (4) Call TreeSearch(4,root) 1/14/2016 Copyright © All Rights Reserved by author12

BST – Insert Operation To perform operation insert(k, o), we search for key k (using TreeSearch) To perform operation insert(k, o), we search for key k (using TreeSearch) Assume k is not already in the tree, and let w be the leaf reached by the Search. Assume k is not already in the tree, and let w be the leaf reached by the Search. We insert k at node w and expand w into an internal node. We insert k at node w and expand w into an internal node. For e.g. insert (5) For e.g. insert (5) 1/14/2016 Copyright © All Rights Reserved by author13

BST – Insert Operation 1/14/2016 Copyright © All Rights Reserved by author14

BST – Delete Operation To perform operation remove(k), we search for key k. To perform operation remove(k), we search for key k. Assume key k is in the tree, and let v be the node storing k. Assume key k is in the tree, and let v be the node storing k. If node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent. If node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parent. For e.g. remove (4) For e.g. remove (4) 1/14/2016 Copyright © All Rights Reserved by author15

BST – Delete Operation 1/14/2016 Copyright © All Rights Reserved by author16

BST – Delete Operation We consider the case where the key k to be removed is stored at a node v whose children are both internal We consider the case where the key k to be removed is stored at a node v whose children are both internal –we find the internal node w that follows v in an inorder traversal. –we copy key(w) into node v –we remove node w and its left child z (which must be a leaf) by means of operation removeExternal(z). –Add key to inorder successor of w. –For e.g remove (3) 1/14/2016 Copyright © All Rights Reserved by author17

BST – Delete Operation 1/14/2016 Copyright © All Rights Reserved by author18

BST – Delete Operation 1/14/2016 Copyright © All Rights Reserved by author19

BST - Performance Consider a dictionary with n items implemented by means of a binary search tree of height h. Consider a dictionary with n items implemented by means of a binary search tree of height h. –the space used is O(n). –methods find, insert and remove take O(h) time. The height h is O(n) in the worst case and O(log n) in the best case. The height h is O(n) in the worst case and O(log n) in the best case. 1/14/2016 Copyright © All Rights Reserved by author20

BST - Performance 1/14/2016 Copyright © All Rights Reserved by author21

Binary Heap: Definition Binary heap. Binary heap. –Almost complete binary tree. filled on all levels, except last, where filled from left to right. filled on all levels, except last, where filled from left to right. –Min-heap ordered. every child greater than (or equal to) parent. every child greater than (or equal to) parent /14/2016 Copyright © All Rights Reserved by author22

Binary Heap: Properties Properties. Properties. –Min element is in root. –Heap with N elements has height =  log 2 N  N = 14 Height = 3 1/14/2016 Copyright © All Rights Reserved by author23

Binary Heaps: Array Impl. Implementing binary heaps. Implementing binary heaps. –Use an array: no need for explicit parent or child pointers. Parent(i) =  i/2  Parent(i) =  i/2  Left(i) = 2i Left(i) = 2i Right(i) = 2i + 1 Right(i) = 2i /14/2016 Copyright © All Rights Reserved by author 24

Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence next free slot 1/14/2016 Copyright © All Rights Reserved by author25

Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence swap with parent 1/14/2016 Copyright © All Rights Reserved by author26

Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence swap with parent 1/14/2016 Copyright © All Rights Reserved by author27

Binary Heap: Insertion Insert element x into heap. Insert element x into heap. –Insert into next available slot. –Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence Peter principle: nodes rise to level of incompetence –O(log N) operations stop: heap ordered 1/14/2016 Copyright © All Rights Reserved by author28

Binary Heap: Decrease Key Decrease key of element x to k. Decrease key of element x to k. –Bubble up until it's heap ordered. –O(log N) operations /14/2016 Copyright © All Rights Reserved by author29

Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted /14/2016 Copyright © All Rights Reserved by author30

Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted /14/2016 Copyright © All Rights Reserved by author31

Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted exchange with left child 1/14/2016 Copyright © All Rights Reserved by author32

Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted exchange with right child 1/14/2016 Copyright © All Rights Reserved by author33

Binary Heap: Delete Min Delete minimum element from heap. Delete minimum element from heap. –Exchange root with rightmost leaf. –Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted. power struggle principle: better subordinate is promoted. –O(log N) operations stop: heap ordered 1/14/2016 Copyright © All Rights Reserved by author34

Binary Heap: Heapsort Heapsort. Heapsort. –Insert N items into binary heap. –Perform N delete-min operations. –O(N log N) sort. –No extra storage. 1/14/2016 Copyright © All Rights Reserved by author35

H1H1 H2H Binary Heap: Union Combine two binary heaps H 1 and H 2 into one heap. Combine two binary heaps H 1 and H 2 into one heap. No easy solution. No easy solution. –  (N) operations apparently required. Can support fast union with fancier heaps. Can support fast union with fancier heaps. 1/14/2016 Copyright © All Rights Reserved by author36

Binomial Tree Binomial tree. Binomial tree. –Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 1/14/2016 Copyright © All Rights Reserved by author37

Binomial Tree Useful properties of order k binomial tree B k Useful properties of order k binomial tree B k –Number of nodes = 2 k. –Height = k. –Degree of root = k. –Deleting root yields binomial trees B k-1, …, B 0. Proof. Proof. –By induction on k. B0B0 B1B1 B2B2 B3B3 B4B4 B1B1 BkBk B k +1 B2B2 B0B0 1/14/2016 Copyright © All Rights Reserved by author38

Binomial Tree A property useful for naming the data structure. A property useful for naming the data structure. –B k has nodes at depth i. B4B4 depth 2 depth 3 depth 4 depth 0 depth 1 1/14/2016 Copyright © All Rights Reserved by author39

Binomial Heap Binomial heap (Vuillemin, 1978). Binomial heap (Vuillemin, 1978). –Sequence of binomial trees that satisfy binomial heap property. each tree is min-heap ordered. each tree is min-heap ordered. 0 or 1 binomial tree of order k. 0 or 1 binomial tree of order k. B4B4 B0B0 B1B /14/2016 Copyright © All Rights Reserved by author 40

Binomial Heap: Implementation Implementation. Implementation. –Represent trees using left-child, right sibling pointers. three links per node (parent, left, right). three links per node (parent, left, right). –Roots of trees connected with singly linked list. degrees of trees strictly decreasing from left to right. degrees of trees strictly decreasing from left to right heap 29 Leftist Power-of-2 Heap Binomial Heap Parent Left Right 1/14/2016 Copyright © All Rights Reserved by author 41

Binomial Heap: Properties Properties of N-node binomial heap. Properties of N-node binomial heap. –Min key contained in root of B 0, B 1,..., B k. –Contains binomial tree B i iff b i = 1 where b n  b 2 b 1 b 0 is binary representation of N. –At most  log 2 N  + 1 binomial trees. –Height   log 2 N . B4B4 B0B0 B1B N = 19 # trees = 3 height = 4 binary = /14/2016 Copyright © All Rights Reserved by author 42

Binomial Heap: Union Create heap H that is union of heaps H' and H''. Create heap H that is union of heaps H' and H''. –"Mergeable heaps." –Easy if H' and H'' are each order k binomial trees. connect roots of H' and H.'' connect roots of H' and H.'' choose smaller key to be root of H. choose smaller key to be root of H. H'' H' 1/14/2016 Copyright © All Rights Reserved by author43

Binomial Heap: Union = /14/2016 Copyright © All Rights Reserved by author 44

Binomial Heap: Union /14/2016 Copyright © All Rights Reserved by author45

Binomial Heap: Union /14/2016 Copyright © All Rights Reserved by author46

/14/2016 Copyright © All Rights Reserved by author47

/14/2016 Copyright © All Rights Reserved by author48

/14/2016 Copyright © All Rights Reserved by author 49

/14/2016 Copyright © All Rights Reserved by author 50

Binomial Heap: Union Create heap H that is union of heaps H' and H''. Create heap H that is union of heaps H' and H''. –Analogous to binary addition. Running time O(log N) Running time O(log N) –Proportional to number of trees in root lists  2(  log 2 N  + 1) = 26 1/14/2016 Copyright © All Rights Reserved by author 51

H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. Delete node with minimum key in binomial heap H. –Find root x with min key in root list of H, and delete –H'  broken binomial trees –H  Union(H', H) Running time O(log N) Running time O(log N) 1/14/2016 Copyright © All Rights Reserved by author52

Binomial Heap: Delete Min Delete node with min key in binomial heap H. Delete node with min key in binomial heap H. –Find root x with min key in root list of H, and delete –H'  broken binomial trees –H  Union(H', H) Running time O(log N) Running time O(log N) H H' 1/14/2016 Copyright © All Rights Reserved by author 53

x H Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. Decrease key of node x in binomial heap H. –Suppose x is in binomial tree B k. –Bubble node x up the tree if x is too small. Running time O(log N) Running time O(log N) –Proportional to depth of node x   log 2 N . depth = 3 1/14/2016 Copyright © All Rights Reserved by author54

Binomial Heap: Delete Delete node x in binomial heap H. Delete node x in binomial heap H. –Decrease key of x to - . –Delete min. Running time O(log N) Running time O(log N) 1/14/2016 Copyright © All Rights Reserved by author55

Binomial Heap: Insert Insert a new node x into binomial heap H. Insert a new node x into binomial heap H. –H'  MakeHeap(x) –H  Union(H', H) Running time. O(log N) Running time. O(log N) H x H' 1/14/2016 Copyright © All Rights Reserved by author56

Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. Insert a new node x into binomial heap H. –If N = , then only 1 steps. –If N = , then only 2 steps. –If N = , then only 3 steps. –If N = , then only 4 steps x 1/14/2016 Copyright © All Rights Reserved by author57

Binomial Heap: Sequence of Inserts Inserting 1 item can take  (log N) time. Inserting 1 item can take  (log N) time. –If N = , then log 2 N steps. But, inserting sequence of N items takes O(N) time! But, inserting sequence of N items takes O(N) time! –(N/2)(1) + (N/4)(2) + (N/8)(3) +...  2N –Amortized analysis. –Basis for getting most operations down to constant time. operations down to constant time. 1/14/2016 Copyright © All Rights Reserved by author58

Red-Black Tree Popular alternative to the BST tree (Why??). Popular alternative to the BST tree (Why??). Operations take O(log N) time in worst case. Operations take O(log N) time in worst case. Height is at most 2log(N+1). Height is at most 2log(N+1). A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. The root is black. The root is black. If node is red, its children must be black. If node is red, its children must be black. Every path from a node to a null reference must contain the same number of black nodes. Every path from a node to a null reference must contain the same number of black nodes. Basic operations to conform with rules are color changes and tree rotations. Basic operations to conform with rules are color changes and tree rotations. 1/14/2016 Copyright © All Rights Reserved by author59

Theorem 1 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node. 1/14/2016 Copyright © All Rights Reserved by author60 Red-Black Tree

Theorem 2 – In a red-black tree, no path from any node N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem1, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path. 1/14/2016 Copyright © All Rights Reserved by author61 Red-Black Tree

Theorem 3 – A red-black tree with n internal nodes has height h <= 2 log(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 1, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 log(n + 1) >= h/2 2log(n + 1) >= h 1/14/2016 Copyright © All Rights Reserved by author62 Red-Black Tree

Bottom-Up Insertion Cases: Cases: –0: X is the root – color it black. –1: Both parent and uncle are red – color parent and uncle black, color grandparent red, point X to grandparent, check new situation. –2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children – color grandparent red, color X black, rotate left on parent, rotate right on grandparent. –3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children – color parent black, color grandparent red, rotate right on grandparent. 1/14/2016 Copyright © All Rights Reserved by author63

Top-Down Red-Black Trees In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. So, T-Down insertion can be done iteratively which is generally faster. So, T-Down insertion can be done iteratively which is generally faster. Insertion is always done as a leaf (as in ordinary BST insertion). Insertion is always done as a leaf (as in ordinary BST insertion). 1/14/2016 Copyright © All Rights Reserved by author64

Process On the way down, when we see a node X that has two red children, we make X red and its two children black. On the way down, when we see a node X that has two red children, we make X red and its two children black. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case. 1/14/2016 Copyright © All Rights Reserved by author65

Example: Insert Two red children 1/14/2016 Copyright © All Rights Reserved by author66

Example (Cont.) flip colors - two red nodes 1/14/2016 Copyright © All Rights Reserved by author67

Example (Cont.): Do a single rotation /14/2016 Copyright © All Rights Reserved by author68

Example (Cont.): Now Insert /14/2016 Copyright © All Rights Reserved by author69

Important note Since the parent of the newly inserted node was black, we are done. Since the parent of the newly inserted node was black, we are done. Had the parent of the inserted node been red, one more rotation would have had to be performed. Had the parent of the inserted node been red, one more rotation would have had to be performed. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees. 1/14/2016 Copyright © All Rights Reserved by author70

Top-Down Deletions Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. Nodes with two children are never removed. Their contents are just replaced. Nodes with two children are never removed. Their contents are just replaced. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is black, its removal will violate property. If the node to be deleted is black, its removal will violate property. The solution is to ensure that any node to be deleted is red. The solution is to ensure that any node to be deleted is red. 1/14/2016 Copyright © All Rights Reserved by author71

Deterministic Skip Lists A probabilistically balanced linked list. A probabilistically balanced linked list. Invented in 1986 by William Pugh. Invented in 1986 by William Pugh. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them. 1/14/2016 Copyright © All Rights Reserved by author72

Skip List 3 6 NIL NIL xtra pointers every eighth item - full structure skip list - same link distribution, random choice 1/14/2016 Copyright © All Rights Reserved by author73

Search time In the deterministic version (a-d): In the deterministic version (a-d): –in a, we need to check at most n nodes –in b, at most  n/2  +1 nodes –in c, at most  n/4  +2 nodes –in general, at most  log N  nodes Efficient search, but impractical insertion and deletion. Efficient search, but impractical insertion and deletion. Not a real time data structure. Not a real time data structure. Probabilistic in nature. Probabilistic in nature. 1/14/2016 Copyright © All Rights Reserved by author74

Levels A node with k forward pointers is called a level k node. A node with k forward pointers is called a level k node. If every (2 i )th node has a pointer 2 i nodes ahead, they have the following distribution: If every (2 i )th node has a pointer 2 i nodes ahead, they have the following distribution: levelpercent …… 1/14/2016 Copyright © All Rights Reserved by author75

Central idea in skip lists Choose levels of nodes randomly, but in the same proportions (as in e). Choose levels of nodes randomly, but in the same proportions (as in e). A node’s i th forward pointer, points to the next node of level i or higher. A node’s i th forward pointer, points to the next node of level i or higher. Insertions and deletions require only local modifications. Insertions and deletions require only local modifications. A node’s level never changes after first being chosen. A node’s level never changes after first being chosen. 1/14/2016 Copyright © All Rights Reserved by author76

Insertion To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in deterministic skip list). To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in deterministic skip list). 1/14/2016 Copyright © All Rights Reserved by author77

Data Structure Selection in Programming Tough question (Agree or not !!!). Tough question (Agree or not !!!). Comes with practice and experience. Comes with practice and experience. Remember the golden rule. Remember the golden rule. Data Structure will affect the runtime of your algorithm, but not the result Pick the data structure depending on the problem Pick the data structure depending on the problem –time vs. space tradeoff (e.g. hash tables provide fast search but need many more slots than entries). 1/14/2016 Copyright © All Rights Reserved by author78

Data Structure Selection in Programming Think about these questions before deciding on the right data structure Think about these questions before deciding on the right data structure –What are my end objectives? –What features does the data structure provide? –what are costs (time, space, etc.) associated with them? (Free lunch is only available in a mouse trap!!) – How well does this combination suit the particular algorithm I have in mind? 1/14/2016 Copyright © All Rights Reserved by author79

Some tips… At least 90% of the data structures used in totality are simple arrays. At least 90% of the data structures used in totality are simple arrays. The rest are mainly linked lists or binary trees. The rest are mainly linked lists or binary trees. Very rarely is it worth using a hash or a red black tree. Very rarely is it worth using a hash or a red black tree. If you need to find something with equality only, then hash. If you need to find something with equality only, then hash. If you need to find a range of something (e.g. where ‘x’ between ‘a’ and ‘b’ or where ‘y’ >= ‘z’) then use a tree or skiplist. If you need to find a range of something (e.g. where ‘x’ between ‘a’ and ‘b’ or where ‘y’ >= ‘z’) then use a tree or skiplist. 1/14/2016 Copyright © All Rights Reserved by author80

Some tips… If you need to order a list by some kind of importance and modify the list on the fly, then a priority queue is wanted. If you need to order a list by some kind of importance and modify the list on the fly, then a priority queue is wanted. If you need to solve problems involving some sort of web like structure, then a graph is often wanted. If you need to solve problems involving some sort of web like structure, then a graph is often wanted. If you want to access data by content rather than by id number, there’s nothing like a hash table. If you want to access data by content rather than by id number, there’s nothing like a hash table. If you want to keep data sorted, despite arbitrary inserts and deletes, you are looking at a red black tree. If you want to keep data sorted, despite arbitrary inserts and deletes, you are looking at a red black tree. 1/14/2016 Copyright © All Rights Reserved by author81

References [1] Advanced Data Structures by Goodrich and Tamassia, 2 nd edition. and Tamassia, 2 nd edition. [2] Introduction to Algorithms by Cormen et. al., 2 nd edition. et. al., 2 nd edition. [3] Binary and Binomial Heaps, Princeton university, notes by Kevin Wayne. university, notes by Kevin Wayne. [4] Lecture notes, Dr. S.N Maheshwari, CSE- IIT Delhi. IIT Delhi. 1/14/2016 Copyright © All Rights Reserved by author82

Questions 1/14/ Copyright © All Rights Reserved by author

1/14/ Copyright © All Rights Reserved by author