CS102 Binary Search Trees

Similar documents
Lecture 6: Analysis of Algorithms (CS )

Balanced Binary Search Trees. Victor Gao

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Trees. (Trees) Data Structures and Programming Spring / 28

A set of nodes (or vertices) with a single starting point

Lecture: Analysis of Algorithms (CS )

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Analysis of Algorithms

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

COMP Analysis of Algorithms & Data Structures

Algorithms. AVL Tree

COMP Analysis of Algorithms & Data Structures

Binary Search Trees. Analysis of Algorithms

Binary Trees, Binary Search Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CSCI Trees. Mark Redekopp David Kempe

Properties of red-black trees

CMSC 341 Lecture 15 Leftist Heaps

Lecture 23: Binary Search Trees

Binary Trees. Recursive definition. Is this a binary tree?

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

CMSC 341 Lecture 15 Leftist Heaps

Algorithms. Deleting from Red-Black Trees B-Trees

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Balanced Binary Search Trees

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Chapter 22 Splay Trees

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

CS350: Data Structures Red-Black Trees

Data Structures Lesson 7

Section 4 SOLUTION: AVL Trees & B-Trees

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

Search Trees. Undirected graph Directed graph Tree Binary search tree

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Binary Trees and Binary Search Trees

Binary Search Trees, etc.

CS 171: Introduction to Computer Science II. Binary Search Trees

CSC148 Week 7. Larry Zhang

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

COMP Analysis of Algorithms & Data Structures

CISC 235: Topic 4. Balanced Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CS 380 ALGORITHM DESIGN AND ANALYSIS

Self-Balancing Search Trees. Chapter 11

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS-301 Data Structure. Tariq Hanif

CS521 \ Notes for the Final Exam

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Section 1: True / False (1 point each, 15 pts total)

CMSC 341 Leftist Heaps

Chapter 5. Binary Trees

Exam Data structures DAT036/DAT037/DIT960

Balanced Search Trees. CS 3110 Fall 2010

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Fall, 2015 Prof. Jungkeun Park

Final Examination CSE 100 UCSD (Practice)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CS 315 Data Structures mid-term 2

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Part 2: Balanced Trees

CMSC 341. Binary Search Trees CMSC 341 BST

SFU CMPT Lecture: Week 9

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Augmenting Data Structures

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Priority Queues (Heaps)

CS 241 Analysis of Algorithms

void insert( Type const & ) void push_front( Type const & )

Spring 2018 Mentoring 8: March 14, Binary Trees

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Red-black trees (19.5), B-trees (19.8), trees

COMP : Trees. COMP20012 Trees 219

Advanced Set Representation Methods

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Binary Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Trees. Prof. Dr. Debora Weber-Wulff

Data Structures and Algorithms

CSE 100 Advanced Data Structures

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Programming Abstractions

CS 350 : Data Structures Binary Search Trees

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Transcription:

CS102 Binary Search Trees Prof Tejada 1

To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one major organizational property (the binary search tree property): Let X be a Node in a binary search tree Every Node X is comparable to every other Node via a field we call the key (for now, "data" is the key) If Y is a Node in the left subtree of X: key[y] <= key[x] If keys must be unique, then we have key[y] < key[x] You can implement your BST to enforce this We will assume that this is the case here If Y is a Node in the right subtree of X: key[y] > key[x] 9

Binary Search Trees If Y is a Node in the left subtree of X: key[y] < key[x] If Y is a Node in the right subtree of X: key[y] > key[x] 50 60 70 Root 30 58 80 46 77 10

Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 60 Root 50 70 30 58 80 46 77 11

Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left Current 50 60 70 Root 30 58 80 46 77 12

Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 60 Root 58 == 60? No 58 < 60? Yes, go left Current 50 70 58 == 50? No 58 < 50? No 58 > 50? Yes, go right 30 58 80 46 77 13

Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Root Ex: find 58 Start at root 60 58 == 60? No 58 < 60? Yes, go left 58 == 50? No 58 < 50? No 58 > 50? Yes, go right 58 == 58? Yes 30 46 50 Current 58 70 77 80 14

Binary Search Tree Search What will our search algorithm be? Start at the root Is the current node what we want? If yes, we re done If no... If the key we want is less than the key of the current node, search the left subtree Otherwise, search the right subtree Should our solution be recursive or iterative? 15

