Algorithms and complexity: 2 The dictionary problem: search trees

Similar documents
18. Binary Search Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Algorithms. AVL Tree

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

9. Binary Search Trees

We have the pointers reference the next node in an inorder traversal; called threads

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Multi-Way Search Tree

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

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

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Programming II (CS300)

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

Principles of Computer Science

Binary Trees, Binary Search Trees

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Balanced Search Trees

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

Fall, 2015 Prof. Jungkeun Park

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

TREES. Trees - Introduction

CS102 Binary Search Trees

Tree traversals and binary trees

3 Trees: traversal and analysis of standard search trees

Data Structures in Java

Data Structures in Java

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS24 Week 8 Lecture 1

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

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

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

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

Laboratory Module Trees

Multiway Search Trees. Multiway-Search Trees (cont d)

Lecture 23: Binary Search Trees

SFU CMPT Lecture: Week 9

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

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

CS 315 Data Structures mid-term 2

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

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

3 Trees: traversal and analysis of standard search trees. Summer Term 2010

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

Unit 9 Practice Test (AB27-30)

Dynamic Access Binary Search Trees

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Advanced Set Representation Methods

Balanced Binary Search Trees. Victor Gao

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

INF2220: algorithms and data structures Series 1

CS350: Data Structures Binary Search Trees

We assume uniform hashing (UH):

DIT960 Datastrukturer

Data Structures. Motivation

Algorithms. Deleting from Red-Black Trees B-Trees

Augmenting Data Structures

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

V Advanced Data Structures

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

Spring 2018 Mentoring 8: March 14, Binary Trees

Second Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

Chapter 20: Binary Trees

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

CS171 Final Practice Exam

Data Structures and Algorithms

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

CMSC 341. Binary Search Trees CMSC 341 BST

What is a Multi-way tree?

Algorithms and Data Structures

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

CS 350 : Data Structures Binary Search Trees

TREES Lecture 12 CS2110 Fall 2016

V Advanced Data Structures

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

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

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

CMSC 341 Lecture 15 Leftist Heaps

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

CS-301 Data Structure. Tariq Hanif

Lecture 3: B-Trees. October Lecture 3: B-Trees

TREES Lecture 12 CS2110 Spring 2018

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

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

CSCI Trees. Mark Redekopp David Kempe

Search Trees - 1 Venkatanatha Sarma Y

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

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

Title Description Participants Textbook

CMSC 341 Lecture 15 Leftist Heaps

Transcription:

Algorithms and complexity: 2 The dictionary problem: search trees

The dictionary problem Given: a set of objects (data) where each element can be identified by a unique key (integer, string,... ). Goal: a structure for storing the set of keys such that at least the following operations (methods) are supported: search (find, access) insert delete Intuition: english-german dictionary 22.04.2012 Theory 1 - Search Trees 2

