CPS 100 8.1 Balanced Search Trees l BST: efficient lookup, insertion, deletion  Average case: O(log n) for all operations since find is O(log n) [complexity.

Slides:



Advertisements
Similar presentations
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
Advertisements

Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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):
CSE 326: Data Structures AVL Trees
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
AVL Trees ITCS6114 Algorithms and Data Structures.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CPS Balanced Search Trees l BST: efficient lookup, insertion, deletion  Average case: O(log n) for all operations since find is O(log n) [complexity.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
AVL Tree Definition: Theorem (Adel'son-Vel'skii and Landis 1962):
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Fundamental Structures of Computer Science Feb 03, 2005 Ananda Guna Tries.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CPS 100, Spring Search, Trees, Games, Backtracking l Trees help with search  Set, map: binary search tree, balanced and otherwise  Quadtree.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CPS 100, Spring Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.
Generic Trees—Trie, Compressed Trie, Suffix Trie (with Analysi
CSE332: Data Abstractions Lecture 7: AVL Trees
Searching, Maps,Tries (hashing)
From doubly-linked lists to binary trees
File Organization and Processing Week 3
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
UNIT III TREES.
CSIT 402 Data Structures II
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Trees (Chapter 4) Binary Search Trees - Review Definition
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Tree Rotations and AVL Trees
AVL Trees CSE 373 Data Structures.
Binary Search Trees.
Self-Balancing Search Trees
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CS 261 – Data Structures AVL Trees.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CPS Balanced Search Trees l BST: efficient lookup, insertion, deletion  Average case: O(log n) for all operations since find is O(log n) [complexity of insert after find is O(1), why?]  Worst case is bad, what's big-Oh? What's the tree shape?  If we can guarantee log n in worst case, why might this be preferable to hashing? Properties of search tree? l Balanced Search trees  Use rotations to maintain balance, different implementations rotate/rebalance at different times  AVL tree is conceptually simple, bookkeeping means coefficient for big-Oh is higher than other ideas  Red-black tree harder to code but good performance: basis for Java map classes and most C++ map classes

CPS Balance trees we won't study l B-trees are used when data is both in memory and on disk  File systems, really large data sets  Rebalancing guarantees good performance both asymptotically and in practice. Differences between cache, memory, disk are important l Splay trees rebalance during insertion and during search, nodes accessed often more closer to root  Other nodes can move further from root, consequences? Performance for some nodes gets better, for others …  No guarantee running time for a single operation, but guaranteed good performance for a sequence of operations, this is good amortized cost (vector push_back)

CPS Balanced trees we will study l Both kinds have worst-case O(log n) time for tree operations l AVL (Adel’son-Velskii and Landis), 1962  Nodes are “height-balanced”, subtree heights differ by 1  Rebalancing requires per-node bookkeeping of height  l Red-black tree uses same rotations, but can rebalance in one pass, contrast to AVL tree  In AVL case, insert, calculate balance factors, rebalance  In Red-black tree can rebalance on the way down, code is more complex, but doable  STL in C++ uses red-black tree for map and set classes  Standard java.util.TreeMap/TreeSet use red-black

CPS Rotations and balanced trees l Height-balanced trees  For every node, left and right subtree heights differ by at most 1  After insertion/deletion need to rebalance  Every operation leaves tree in a balanced state: invariant property of tree l Find deepest node that’s unbalanced then make sure:  On path from root to inserted/deleted node  Rebalance at this unbalanced point only Are these trees height- balanced?

CPS Rotation to rebalance l When a node N (root) is unbalanced height differs by 2 (must be more than one)  Change N->left->left doLeft  Change N->left->right doLeftRight  Change N->right->left doRightLeft  Change N->right->right doRight l First/last cases are symmetric l Middle cases require two rotations  First of the two puts tree into doLeft or doRight A B C A B C Tree * doLeft(Tree * root) { Tree * newRoot = root->left; root->left = newRoot->right; newRoot->right = root; return newRoot; } N N

CPS Rotation up close (doLeft) l Why is this called doLeft?  N will no longer be root, new value in left->left subtree  Left child becomes new root l Rotation isn’t “to the left”, but rather “brings left child up”  doLeftChildRotate? A B C N A B C N Tree * doLeft(Tree * root) { Tree * newRoot = root->left; root->left = newRoot->right; newRoot->right = root; return newRoot; }

CPS Rotation to rebalance l Suppose we add a new node in right subtree of left child of root  Single rotation can’t fix  Need to rotate twice l First stage is shown at bottom  Rotate blue node right (its right child takes its place)  This is left child of unbalanced B A C N A B C N AB1B1 B2B2 C ????? Tree * doRight(Tree * root) { Tree * newRoot = root->right; root->right = newRoot->left; newRoot->left = root; return newRoot; } B2B2 A B1B1

CPS Double rotation complete AB1B1 B2B2 C B2B2 A B1B1 C B2B2 A B1B1 C l Calculate where to rotate and what case, do the rotations Tree * doRight(Tree * root) { Tree * newRoot = root->right; root->right = newRoot->left; newRoot->left = root; return newRoot; } Tree * doLeft(Tree * root) { Tree * newRoot = root->left; root->left = newRoot->right; newRoot->right = root; return newRoot; }

CPS AVL tree practice l Insert into AVL tree:   After adding 16: doLeftRight  After 3, doLeft on doRight doLeft

CPS AVL practice: continued, and finished l After adding 13, ok l After adding14, not ok  doRight at

CPS Trie: efficient search of words/suffixes l A trie (from retrieval, but pronounced “try”) supports  Insertion: a word into the trie (delete and look up)  These operations are O(size of string) regardless of how many strings are stored in the trie! Guaranteed ! l In some ways a trie is like a 128 (or 26 or alphabet-size) tree, one branch/edge for each character/letter  Node stores branches to other nodes  Node stores whether it ends the string from root to it l Extremely useful in DNA/string processing  monkeys and typewriter simulation which is similar to some methods used in Natural Language understanding (n-gram methods)

CPS Trie picture and code (see trie.cpp) l To add string  Start at root, for each char create node as needed, go down tree, mark last node l To find string  Start at root, follow links If NULL, not found  Check word flag at end l To print all nodes  Visit every node, build string as nodes traversed l What about union and intersection? a c p r n s a r a h ao st cd Indicates word ends here

CPS Boggle: Tries, backtracking, structure Find words on 4x4 grid Adjacent letters:   l No re-use in same word Two approaches to find all words l Try to form every word on board  Look up prefix as you go Trie is useful for prefixes l Look up every word in dictionary  For each word: on board? l ZEAL and SMILES HISE W D BL A M IB Z S VE

CPS Search board for word: trieboggle.cpp void search(int row, int col, TrieNode * t, string soFar) // pre: row, col are on board, soFar is valid prefix, // string constructed on board during current search // t represents the path in the trie of soFar { if (!legal(row,col) || isVisited(row,col) ) return; char ch = myBoard[row][col]; // check if still a prefix Node * child = t->links[ch]; if (child == 0) return; // NOT a prefix, stop // still prefix, continue markVisited(row,col); // don’t revisit string newPrefix = word + ch; if (child->isWord) cout << newPrefix << endl; doFind(row-1,col-1,child,newPrefix); // up-left doFind(row-1,col, child,newPrefix); // straight up doFind(row-1,col+1,child,newPrefix); // still 5 more calls unVisit(row,col); // now ok to revisit }

CPS Search for all words: boggle.cpp bool wordFound(const string& s, const Point& p) // pre: s is suffix of word searched for, prefix so far // is found and last letter of found prefix at p(row,col) { if (s.length() == 0) return true; // no more suffix, done tvector points = myPointsFor(s[0]); for(int k=0; k < points.size(); k++) { Point nextP = points[k]; if (IsAdjacent(p,nextP) && ! isVisited(nextP)) { markVisited(nextP); // don’t visit again if (wordFound(s.substr(1,s.length()-1),nextP)) { return true; } unVisit(nextP); // ok to visit again } return false; // tried to find s, failed in all attempts }

CPS Danny Hillis l The third culture consists of those scientists and other thinkers in the empirical world who, through their work and expository writing, are taking the place of the traditional intellectual in rendering visible the deeper meanings of our lives, redefining who and what we are. (Wired 1998) And now we are beginning to depend on computers to help us evolve new computers that let us produce things of much greater complexity. Yet we don't quite understand the process - it's getting ahead of us. We're now using programs to make much faster computers so the process can run much faster. That's what's so confusing - technologies are feeding back on themselves; we're taking off. We're at that point analogous to when single-celled organisms were turning into multicelled organisms. We are amoebas and we can't figure out what the hell this thing is that we're creating.