Binary Search Tree Search Iterative search bool BinaryTree<T>::search(const T& key) { //start at the root BTNode<T>* current = this->root; while(current!= NULL) { if (current->key == key) { return true; //found the value } else if (key < current->key) { //search the left subtree current = current->left; } else { //search the right subtree current = current->right; } } return false; //could not find the value } 16

} Binary Search Tree Search Recursive search //public helper function bool BinaryTree<T>::search(const T& key) { return search(root, key); //start at the root } //private recursive function bool BinaryTree<T>::search(BTNode<T>* node, const T& key) { if (node == NULL current->key == key) { //found or couldn t find value return (node!= NULL); } else if (key < node->key) { //search the left subtree return search(node->left, key); } else { //search the right subtree return search(node->right, key); } Base Case Recursive Case 1 Recursive Case 2 17

Binary Search Tree Min/Max How would you find the minimum/maximum keys in a binary search tree? Root 60 50 70 30 58 80 46 77 18

Binary Search Tree Min/Max What will our findmin algorithm be? Start at the root Does the current Node have a left child? If yes, go to the left child If no, we re at the minimum Start at the root What will our findmax algorithm be? Does the current Node have a right child? If yes, go to the right child If no, we re at the maximum 19

Binary Search Tree Min/Max T& BinaryTree<T>::getMinimum() { if (root == NULL) { throw runtime_error("tree is empty!"); } //keep going left until we can t anymore BTNode<T> *current = this->root; while(current->left!= NULL) { current = current->left; } return current->key; } 20

Binary Search Tree Traversal Now that we have an implicit ordering to our tree, what happens with the traversal algorithms we previously discussed? In Order Traversal Visits all the nodes in the tree in order 60 Root Order of output: 30 46 50 58 60 70 77 80 50 70 30 58 80 46 77 21

Binary Search Tree Traversal Tree Sort Given an unordered list of elements, add them all to a binary search tree Traverse the binary search tree with an in-order traversal All the nodes come back out sorted! 22

Binary Search Tree Insertion Binary Search Tree Insertion 1) Create the new node with its data and set both of its pointers to NULL 2) If the tree is empty, make the new node the root 3) Walk down the tree node by node like you were doing a search If new value is less than current node, move to left subtree, otherwise move to right subtree Potentially reject duplicates New nodes are always added in the place of an existing NULL...they never reposition other nodes 24

Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Root 60 50 70 30 58 80 46 77 25

Ex: insert 59 Make new node Binary Search Tree Insertion How do you insert a new value into a binary search tree? 60 Root 50 70 30 58 80 New Node 59 46 77 26

Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left Current 60 Root 50 70 30 58 80 New Node 59 46 77 27

59 > 50; go right Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left Current 50 60 70 Root 30 58 80 New Node 59 46 77 28

Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left 60 Root 59 > 50; go right 59 > 58; go right 50 Current 70 30 58 80 New Node 59 46 77 29

Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left 60 Root 59 > 50; go right 59 > 58; go right Right is NULL 50 70 Insert new Node 30 58 80 46 59 77 30

Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left 60 Root 59 > 50; go right 59 > 58; go right Right is NULL 50 70 Insert new Node 30 58 80 46 59 77 31

Binary Search Tree Remove Binary Search Tree Removal Search the tree for the node that you want to remove When node is found, removal breaks down into 3 distinct cases 1) Remove a node with no children 2) Remove a node with one child 3) Remove a node with two children 32

