Special Purpose Trees: Tries and Height Balanced Trees CS 400/600 – Data Structures.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
Trees Types and Operations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
AA Trees another 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 balanced.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
BST Data Structure A BST node contains: A BST contains
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CS 367 – Introduction to Data Structures
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
Find Find 13: Find Min and Find Max Find Min: Find Max:
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
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.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
CS 367 Introduction to Data Structures Lecture 8.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Introduction Applications Balance Factor Rotations Deletion Example
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Tree data structure.
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Tree data structure.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
CS2606 Trees.
A Robust Data Structure
(edited by Nadia Al-Ghreimil)
Non-Linear Structures
Search Sorted Array: Binary Search Linked List: Linear Search
CS 261 – Data Structures Binary Search Trees.
Presentation transcript:

Special Purpose Trees: Tries and Height Balanced Trees CS 400/600 – Data Structures

Advanced Trees2 Space Decomposition  BST – object space decomposition The shape of the tree depends on the order in which the keys are added Each key add splits the space into two parts, based on the key value Example: 70, 80 Values from 1 to to 6970 to to 8980 to

Advanced Trees3 Key Space Decomposition  We might prefer to evenly split the space based on the possible key values:  A tree based on key space decomposition is called a trie

Advanced Trees4 Binary Tries  If the key is an integer, we can split the space into two equal halves by looking at a single bit of the key Example: 8-bit key, values from 0 to 255 0xxxxxxx = 0 to 127 1xxxxxxx = 128 to xxxxxx = 0 to 63 01xxxxxx = 64 to 127  Values only at the leaf nodes!

Advanced Trees5 A Binary Trie  A binary trie for input set {2, 7, 24, 32, 37, 40, 120} Internal nodes don’t need to store anything: left=0, right= The trie will be the same shape, regardless of the order of insertion.

Advanced Trees6 Bitwise operations in C++ unsigned char i, j; // eight-bit values i = 3;// i = j = i << 4;// j = (48) // Testing a single bit: i = 1 << 4;// i = i = i & j;// bitwise AND, i = if (i) {}// if i==0, the bit was 0 else {}// otherwise it was 1

Advanced Trees7 Wasted space What if we add only 2, 7 and 32 to our binary trie? A lot of wasted space for nodes with only one child. Only two decisions to make. 1

Advanced Trees8 Compressing a trie  PATRICIA trie: Only include nodes with more than one child Levels do not always test a fixed bit position Each node stores a bit index, and a value 0000xxx 00000xx 01xxxxx xx 0xxxxxx

Advanced Trees9 Alphabet trie  Branching factor can be greater than 2:

Advanced Trees10 Balanced Trees  Binary search tree performance suffers when the tree is unbalanced  The AVL tree is a BST with the following additional property: For every node, the heights of its left and right subtrees differ by at most 1. The depth of an n node tree will be, at most, O(log n), so search and insert are O(log n) operations, even in the worst case. Insert and delete must maintain tree balance.

Advanced Trees11 An unbalanced BST The pivot node, is called s. Your text says it is the “bottom-most unbalanced node”, but this is not always correct….

Advanced Trees12 Handling both children What if s has two children? Where can we put this? Well, this node just lost a child, right?

Advanced Trees13 Single Rotation Ta dah!

Advanced Trees14 When a single rotation is not enough Insert Still unbalanced!!

Advanced Trees15 What’s the difference? Unbalanced The extra node is the left child of the left child of the left child of the unbalanced node. Unbalanced The extra node is the right child of the right child of the left child of the unbalanced node.

Advanced Trees16 Double Rotation When there is a bend in the path from the unbalanced node to the extra node, we must do a double rotation: Rotate below the pivot node Then rotate at the pivot node.

Advanced Trees17 Unbalanced Trees  With a single insertion or deletion, the tree can become unbalanced by at most one node: s Call the bottommost unbalanced node s. pivot

Advanced Trees18 Unbalanced subtrees  The extra node can’t be a child of s.  Rather it must be either: 1.The left child of the left child of s, 2.The right child of the left child of s, 3.The left child of the right child of s, or 4.The right child of the right child of s. For cases 1 & 4, we do a single rotation For cases 2 & 3, we do a double rotation s

Advanced Trees19 Single Rotation  P  S  B < P  S  C  S (Because C  P && S < P) P S C B A P S CB A The single rotation for the right child of the right child of S is the mirror image of this.

Advanced Trees20 Left single rotation P S C B A P S CB A

Advanced Trees21 Another view…

Advanced Trees22 When a single rotation isn’t enough…

Advanced Trees23 Double Rotation  S becomes the new root B gets the empty spot in the left subtree C gets the empty spot in the right subtree G S C A B P D G S C A B P D

Advanced Trees24 Double Left Rotation  Mirror image of double right rotation G S C A B P D G S C A B P D

