Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.

Slides:



Advertisements
Similar presentations
CSE 4101/5101 Prof. Andy Mirzaian Augmenting Data Structures.
Advertisements

Fenwick Tree (binary indexed tree).
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Segment Trees. Longest Non Decreasing Subsequence (Revisited) Length of LNDS ending at i th Loc This runs in O(n 2 ). Can we do.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
Segment Trees Extremely versatile. Basic Segment Tree void add(int pos, int val); int range_sum(int start, int end); Three ways below of visualizing it.
CS 171: Introduction to Computer Science II
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
10. Binary Trees A. Introduction: Searching a linked list.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Lecture 06: Tree Structures Topics: Trees in general Binary Search Trees Application: Huffman Coding Other types of Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
COSC2007 Data Structures II
Data Structure (III) GagGuy.
A Level Computer Science Topic 9: Data Structures T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
“On an Algorithm of Zemlyachenko for Subtree Isomorphism” Yefim Dinitz, Alon Itai, Michael Rodeh (1998) Presented by: Masha Igra, Merav Bukra.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture1 introductions and Tree Data Structures 11/12/20151.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Segment Trees Basic data structure in computational geometry. Computational geometry.  Computations with geometric objects.  Points in 1-, 2-, 3-, d-space.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Discrete Mathematics Chapter 5 Trees.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Fat heaps (K & Tarjan 96). 2 Goal Want to achieve the performance of Fibonnaci heaps but on the worst case. Why ? Theoretical curiosity and some applications.
Priority Search Trees Keys are pairs (x,y). Basic (search, insert, delete) and rectangle operations. Two varieties.  Based on a balanced binary search.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Interval Trees Marco Gallotta. Problem ● Given a collection of items i, each with value V i ● Want to answer many queries of the form: How many items.
Range Trees IOI Training Camp Keegan C-Smith.
Trees Chapter 15.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary Search Trees Rem Collier Room A1.02
Week 6 - Wednesday CS221.
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Data Structures: Segment Trees, Fenwick Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Segment Trees Basic data structure in computational geometry.
Math 221 Huffman Codes.
Chapter 14: Augmenting Data Structures
Binary Trees, Binary Search Trees
Tree.
Binary Trees, Binary Search Trees
A Binary Tree is a tree in which each node has at most 2 children
Honors Track: Competitive Programming & Problem Solving Binary Indexed Trees Christiaan Dirkx.
Presentation transcript:

Trees By Charl du Plessis

Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed Trees Binary Indexed Trees

Basic Terminology Connected graph with no cyclesConnected graph with no cycles Unique path from every vertex to every other vertexUnique path from every vertex to every other vertex E = V-1 in any tree, where E and V are the number of edges and vertices respectively.E = V-1 in any tree, where E and V are the number of edges and vertices respectively. Root Leaves

Binary Search Trees Structure? Structure? Operations: O(h) Operations: O(h) - query - delete - insert Cons: Degenerate trees make all operations Worst case O(n) But can be “self-balanced” such as in Red-Black Trees. C++ stl uses self-balancing binary trees

Interval Trees: Problem Consider the problem: You have a set of n items; 1,…, n You have a set of n items; 1,…, n There is some number of each item There is some number of each item You want to increment the number of each item in range [x,y] by one. (for each i, x <= i <= y) You want to increment the number of each item in range [x,y] by one. (for each i, x <= i <= y) And query how many of item i there are And query how many of item i there are

Interval Trees: Structure Use a binary tree, where each node holds a count over range [A,B] Use a binary tree, where each node holds a count over range [A,B] Root holds count over [1,n] Root holds count over [1,n] Node ranging over [A,B] will have children [A, (A+B)/2] and [(A+B)/2+1, B] Node ranging over [A,B] will have children [A, (A+B)/2] and [(A+B)/2+1, B] The leaves of the tree will store count over a single item range The leaves of the tree will store count over a single item range

Interval Trees: Representation Representation of tree: Use an array Use an array Index nodes from 1 Index nodes from 1 Children of node i will be 2*i and 2*i +1 Children of node i will be 2*i and 2*i

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (0) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (3) 7 [4,4] (0)

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (0) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (3) 7 [4,4] (0) [x,y] = [3,4]

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (1) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (3) 7 [4,4] (0) [x,y] = [3,4]

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (1) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (3) 7 [4,4] (0) [x,y] = [2,4]

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (1) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (3) 7 [4,4] (0) [x,y] = [2,4]

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (1) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (4) 7 [4,4] (0) [x,y] = [2,4]

Interval Trees: Updating a range 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (2) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (4) 7 [4,4] (0) [x,y] = [2,4]

Interval Trees: Updating Code Updating a range [x,y]: Recurse into the tree, starting at root Recurse into the tree, starting at root Suppose [A,B] is the current interval being considered: Suppose [A,B] is the current interval being considered: If [x,y] overlaps [A,B]: Increment count of node Else if [A,B] contains none of [x,y]: StopElse: Recurse into Left Child. Recurse into Right Child.

Interval Trees: Query 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (2) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (4) 7 [4,4] (0) Retrieve value of item 3 1

Interval Trees: Query 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (2) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (4) 7 [4,4] (0) Retrieve value of item 3 1+2

Interval Trees: Query 1 [1,4] (1) 2 [1,2] (3) 3 [3,4] (2) 4 [1,1] (1) 6 [3,3] (2) 5 [2,2] (4) 7 [4,4] (0) Retrieve value of item = 5

Interval Trees: Query Code Querying the number of item i: - Let sum = 0 - Recurse into tree. - Considering range [A,B]: sum += count of [A,B] If (A != B): Recurse into child containing i in its range.

Binary Indexed Trees: Problem Problem: Suppose we have a row of n numbers. We want to: 1.) Increment value of the ith number 2.) Query the sum of the values in range [k,j] [k,j] BIT implementation: - Query can be done in O(log n) for worst case - Short code Radix trees are equivalent in efficiency

Binary Indexed Trees: Introduction Interested in counts over ranges of the form [1,i] Interested in counts over ranges of the form [1,i] Uses the fact every number can be written as the sum of powers of 2. Uses the fact every number can be written as the sum of powers of 2. We will use this to represent ranges [1,i] We will use this to represent ranges [1,i] 13 = = [1, 13] = [1, 8] + [9, 12] + [13, 13] [1, 13] = [1, 8] + [9, 12] + [13, 13]

Binary Indexed Trees: Intervals We store interval counts of the form [i -2^r+1, i] where r is the position of the last non-zero digit of i in binary form We store interval counts of the form [i -2^r+1, i] where r is the position of the last non-zero digit of i in binary form Example: Example: 13 = 1011 in binary, position of last non-zero digit is 0. 4 = 100 in binary, position of last non-zero digit is 2. How it works will become clear when we explain the structure of the tree How it works will become clear when we explain the structure of the tree

Binary Indexed Trees: Structure f[i] = value at index i c[i] = f[1] + f[2] + … + f[i] tree[i] = count of [i-2^r +1,i] i(i-2^r+1)…i 11 21… … … … … … … …16

Binary Indexed Trees: Structure

Binary Indexed Trees: Query int read(int idx) { int sum = 0; while (idx > 0) { sum += tree[idx]; idx -= (idx & -idx); } return sum; }

Binary Indexed Trees: Update void update(int idx,int val) { while (idx <= MaxVal) { tree[idx] += val; idx += (idx & -idx); }

Questions ?