Binary Search Tree Remove Remove a node with no children (case #1) Find the node and its parent Set the parent pointer to the node to NULL Delete the node 33

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Root 60 50 70 30 58 80 46 77 34

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value 60 Root 50 70 30 58 80 To remove 46 77 35

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value Set parent s pointer to NULL 60 Root 50 70 30 58 80 To remove 46 77 36

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value Set parent s pointer to NULL Delete the child 60 Root 50 70 30 58 80 To remove 46 77 37

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value Set parent s pointer to NULL Delete the child 60 Root 50 70 30 58 80 77 38

Binary Search Tree Remove Remove a node with one child (case #2) Find the node and its parent Set the parent pointer to the node s only child Delete the node 39

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Root 60 50 70 30 58 80 46 77 40

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value 60 Root 50 70 To remove 30 58 80 46 77 41

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value Pointer value s parent to value s only child 60 Root 50 70 To remove 30 58 80 46 77 42

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value Pointer value s parent to value s only child 60 Root Delete the node 50 70 To remove 30 58 80 46 77 43

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value Pointer value s parent to value s only child Delete the node 50 60 70 Root 58 80 46 77 44

Binary Search Tree Remove Remove a node with two children (case #3) Find the node Remove node s successor or predecessor Replace node s data with successor/predecessor data Predecessor(x) The node in the tree with the biggest value less than x Successor(x) The node in the tree with the smallest value greater than x 45

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Root 60 50 70 30 58 80 46 77 46

Case #3 Ex: remove 50 Search for the value Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Root 60 50 70 30 58 80 46 77 47

Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in Binary Search Tree Remove How do you remove a value from a binary search tree? left subtree of node To remove 50 60 70 Root 30 58 80 Predecessor 46 77 48

Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in left subtree of node Swap values of node and its predecessor Binary Search Tree Remove How do you remove a value from a binary search tree? Predecessor 30 46 Swap 58 60 70 Root 80 To remove 50 77 49

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in 60 Root left subtree of node Swap values of node and its predecessor 46 70 Delete the node via case #1 or case #2 30 58 80 To remove 50 77 50

Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in 60 Root left subtree of node Swap values of node and its predecessor 46 70 Delete the node via case #1 or case #2 30 58 80 77 51

Binary Search Tree Construction Insert the following values in an empty tree (in this order) 60, 46, 77, 45, 50, 63, 91 What does the resulting tree look like? 52

Binary Search Tree Construction Insert the following values in an empty tree (in this order) 60, 46, 77, 45, 50, 63, 91 What does the resulting tree look like? Tree is perfectly balanced! Height = log(n+1) 60 Root 46 77 45 50 63 91 53

Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? 54

Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 Root 91 What does the resulting tree look like? Insert 91 55

Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 Root 91 What does the resulting tree look like? Insert 91 Insert 77 77 56

Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 Root 91 What does the resulting tree look like? Insert 91 Insert 77 Insert 63 63 77 57

Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert 91 Insert 77 Insert 63 60 Insert 60 63 Root 77 91 58

Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 Root 91 What does the resulting tree look like? Tree is completely unbalanced Height = n 50 60 63 77 46 45 59

Binary Search Tree Efficiency Binary Search Tree (search, add, remove) What are the best case scenarios? What s the Big O for this case? Tree is balanced: O(log(n)) What are the worst case scenarios? What s the Big O for this case? Tree is linear: O(n) 60

CS102 Balanced Binary Search Trees Prof Tejada 1

Balanced Binary Trees Can we ever guarantee a balanced binary tree? 3

Balanced Binary Trees Randomize insertion order If there is no particular order of element insertion that gives us the worst case, then the Big O trends toward O(log(n)) The same argument as randomized quicksort Unfortunately, this is not always possible 4

Balanced Binary Trees Global balance: all leaf nodes are at the maximum depth or at a depth one less than the maximum Local balance: difference between the maximum depth of the left and right subtrees of nonleaf node t is at most 1 Ex: AVL trees There are other types of balanced binary search trees Ex: Red-black trees, Treaps 5

Binary Search Tree Rotation A binary search tree can be balanced There is a single primitive called rotation If rotation is performed at the right place, it can bring a tree into a more balanced state If rotation is performed at the wrong place, it can make a tree even more unbalanced There is basically only one type of rotation Known as a single rotation "single rotation to the left" "single rotation to the right" There is somethingn called "double rotation" It s just two single rotations in a row 6

Binary Search Tree Rotation 7 4 11 x 3 6 9 18 y 2 14 19 12 17 22 Left-Rotate(T, x) 20 7 4 18 y 3 6 x 11 19 2 9 14 22 12 17 20 7

Binary Search Tree Rotation Rotation: (within a subtree) Single right rotation around node a b a C single rotation A b a A B B C Single left rotation around node b b a C single rotation A b a A B B C 8

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: 5 4 3 2 1 9

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 4 5 3 2 Single right rotation around node a 1 b a C single rotation A b a A B B C 10

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 b 2 a 3 4 5 Single right rotation around node a 1 b a C single rotation A b a A B B C 11

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 2 b 4 5 1 3 a Single right rotation around node a b a C single rotation A b a A B B C 12

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 Single right rotation around node 5 2 4 1 3 5 Single right rotation around node a b a C single rotation A b a A B B C 13

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 Single right rotation around node 5 a is 5, b is 4 1 2 b 4 3 a 5 Single right rotation around node a b a C single rotation A b a A B B C 14

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 Single right rotation around node 5 a is 5, b is 4 2 1 3 4 b 5 a Single right rotation around node a b a C single rotation A b a A B B C 15

Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree In practice, you do not want to scan the whole tree to look for a good place to balance For an operation with a BST (such as insert and remove), you must not visit/inspect more than O(log(N)) nodes You need to use the structure of a particular BST to make sure that you stay within this limit AVL trees, Red-black trees, Treaps have different structures 16

AVL Trees AVL Trees Each node knows its height in the tree (not a requirement) Each node calculates a balance factor Balance Factor Height of its left subtree minus the height of its right subtree (or vice-versa) A node with balance factor 1, 0, or -1 is considered balanced and need not be changed A node with any other balance factor is considered unacceptable and must be rebalanced Search/Insert/Remove become O(log(n)) 17

AVL Trees AVL tree example: +1 +1-1 -1-1 +1 +1 +1 +1 0-1 0 0 0 0 +1 0 0 0 0 0 18

AVL Trees AVL tree insertion: Only need to balance at once place (but where?) +1 +1-1 -1-1 +1 +1 +1 +1 0-1 0 0 0 0 +1 0 0 0 0 0 0 19

AVL Trees AVL tree insertion: Only need to balance at once place (but where?) +1 +1-1 -1-1 +1 +1 +1 +1 0-2 0 0 0 0 +1 0-1 0 0 0 0 20

AVL Trees AVL tree insertion: Only need to balance at once place (but where?) +1 +1-1 -1-1 +1 +1 +1 +1 0 0 0 0 0 0 +1 0 0 0 0 0 0 21

AVL Trees AVL tree deletion is more complicated +1 +1-1 -1-1 +1 +1 +1 +1 0-1 0 0 0 0 +1 0 0 0 0 0 22

Red-Black Trees Red/Black Trees Each node stores one extra piece of information: it is colored red or black A binary search tree is a Red-Black Tree if it satisfies the following red/black properties: 1) Every node is red or black 2) The root is black 3) Every leaf (NULL) is black 4) If a node is red, then both of its children are black 5) Every simple paths from a node to any descendant leave node contain the same number of black nodes (4) and (5) imply that the tree is relatively balanced If deepest path from root to a leaf has length X, shortest path from root to a leaf has length X/2 23