Advanced Trees25 The AVL tree  Just like a BST, but after every insert and delete operation, balance is checked, and a single or double rotation operation is done if necessary.  The rotation operations are O(1), so the insert time is still O(log n)  Tree is always balanced, so search is O(log n)  A cousin of the AVL tree is the Splay tree Details in your text on pp. 431 – 434

Advanced Trees26 Spatial Data Structures  Suppose we have a database of buildings and the keys are the x and y coordinates of the building on a map  We could use two BST’s, one for x and one for y, but this has disadvantages Expensive to search for all buildings in a certain rectangle, or all buildings close to another building Not a natural representation  This is an example of a multidimensional key

Advanced Trees27 The K-D tree  Suppose you have a d-dimensional key  The K-D tree is a BST, but the decision at level i is based on the (i % k) th dimension K-D tree for cities at (40,50), (15, 70), (70, 10), (69, 50), (55, 80), and (80, 90).

Advanced Trees28 Spatial Decomposition  Each node in the tree represents a cut of the key space in a direction parallel to one of the dimensional axes: As with a BST, the tree and the division of the key space depend upon the order in which the data are inserted into the tree.

Advanced Trees29 Searching a K-D tree  At each level, decisions are made on only one coordinate Example – At level 1 of the following tree, records with y > 45 can be in either the right or left subtree of the root: Example: Search for record (x, y) = (69, 50)

Advanced Trees30 Implementation of Search bool KDtree::findhelp(BinNode *subroot, int *coord, Elem &e, int discrim) const { if (subroot == NULL) return false; int *currcoord; currcoord = subroot->coord(); if (EqualCoords(currcoord, coord)) { e = subroot->val(); return true; } if (curcoord[discrim] < coord[discrim]) return findhelp(subroot->left(), coord, e, (discrim+1)%D); else return findhelp(subroot->right(), coord, e, (discrim+1)%D); }

Advanced Trees31 K-D Insert  Insert into a K-D tree is similar to BST insertion First search until a NULL pointer is found Insert the new record into the proper child pointer

Advanced Trees32 K-D delete  K-D delete is more complicated than BST delete. To delete a node, N: If N has no children, replace it with a NULL If N has two children, we must find the smallest value in the right subtree. However we must find the smallest value for the same discriminator Not necessarily leftmost, since some branches are not based on this discriminator  Use a modified findmin() routine Then we call delete recursively to remove the min node.

Advanced Trees33 K-D delete example A (30, 50) B (20, 40)C (32, 70) D (25, 33)E (15, 72)F (52, 12)G (35, 88) H (33, 74)I (37, 92) X Y X Y

Advanced Trees34 KDTree::findmin() BinNode* KDtree::findmin(BinNode *subroot, int discrim, int currdis) const { BinNode *temp1, *temp2; int *coord, *t1coord, *t2coord; if (subroot == NULL) return NULL; coord = subroot->coord(); temp1 = find findmin(subroot->left(), discrim, (currdis+1)%D); if (temp1 != NULL) t1coord = temp1->coord(); if (discrim != currdis) { // Min could be on either side: temp2 = findmin(subroot->right(), discrim, (currdis+1)%D); if (temp2 != NULL) t2coord = temp2->coord(); if ((temp1 == NULL) || ((temp2 != NULL) && t2coord[discrim] < t1coord[discrim]))) temp1 = temp2; } // Now temp1 has the smallest value of subroot’s children if ((temp1 == NULL) || (coord[discrim]<t1coord[discrim])) return subroot; else return temp1; }

Advanced Trees35 Deleting (2)  If there is no right subtree, we can’t just find the max value in the left subtree, because it might be duplicated, and duplicates belong in the right subtree.  Instead, we can move the left subtree to the right and then replace the node to be deleted with the minimum value, just as before

Advanced Trees36 Radius search  Suppose we want all points within distance d of a query point  When the difference between the query point and the search point is greater than d in any dimension, the query point clearly cannot be within distance d We can disregard an entire subtree at a time

Advanced Trees37 Radius Search (2)  Search for all points within 25 units of (25,65) Root (A) distance = 25, report Root node: x = 40 – check both children Report B, no children Do not report C, check children  No left child. Right child: y  10, must be checked Do not report D, check children  Left: x < 69 – much check  Right: x  69 – no children can match, skip entire subtree Check E, do not report x = 50 25, 65 x = 0y = 40 y = 90

Advanced Trees38 The PR Quadtree  Like a BST, the location of the cuts in a K-D tree depend on the objects and the order in which they are presented  The equivalent of a trie for spatial data structures is the PR (Point-Region) Quadtree  Every node has four children, which cut the x and y dimensions in half Three-dimensional equivalent is an octree

Advanced Trees39 Quadtrees  Nodes have four children or none A B C D E AB E DC NW NE SW SE (30, 90)(95, 85) (98, 35)(117, 52) (110,25) This quadtree will result, no matter what order the data are presented in.