Paolo Ferragina Dipartimento di Informatica, Università di Pisa

Slides:



Advertisements
Similar presentations
Interval Heaps Complete binary tree. Each node (except possibly last one) has 2 elements. Last node has 1 or 2 elements. Let a and b be the elements in.
Advertisements

An Improved Succinct Dynamic k-Ary Tree Representation (work in progress) Diego Arroyuelo Department of Computer Science, Universidad de Chile.
Paolo Ferragina, Università di Pisa Compressed Rank & Select on general strings Paolo Ferragina Dipartimento di Informatica, Università di Pisa.
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Trees CS 110: Data Structures and Algorithms First Semester,
Succinct Representations of Trees S. Srinivasa Rao Seoul National University.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Mike 66 Sept Succinct Data Structures: Techniques and Lower Bounds Ian Munro University of Waterloo Joint work with/ work of Arash Farzan, Alex Golynski,
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
B+ Trees COMP
Basics of Data Compression Paolo Ferragina Dipartimento di Informatica Università di Pisa.
Succinct Representations of Trees
Introduction n – length of text, m – length of search pattern string Generally suffix tree construction takes O(n) time, O(n) space and searching takes.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Summer School '131 Succinct Data Structures Ian Munro.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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:
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Tree Data Structures.
Succinct Data Structures Ian Munro University of Waterloo Joint work with David Benoit, Andrej Brodnik, D, Clark, F. Fich, M. He, J. Horton, A. López-Ortiz,
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Random access to arrays of variable-length items
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
Binary Search Trees ©Robert E. Tarjan Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine.
Index construction: Compression of postings Paolo Ferragina Dipartimento di Informatica Università di Pisa Reading 5.3 and a paper.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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
Index construction: Compression of postings
Analysis of Algorithms
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Red-Black Trees v z Red-Black Trees Red-Black Trees
Succinct Data Structures
Recursive Objects (Part 4)
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Multiway search trees and the (2,4)-tree
AVL Tree.
Paolo Ferragina Dipartimento di Informatica, Università di Pisa
Section 8.1 Trees.
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
Lecture 25 Splay Tree Chapter 10 of textbook
Binomial Heaps Chapter 19.
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.
Data Structures Lecture 27 Sohail Aslam.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees v z Red-Black Trees Red-Black Trees
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
CS 583 Analysis of Algorithms
Trees & Forests D. J. Foreman.
Index construction: Compression of postings
Forests D. J. Foreman.
Trees (Part 1, Theoretical)
Paolo Ferragina Dipartimento di Informatica, Università di Pisa
CS 583 Analysis of Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Trees Addenda.
Solution for Section Worksheet 4, #7b & #7c
Index construction: Compression of postings
CS 261 – Data Structures Trees.
Tree.
Index construction: Compression of postings
Chapter 20: Binary Trees.
Succinct Data Structures
Tree and its terminologies
Presentation transcript:

Paolo Ferragina Dipartimento di Informatica, Università di Pisa Representing Trees Paolo Ferragina Dipartimento di Informatica, Università di Pisa Paolo Ferragina, Università di Pisa

Standard representation Binary tree: each node has two pointers to its left and right children An n-node tree takes 2n pointers or 2n lg n bits. Supports finding left child or right child of a node (in constant time). For each extra operation (eg. parent, subtree size) we have to pay additional n lg n bits each. x x x x x x x x x

Can we improve the space bound? There are less than 22n distinct binary trees on n nodes. 2n bits are enough to distinguish between any two different binary trees. Can we represent an n node binary tree using 2n bits, and still be able to navigate it in constant time ?

Binary tree representation A binary tree on n nodes can be represented using 2n+o(n) bits to support: parent left child right child in constant time.

Heap-like notation for a binary tree 1 Add external nodes 1 1 Label internal nodes with a 1 and external nodes with a 0 1 1 1 1 1 Write the labels in level order 1 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 0 One can reconstruct the tree from this sequence An n node binary tree can be represented in 2n+1 bits. What about the operations?

Heap-like notation for a binary tree 1 x  x: # 1’s up to x (Rank1(x)) x  x: position of x-th 1 (Select1(x)) 1 2 3 2 3 4 5 6 7 Let x be the code of an internal node: left child(x) = 2x is the code on green right child(x) = 2x+1 is the code on green If bit is 0 then pointer is NULL 4 5 6 12 8 9 10 11 13 7 8 14 15 16 17 Let x be the green code of an internal node or a leaf: - parent(x) = ⌊x/2⌋ is the code on red 1 2 3 4 5 6 7 8 1 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Arbitrary fan-out A rooted ordered tree (on n nodes, arbitrary fan-out): Navigational operations: - parent(x) = a - first child(x) = b - next sibling(x) = c Other useful operations: - degree(x) = 2 - subtree size(x) = 4 a x c b

Level-order degree sequence (LOUDS) 3 Write the degree sequence in level order 3 2 0 3 0 1 0 2 0 0 0 0 2 3 But, this still requires n lg n bits 1 2 Solution: write degree k in unary 1k 0 3 2 0 3 0 1 0 2 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0 Takes 2n-1 bits (every node is represented twice, as 0 and as 1, except the root represented only as 0) A tree is uniquely determined by its degree sequence

Supporting operations Add a dummy root so that each node has a corresponding 1 1 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 1 Node k corresponds to the k-th 1 in the sequence Children of k correspond to the 1s following the k-th 0 3 4 2 First_child(k): y=Select_0(k)+1 if B[y] = 0 then leaf else return y-k [i.e. #1 up to y] Next sibling(k) = k+1 if B[Select_1(k)+1] ≠ 0 Parent(k) = # 0’s up to the k-th 1 7 9 5 6 8 degree(k) = Select_0(k+1) – ( Select_0(k) + 1 ) In 2n+o(n) bits and constant time per ops. No support for subtree size. 10 11 12