Red-Black Trees 13 8 17 1 11 15 25 NIL 6 NIL NIL NIL NIL 22 27 NIL NIL NIL NIL NIL NIL 24

Red-Black Trees Red/Black Trees Properties are maintained via rotations Search/Insert/Remove become O(log(n)) Algorithm is bit complicated 25

Treaps Treaps Merge two of our favorite data structures Binary Search Trees Heaps Add some randomization to insertion Each element in the tree is given a random numeric priority Treap Properties If v is a left child of u, then v < u (BST property) If v is a right child of u, then v > u (BST property) If v is a child of u, the priority(v) < priority(u) (Max-heap property) Search/Insert/Remove become O(log(n)) 26

Treaps example: Letters are node values Numbers are priorities Treaps 4 c 9 h 7 j 2 a 0 e 27

Treaps example: Letters are node values Numbers are priorities Ex: Insert node f Ex: random priority 6 Treaps 2 a 4 c 0 e 9 h 7 j 28

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap 2 a 4 c 0 e 7 j 6 f 29

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e 2 a 4 c 0 e 7 j 6 f 30

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e 2 a 4 c 6 f 7 j 0 e 31

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e 2 a 4 c 6 f 7 j Not a heap 0 e 32

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e Not a heap Single left rotation around c 2 a 4 c 0 e 6 f 7 j 33

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e Not a heap 4 c 6 f 7 j Single left rotation around c 2 a 0 e 34

Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e Not a heap 4 c 6 f 7 j Single left rotation around c It s a heap! Done. 2 a 0 e 35

Other Types of Trees Trees don t have to be binary B-Trees (Trees with N children per node) 2,3,4-Trees (B-Trees with 2-4 children per node) B+-Trees (B-Trees with data only at the leaf) Filesystems look like trees Process hierarchies look like trees Lots of AI and search algorithms use trees to represent complex state transitions A* Search 36