CS102 Binary Search Trees
|
|
- Madeleine Webster
- 6 years ago
- Views:
Transcription
1 CS102 Binary Search Trees Prof Tejada 1
2 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
3 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] Root
4 Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find Root
5 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 Root
6 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? No 58 < 50? No 58 > 50? Yes, go right
7 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? No 58 < 60? Yes, go left 58 == 50? No 58 < 50? No 58 > 50? Yes, go right 58 == 58? Yes Current
8 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
9 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
10 } 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
11 Binary Search Tree Min/Max How would you find the minimum/maximum keys in a binary search tree? Root
12 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
13 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
14 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:
15 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
16 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
17 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Root
18 Ex: insert 59 Make new node Binary Search Tree Insertion How do you insert a new value into a binary search tree? 60 Root New Node
19 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 New Node
20 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 Root New Node
21 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 New Node
22 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 Insert new Node
23 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 Insert new Node
24 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
25 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
26 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Root
27 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 To remove
28 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 To remove
29 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 To remove
30 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
31 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
32 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Root
33 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 To remove
34 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 To remove
35 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 To remove
36 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 Root
37 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
38 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Root
39 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
40 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 Root Predecessor
41 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 Swap Root 80 To remove
42 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 Delete the node via case #1 or case # To remove
43 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 Delete the node via case #1 or case #
44 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
45 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 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
47 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
48 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
49 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
50 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 Insert Root
51 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
52 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
53 CS102 Balanced Binary Search Trees Prof Tejada 1
54 Balanced Binary Trees Can we ever guarantee a balanced binary tree? 3
55 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
56 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
57 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
58 Binary Search Tree Rotation x y Left-Rotate(T, x) y 3 6 x
59 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
60 Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example:
61 Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node Single right rotation around node a 1 b a C single rotation A b a A B B C 10
62 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 Single right rotation around node a 1 b a C single rotation A b a A B B C 11
63 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 a Single right rotation around node a b a C single rotation A b a A B B C 12
64 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 Single right rotation around node a b a C single rotation A b a A B B C 13
65 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 b 4 3 a 5 Single right rotation around node a b a C single rotation A b a A B B C 14
66 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 b 5 a Single right rotation around node a b a C single rotation A b a A B B C 15
67 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
68 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
69 AVL Trees AVL tree example:
70 AVL Trees AVL tree insertion: Only need to balance at once place (but where?)
71 AVL Trees AVL tree insertion: Only need to balance at once place (but where?)
72 AVL Trees AVL tree insertion: Only need to balance at once place (but where?)
73 AVL Trees AVL tree deletion is more complicated
74 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
75 Red-Black Trees NIL 6 NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL 24
76 Red-Black Trees Red/Black Trees Properties are maintained via rotations Search/Insert/Remove become O(log(n)) Algorithm is bit complicated 25
77 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
78 Treaps example: Letters are node values Numbers are priorities Treaps 4 c 9 h 7 j 2 a 0 e 27
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
Lecture 6: Analysis of Algorithms (CS )
Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black
More informationBalanced Binary Search Trees. Victor Gao
Balanced Binary Search Trees Victor Gao OUTLINE Binary Heap Revisited BST Revisited Balanced Binary Search Trees Rotation Treap Splay Tree BINARY HEAP: REVIEW A binary heap is a complete binary tree such
More informationSorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min
Binary Search Trees FRIDAY ALGORITHMS Sorted Arrays Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min 6 10 11 17 2 0 6 Running Time O(1) O(lg n) O(1) O(1)
More informationTrees. (Trees) Data Structures and Programming Spring / 28
Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r
More informationA set of nodes (or vertices) with a single starting point
Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of
More informationLecture: Analysis of Algorithms (CS )
Lecture: Analysis of Algorithms (CS583-002) Amarda Shehu Fall 2017 1 Binary Search Trees Traversals, Querying, Insertion, and Deletion Sorting with BSTs 2 Example: Red-black Trees Height of a Red-black
More informationBinary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree
0/2/ Preview Binary Tree Tree Binary Tree Property functions In-order walk Pre-order walk Post-order walk Search Tree Insert an element to the Tree Delete an element form the Tree A binary tree is a tree
More informationComputational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs
Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in
More informationAnalysis of Algorithms
Analysis of Algorithms Trees-I Prof. Muhammad Saeed Tree Representation.. Analysis Of Algorithms 2 .. Tree Representation Analysis Of Algorithms 3 Nomenclature Nodes (13) Size (13) Degree of a node Depth
More informationAlgorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms
More informationModule 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.
The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures
More informationAlgorithms. AVL Tree
Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba 1 / 12 Binary Search Trees (review) Structure
More informationBinary Search Trees. Analysis of Algorithms
Binary Search Trees Analysis of Algorithms Binary Search Trees A BST is a binary tree in symmetric order 31 Each node has a key and every node s key is: 19 23 25 35 38 40 larger than all keys in its left
More informationBinary Trees, Binary Search Trees
Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)
More informationUses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010
Uses for About Binary January 31, 2010 Uses for About Binary Uses for Uses for About Basic Idea Implementing Binary Example: Expression Binary Search Uses for Uses for About Binary Uses for Storage Binary
More informationCSCI Trees. Mark Redekopp David Kempe
CSCI 104 2-3 Trees Mark Redekopp David Kempe Trees & Maps/Sets C++ STL "maps" and "sets" use binary search trees internally to store their keys (and values) that can grow or contract as needed This allows
More informationProperties of red-black trees
Red-Black Trees Introduction We have seen that a binary search tree is a useful tool. I.e., if its height is h, then we can implement any basic operation on it in O(h) units of time. The problem: given
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationLecture 23: Binary Search Trees
Lecture 23: Binary Search Trees CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki 1 BST A binary tree is a binary search tree iff it is empty or if the value of every node is both greater than or equal
More informationBinary Trees. Recursive definition. Is this a binary tree?
Binary Search Trees Binary Trees Recursive definition 1. An empty tree is a binary tree 2. A node with two child subtrees is a binary tree 3. Only what you get from 1 by a finite number of applications
More informationCS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results
HW Difficulty CS 361, Lecture 21 Jared Saia University of New Mexico The HW in this class is inherently difficult, this is a difficult class. You need to be able to solve problems as hard as the problems
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationAlgorithms. Deleting from Red-Black Trees B-Trees
Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that
More informationWeek 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS
ASSIGNMENTS h0 available and due before 10pm on Monday 1/28 h1 available and due before 10pm on Monday 2/4 p1 available and due before 10pm on Thursday 2/7 Week 2 TA Lab Consulting - See schedule (cs400
More informationBalanced Binary Search Trees
Balanced Binary Search Trees Pedro Ribeiro DCC/FCUP 2017/2018 Pedro Ribeiro (DCC/FCUP) Balanced Binary Search Trees 2017/2018 1 / 48 Motivation Let S be a set of comparable objects/items: Let a and b be
More informationCSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331
CSE2331/5331 Topic 6: Binary Search Tree Data structure Operations Set Operations Maximum Extract-Max Insert Increase-key We can use priority queue (implemented by heap) Search Delete Successor Predecessor
More informationChapter 22 Splay Trees
Chapter 22 Splay Trees Introduction Splay trees support all the operations of binary trees. But they do not guarantee Ο(log N) worst-case performance. Instead, its bounds are amortized, meaning that although
More informationB-Trees. Version of October 2, B-Trees Version of October 2, / 22
B-Trees Version of October 2, 2014 B-Trees Version of October 2, 2014 1 / 22 Motivation An AVL tree can be an excellent data structure for implementing dictionary search, insertion and deletion Each operation
More informationCS350: Data Structures Red-Black Trees
Red-Black Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Red-Black Tree An alternative to AVL trees Insertion can be done in a bottom-up or
More informationData Structures Lesson 7
Data Structures Lesson 7 BSc in Computer Science University of New York, Tirana Assoc. Prof. Dr. Marenglen Biba 1-1 Binary Search Trees For large amounts of input, the linear access time of linked lists
More informationSection 4 SOLUTION: AVL Trees & B-Trees
Section 4 SOLUTION: AVL Trees & B-Trees 1. What 3 properties must an AVL tree have? a. Be a binary tree b. Have Binary Search Tree ordering property (left children < parent, right children > parent) c.
More informationAdvanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret
Advanced Algorithms Class Notes for Thursday, September 18, 2014 Bernard Moret 1 Amortized Analysis (cont d) 1.1 Side note: regarding meldable heaps When we saw how to meld two leftist trees, we did not
More informationSearch Trees. Undirected graph Directed graph Tree Binary search tree
Search Trees Undirected graph Directed graph Tree Binary search tree 1 Binary Search Tree Binary search key property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then
More informationCOSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)
COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back
More informationOperations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.
Priority Queue, Heap and Heap Sort In this time, we will study Priority queue, heap and heap sort. Heap is a data structure, which permits one to insert elements into a set and also to find the largest
More informationTree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.
Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except
More informationBinary Trees and Binary Search Trees
Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe
More informationBinary Search Trees, etc.
Chapter 12 Binary Search Trees, etc. Binary Search trees are data structures that support a variety of dynamic set operations, e.g., Search, Minimum, Maximum, Predecessors, Successors, Insert, and Delete.
More informationCS 171: Introduction to Computer Science II. Binary Search Trees
CS 171: Introduction to Computer Science II Binary Search Trees Binary Search Trees Symbol table applications BST definitions and terminologies Search and insert Traversal Ordered operations Delete Symbol
More informationCSC148 Week 7. Larry Zhang
CSC148 Week 7 Larry Zhang 1 Announcements Test 1 can be picked up in DH-3008 A1 due this Saturday Next week is reading week no lecture, no labs no office hours 2 Recap Last week, learned about binary trees
More informationLec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)
Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Binary Search Trees CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures
More informationCISC 235: Topic 4. Balanced Binary Search Trees
CISC 235: Topic 4 Balanced Binary Search Trees Outline Rationale and definitions Rotations AVL Trees, Red-Black, and AA-Trees Algorithms for searching, insertion, and deletion Analysis of complexity CISC
More informationCISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms
More informationCS 380 ALGORITHM DESIGN AND ANALYSIS
CS 380 ALGORITHM DESIGN AND ANALYSIS Lecture 12: Red-Black Trees Text Reference: Chapters 12, 13 Binary Search Trees (BST): Review Each node in tree T is a object x Contains attributes: Data Pointers to
More informationSelf-Balancing Search Trees. Chapter 11
Self-Balancing Search Trees Chapter 11 Chapter Objectives To understand the impact that balance has on the performance of binary search trees To learn about the AVL tree for storing and maintaining a binary
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every
More informationCS-301 Data Structure. Tariq Hanif
1. The tree data structure is a Linear data structure Non-linear data structure Graphical data structure Data structure like queue FINALTERM EXAMINATION Spring 2012 CS301- Data Structure 25-07-2012 2.
More informationCS521 \ Notes for the Final Exam
CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )
More informationBinary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...
Binary search trees Support many dynamic-set operations, e.g. Search Minimum Maximum Insert Delete... Can be used as dictionary, priority queue... you name it Running time depends on height of tree: 1
More information2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS
Outline catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 4 Why care about advanced implementations? Same entries, different insertion sequence:
More informationSection 1: True / False (1 point each, 15 pts total)
Section : True / False ( point each, pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered
More informationCMSC 341 Leftist Heaps
CMSC 341 Leftist Heaps Based on slides from previous iterations of this course Today s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations Review of Heaps Min Binary
More informationChapter 5. Binary Trees
Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children
More informationExam Data structures DAT036/DAT037/DIT960
Exam Data structures DAT036/DAT037/DIT960 Time Thursday 18 th August 2016, 08:30 12:30 Place Maskinhuset / SB Multisal Course responsible Nick Smallbone, tel. 0707 183062 The exam consists of six questions.
More informationBalanced Search Trees. CS 3110 Fall 2010
Balanced Search Trees CS 3110 Fall 2010 Some Search Structures Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need
More informationDesign and Analysis of Algorithms Lecture- 9: Binary Search Trees
Design and Analysis of Algorithms Lecture- 9: Binary Search Trees Dr. Chung- Wen Albert Tsao 1 Binary Search Trees Data structures that can support dynamic set operations. Search, Minimum, Maximum, Predecessor,
More informationFall, 2015 Prof. Jungkeun Park
Data Structures and Algorithms Binary Search Trees Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea in Univ. of North Texas.
More informationFinal Examination CSE 100 UCSD (Practice)
Final Examination UCSD (Practice) RULES: 1. Don t start the exam until the instructor says to. 2. This is a closed-book, closed-notes, no-calculator exam. Don t refer to any materials other than the exam
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees
Computer Science 10 Data Structures Siena College Fall 018 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree
More informationCS 315 Data Structures mid-term 2
CS 315 Data Structures mid-term 2 1) Shown below is an AVL tree T. Nov 14, 2012 Solutions to OPEN BOOK section. (a) Suggest a key whose insertion does not require any rotation. 18 (b) Suggest a key, if
More informationCSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)
CSE100 Advanced Data Structures Lecture 8 (Based on Paul Kube course materials) CSE 100 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs
More informationBinary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.
Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from
More informationCS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya
CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(lg n) is guaranteed when
More informationPart 2: Balanced Trees
Part 2: Balanced Trees 1 AVL Trees We could dene a perfectly balanced binary search tree with N nodes to be a complete binary search tree, one in which every level except the last is completely full. A
More informationCMSC 341. Binary Search Trees CMSC 341 BST
CMSC 341 Binary Search Trees CMSC 341 BST Announcements Homework #3 dues Thursday (10/5/2017) Exam #1 next Thursday (10/12/2017) CMSC 341 BST A Generic Tree CMSC 341 BST Binary Tree CMSC 341 BST The Binary
More informationSFU CMPT Lecture: Week 9
SFU CMPT-307 2008-2 1 Lecture: Week 9 SFU CMPT-307 2008-2 Lecture: Week 9 Ján Maňuch E-mail: jmanuch@sfu.ca Lecture on July 8, 2008, 5.30pm-8.20pm SFU CMPT-307 2008-2 2 Lecture: Week 9 Binary search trees
More informationHash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell
Hash Tables CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 22, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005
More informationAugmenting Data Structures
Augmenting Data Structures [Not in G &T Text. In CLRS chapter 14.] An AVL tree by itself is not very useful. To support more useful queries we need more structure. General Definition: An augmented data
More informationCS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya
CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is d = ëlog for a binary tree
More informationTrees 2: Linked Representation, Tree Traversal, and Binary Search Trees
Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this
More informationLecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs
Lecture 5 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Reading: Randomized Search Trees by Aragon & Seidel, Algorithmica 1996, http://sims.berkeley.edu/~aragon/pubs/rst96.pdf;
More informationQuiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)
Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Solutions Problem 1. Quiz 1 Solutions Asymptotic orders
More informationBINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015
BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due tonight at midnight -asking for regrades through assignment 5 and midterm must
More informationECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.
ECE 22 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece22/ Trees IV Lecture 2 Prof. Eric Polizzi Summary previous lectures Implementations BST 5 5 7 null 8 null null 7 null
More informationCS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22
CS 234 Module 8 November 15, 2018 CS 234 Module 8 ADT Priority Queue 1 / 22 ADT Priority Queue Data: (key, element pairs) where keys are orderable but not necessarily distinct, and elements are any data.
More informationPriority Queues (Heaps)
Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the
More informationCS 241 Analysis of Algorithms
CS 241 Analysis of Algorithms Professor Eric Aaron Lecture T Th 9:00am Lecture Meeting Location: OLB 205 Business HW4 out Due Tuesday, Nov. 5 For when should we schedule a make-up lecture? Exam: Tuesday
More informationvoid insert( Type const & ) void push_front( Type const & )
6.1 Binary Search Trees A binary search tree is a data structure that can be used for storing sorted data. We will begin by discussing an Abstract Sorted List or Sorted List ADT and then proceed to describe
More informationSpring 2018 Mentoring 8: March 14, Binary Trees
CSM 6B Binary Trees Spring 08 Mentoring 8: March 4, 08 Binary Trees. Define a procedure, height, which takes in a Node and outputs the height of the tree. Recall that the height of a leaf node is 0. private
More informationMID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.
MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions
More informationRed-black trees (19.5), B-trees (19.8), trees
Red-black trees (19.5), B-trees (19.8), 2-3-4 trees Red-black trees A red-black tree is a balanced BST It has a more complicated invariant than an AVL tree: Each node is coloured red or black A red node
More informationCOMP : Trees. COMP20012 Trees 219
COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural
More informationAdvanced Set Representation Methods
Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation
More informationTrees 2: Linked Representation, Tree Traversal, and Binary Search Trees
Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this
More informationECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi
ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Heaps I Lecture 22 Prof. Eric Polizzi Motivations Review of priority queue Input F E D B A Output Input Data structure
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationAdvanced Java Concepts Unit 5: Trees. Notes and Exercises
Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will
More informationTrees. Prof. Dr. Debora Weber-Wulff
Trees Prof. Dr. Debora Weber-Wulff Flickr, _marmota, 2007 Major Sources Michell Waite & Robert Lafore, Data Structures & Algorithms in Java Michael T. Goodrich and Roberto Tamassia Data Structures and
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,
More informationCSE 100 Advanced Data Structures
CSE 100 Advanced Data Structures Overview of course requirements Outline of CSE 100 topics Review of trees Helpful hints for team programming Information about computer accounts Page 1 of 25 CSE 100 web
More information9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology
Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive
More informationDATA STRUCTURES AND ALGORITHMS
LECTURE 13 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 12... Binary Search Trees Binary Tree Traversals Huffman coding Binary Search Tree Today Binary Search
More informationDATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap
DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture TREE is hierarchical (non linear) data structure Binary trees Definitions Full tree,
More informationProgramming Abstractions
Programming Abstractions C S 1 0 6 B Cynthia Lee Topics: Wednesday: Binary Search Tree (BST) Starting with a dream: binary search in a linked list? How our dream provided the inspiration for the BST Note:
More informationCS 350 : Data Structures Binary Search Trees
CS 350 : Data Structures Binary Search Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary
More informationCSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader
CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, 2015 Problem Topic Points Possible Points Earned Grader 1 The Basics 40 2 Application and Comparison 20 3 Run Time Analysis 20 4 C++ and Programming
More information