The dictionary problem (2) first id name next id name next id name next null class ListNode { int id; string name; ListNode next; Search(id) Insert? Delete? string SequentialSearch (int k){ n = first; while (n!= null) { if ( k == n.id) return n.name; n = n.next; return not found ; 22.04.2012 Theory 1 - Introduction 3

The dictionary problem (3) The following conditions can influence the choice of a solution to the dictionary problem: the frequency of the operations: mostly insertion and deletion (dynamic) mostly search (static) approximately the same frequencies other operations to be implemented: set operations: union, intersection, difference quantity,... enumerate the set in a certain order (e.g. ascending by key) the complexity of the solution: average case, worst case, amortized worst case the place where the data is stored: main memory, hard drive, WORM (write once read multiple) 22.04.2012 Theory 1 - Search Trees 4

The dictionary problem (3) Different approaches to the dictionary problem: structuring the complete universe of all possible keys: hashing structuring the set of the actually occurring keys: lists, trees, graphs,... 22.04.2012 Theory 1 - Search Trees 5

Trees Trees are a generalisation of linked lists (each element can have more than one successor) first id name next id name next id name next null 22.04.2012 Theory 1 - Search Trees 6

Trees as graphs (1) Trees are special graphs: in general, a graph G = (N,E) consists of a set N of nodes and a set E of edges the edges are either directed or undirected nodes and edges can be labelled a tree is a connected acyclic graph, where: # nodes = # edges + 1 a general and central concept for the hierarchical structuring of information: decision trees code trees syntax trees 22.04.2012 Theory 1 - Search Trees 7

Trees as graphs (2) Several kinds of trees can be distinguished: undirected tree (with no designated root) 1 rooted tree (one node is designated as the root) 5 1 5 3 4 3 4 2 2 6 Root from each node k there is exactly one path (a sequence of pairwise neighbouring edges) to the root the parent (or: direct predecessor) of a node k is the first neighbour on the path from k to the root the children (or: direct successors) are the other neighbours of k the rank (or: outdegree) of a node k is the number of children of k 6 22.04.2012 Theory 1 - Search Trees 8

Trees as graphs (3) Rooted tree: root: the only node that has no parent leaf nodes (leaves): nodes that have no children internal nodes: all nodes that are not leaves order of a tree T: maximum rank of a node in T the notion tree is often used as a synonym for rooted tree Ordered (rooted) tree: among the children of each node there is an order e.g. the < relation among the keys of the nodes 4 3 < 5 < 6 1 < 2 Binary tree: ordered tree of order 2; the children of a node are referred to as left child and right child Multiway tree: ordered tree of order > 2 22.04.2012 Theory 1 - Search Trees 9

Trees as graphs (4) A more precise definition of the set M d of the ordered rooted trees of order d (d 1): a single node is in M d let t 1,...,t d M d and w a node. Then w with the roots of t 1,...,t d as its children (from left to right) is a tree t M d. The t i are subtrees of t. according to this definition each node has rank d (or rank 0) in general, the rank can be d w nodes of binary trees either have 0 or 2 children nodes with exactly 1 child could also be permitted by allowing empty subtrees in the above definition... t 1 t 2 t d 22.04.2012 Theory 1 - Search Trees 10

Examples tree not a tree not a tree (but two trees!) 22.04.2012 Theory 1 - Search Trees 11

Structural properties of trees Depth of a node k: # edges from the tree root until k (distance of k to the root) Height h(t) of a tree t: maximum depth of a leaf in t. Alternative (recursive) definition: h(leaf) = 0 h(t) = 1 + max{t i root of t i is a child of the root of t (t i is a subtree of t) Level i: all nodes of depth i Complete tree: tree where each non-empty level has the maximum number of nodes. all leaves have the same depth. 22.04.2012 Theory 1 - Search Trees 12

Applications of trees Use of trees for the dictionary problem: node: stores one key tree: stores a set of keys enumeration of the complete set of data 22.04.2012 Theory 1 - Search Trees 13

Standard binary search trees (1) Goal: Storage, retrieval of data (more general: dictionary problem) Two alternative ways of storage: search trees: keys are stored in internal nodes leaf nodes are empty (usually = null), they represent intervals between the keys leaf search trees: keys are stored in the leaves internal nodes contain information in order to direct the search for a key Search tree condition: For each internal node k: all keys in the left subtree t l of k are less (<) than the key in k and all keys in the right subtree t r of k are greater (>) than the key in k k t l t r 22.04.2012 Theory 1 - Search Trees 14

Standard binary search trees (2) 9 3 12 4 How can the search for key s be implemented? (leaf null) k = root; while (k!= null) { if (s == k.key) return true; if (s < k.key) k = k.left; else k = k.right return false; 22.04.2012 Theory 1 - Search Trees 15

Example (without stop mode) Search for key s ends in the internal node k with k.key == s or in the leaf whose interval contains s Root 27 3 39 1 15 14 22.04.2012 Theory 1 - Search Trees 16

Standard binary search trees (3) Leaf search tree: keys are stored in leaf nodes clues (routers) are stored in internal nodes, such that s l s k s r (s l : key in left subtree, s k : router in k, s r : key in right subtree) = should not occur twice in the above inequality choice of s: either maximum key in t l (usual) or minimum key in t r. 22.04.2012 Theory 1 - Search Trees 17

Example: leaf search tree Leaf nodes store keys, internal nodes contain routers. 22.04.2012 Theory 1 - Search Trees 18

Example: leaf search tree Leaf nodes store keys, internal nodes contain routers. 1 9 12 5 6 22.04.2012 Theory 1 - Search Trees 19

Example: leaf search tree Leaf nodes store keys, internal nodes contain routers. 6 1 9 1 5 5 6 9 12 22.04.2012 Theory 1 - Search Trees 20

Standard binary search trees (4) How is the search for key s implemented in a leaf search tree? (leaf = node with 2 null pointers) k = root; if (k == null) return false; while (k.left!= null) { // thus also k.right!= null if (s <= k.key) k = k.left; else k = k.right; // now in the leaf return s==k.key; In the following we always talk about search trees (not leaf search trees). 6 1 9 1 5 5 6 9 12 22.04.2012 Theory 1 - Search Trees 21

Standard binary search trees (5) class SearchNode { int content; SearchNode left; SearchNode right; SearchNode (int c){ // Constructor for a node content = c; // without successor left = right = null; //class SearchNode class SearchTree { SearchNode root; SearchTree () { //... root = null; // Constructor for empty tree 22.04.2012 Theory 1 - Search Trees 22

Standard binary search trees (6) /* Search for c in the tree */ boolean search (int c) { return search (root, c); boolean search (SearchNode n, int c){ while (n!= null) { if (c == n.content) return true; if (c < n.content) n = n.left; else n = n.right; return false; 22.04.2012 Theory 1 - Search Trees 23

Standard binary search trees (7) Alternative tree structure: instead of leaf null, set leaf pointer to a special stop node b for searching, store the search key s in b to save comparisons in internal nodes. Use of a stop node for searching! 22.04.2012 Theory 1 - Search Trees 24

Example (with stop mode) Wurzel 27 3 39 1 15 14 x 22.04.2012 Theory 1 - Search Trees 25

Standard binary search trees (7) Insertion of a node with key s in search tree t. Search for s: 1. search for s ends in a node with s: don t insert (otherwise, there would be duplicated keys) 2. search ends in leaf b: make b an internal node with s as its key and two new leaves. tree remains a search tree! 22.04.2012 Theory 1 - Search Trees 26

Standard binary search trees (8) 9 Insert 5 9 3 12 3 12 4 4 5 Tree structure depends on the order of insertions into the initially empty tree Height can increase linearly, but it can also be in O(log n), more precisely [log 2 (n+1)]. 22.04.2012 Theory 1 - Search Trees 27

Standard binary search trees (9) int height() { return height(root); int height(searchnode n){ if (n == null) return 0; else return 1 + Math.max(height(n.left), height(n.right)); /* Insert c into tree; return true if successful and false if c was in tree already */ boolean insert (int c) { // insert c if (root == null){ root = new SearchNode (c); return true; else return insert (root, c); 22.04.2012 Theory 1 - Search Trees 28

Standard binary search trees (10) boolean insert (SearchNode n, int c){ while (true){ if (c == n.content) return false; if (c < n.content){ if (n.left == null) { n.left = new SearchNode (c); return true; else n = n.left; else { // c > n.content if (n.right == null) { n.right = new SearchNode (c); return true; else n = n.right; 22.04.2012 Theory 1 - Search Trees 29

Special cases The structure of the resulting tree depends on the order, in which the keys are inserted. The minimal height is [log 2 (n+1)] and the maximal height is n. Resulting search trees for the sequences 15, 39, 3, 27, 1, 14 and 1, 3, 14, 15, 27, 39: 1 15 3 14 3 39 1 14 27 15 27 39 22.04.2012 Theory 1 - Search Trees 30

Standard binary search trees (11) A standard tree is created by iterative insertions in an initially empty tree. Which trees are more frequent/typical: the balanced or the degenerate ones? How costly is an insertion? 22.04.2012 Theory 1 - Search Trees 31

Standard binary search trees (11) Deletion of a node with key s from a tree (while retaining the search tree property) Search for s: if search fails: done. otherwise search ends in node k with k.key == s and k has no child, one child or two children: a) no child: done (set the parent s pointer to null instead of k) b) only one child: let k s parent v point to k s child instead of k c) two children: search for the smallest key in k s right subtree, i.e. go right and then to the left as far as possible until you reach p (the symmetrical successor of k); copy p.key to k, delete p (which has at most one child, so follow step (a) or (b)) 22.04.2012 Theory 1 - Search Trees 32

Symmetrical successor Definition: A node q is called the symmetrical successor of a node p if q contains the smallest key greater than or equal to the key of p. Observations: the symmetrical successor q of p is leftmost node in the right subtree of p. the symmetrical successor has at most one child, which is the right child. 22.04.2012 Theory 1 - Search Trees 33

Finding the symmetrical successor Observation: If p has a right child, the symmetrical successor always exists. First go to the right child of p. From there, always proceed to the left child until you find a node without a left child. p q 22.04.2012 Theory 1 - Search Trees 34

Idea of the delete operation Delete p by replacing its content with the content of its symmetrical successor q. Then delete q. Deletion of q is easy because q has at most one child. p x p x q y q y 22.04.2012 Theory 1 - Search Trees 35

Example k has no internal child, one internal child or two internal children: a) v v k s b) v v k s t l t l c) v v d) v k s t r k s t r t l p t r 22.04.2012 Theory 1 - Search Trees 36

Standard binary search trees (12) boolean delete(int c) { return delete(null, root, c); // delete c from the tree rooted in n, whose parent is vn boolean delete(searchnode vn, SearchNode n, int c) { if (n == null) return false; if (c < n.content) return delete(n, n.left, c); if (c > n.content) return delete(n, n.right, c); // now we have: c == n.content if (n.left == null) { point (vn, n, n.right); return true; if (n.right == null) { point (vn, n, n.left); return true; //... 22.04.2012 Theory 1 - Search Trees 37

Standard binary search trees (13) // now n.left!= null and n.right!= null SearchNode q = psymsucc(n); if (n == q) { // right child of q is SymSucc(n) n.content = q.right.content; q.right = q.right.right; return true; else { // left child of q is SymSucc(n) n.content = q.left.content; q.left = q.left.right; return true; // boolean delete(searchnode vn, SearchNode n, int c) // returns the parent of the symmetrical successor SearchNode psymsucc(searchnode n) { if (n.right.left!= null) { n = n.right; while (n.left.left!= null) n = n.left; return n; 22.04.2012 Theory 1 - Search Trees 38

Standard binary search trees (14) // let vn point to m instead of n; // if vn == null, set root pointer to m void point(searchnode vn, SearchNode n, SearchNode m) { if (vn == null) root = m; else if (vn.left == n) vn.left = m; else vn.right = m; 22.04.2012 Theory 1 - Search Trees 39