Hierarchical Data in RDBMS

Size: px
Start display at page:

Download "Hierarchical Data in RDBMS"

Transcription

1 Hierarchical Data in RDBMS

2 Introduction There are times when we need to store "tree" or "hierarchical" data for various modelling problems: Categories, sub-categories and sub-sub-categories in a manufacturing company Navigation menus of complex sitemaps Relationship of members of a multilevel marketing system Data organization and indexing should provide the following: Display of results (how do you build a navigation tree?) Report on statistics (how many people are in a multilevel marketing branch?) Extract a specific segment of the tree desired (you want to show the products with categories within a certain subcategory)

3 Introduction The tables of a relational database are not hierarchical (except perhaps XML), they are simply a flat list. Hierarchical data is defined as a set of data items that are related to each other by hierarchical relationships. Hierarchical relationships exist where one item of data is the parent of another item. Hierarchical data relationship is not naturally suitable to be represented in a relational database table. In what follows, most of the time we consider hierarchical data to be a collection of data where each item has a single parent and zero or more children (with the exception of the root item, which has no parent).

4 Data Modelling The main role of a database is to store data into it for searching purposes. As a consequence, the DBMS should supply means to retrieve various relationship between nodes (such as "subtree", "parent node", "child nodes", "ancestor nodes", "descendant nodes"). The role of a model is to facilitate finding related nodes of any node efficiently. There are some DBMSs that offer support for some hierarchical representations or hierarchical queries

5 Modelling Options Adjacency List Nested Set (Modified Preorder Tree Traversal) Nested Intervals Bridge Table (Closure Table) Path Enumeration (Materialized Path, Lineage Column) Flat Table Multiple lineage columns Fertile Forest Model

6 Model Comparison In order to compare the models in the presentation, we need to address the same problems for each of them in particular: storage complexity and size CRUD operations complexity ease of displaying full tree finding the root node finding all leaf nodes find all descendants of a given node find all ancestors of a given node find all descendants of a given node up to a certain depth Not all operations are covered with examples

7 Adjacency List In the adjacency list model, each item in the table contains a pointer to its parent. The topmost element has a NULL value for its parent. The adjacency list model has the advantage of being quite simple. Adding a new record to the system only requires to know the parent, with no other indexing. While the adjacency list model can be dealt with fairly easily in client-side code with ADTs, working with the model can be more problematic in pure SQL (set based).

8 Adjacency List Suppose that we intend to store the following hierarchy: [A]-+-[B]-+-[D] +-[E] +-[C]-+-[F] +-[G]-+-[H] +-[I]

9 Adjacency List CREATE TABLE adjacency_list_models ( id integer NOT NULL UNIQUE, name varchar(225) DEFAULT NULL, parent_id integer DEFAULT NULL, PRIMARY KEY (id) ); INSERT INTO adjacency_list_models (id, name, parent_id) VALUES (1, 'A', NULL), (2, 'B', 1), (3, 'C', 1), (4, 'D', 2), (5, 'E', 2), (6, 'F', 3), (7, 'G', 3), (8, 'H', 7), (9, 'I', 7);

10 Adjacency List In order to retrieve the full tree: SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4 FROM adjacency_list_models AS t1 LEFT JOIN adjacency_list_models AS t2 ON t2.parent_id = t1.id LEFT JOIN adjacency_list_models AS t3 ON t3.parent_id = t2.id LEFT JOIN adjacency_list_models AS t4 ON t4.parent_id = t3.id WHERE t1.name = 'A'; The query depends on the number of levels of the tree, unmanageable for larger trees

11 Adjacency List In order to retrieve the full tree, some DBMSs have features to build the result independent of the number of levels For example, in PostgreSQL: CREATE RECURSIVE VIEW tree (id, ancestors) AS ( SELECT id, '{}'::integer[] FROM adjacency_list_models WHERE parent_id IS NULL UNION ALL SELECT n.id, t.ancestors n.parent_id FROM adjacency_list_models n, tree t WHERE n.parent_id = t.id); select * from tree; SELECT name, id FROM adjacency_list_models INNER JOIN tree USING (id) WHERE 7 = ANY(ancestors);

12 Adjacency List Finding all leaf nodes: SELECT t1.name FROM adjacency_list_models AS t1 LEFT JOIN adjacency_list_models as t2 ON t1.id = t2.parent_id WHERE t2.id IS NULL; Find the root of the tree SELECT name FROM adjacency_list_models WHERE parent_id is NULL;

13 Adjacency List Pros: Lightweight storage of data (only one field parent_id required to build an entire affiliate tree) Adding a new row doesn t alter any other row Moving a subtree to a different location only requires a change to one row in the database Not possible to obtain referential integrity errors, the database prevents deleting a parent row when still having children Cons: Very intensive overhead to report For each find level in the tree (how deep you are) requires a new query Hard to find all descendants/ancestors for a given node

14 Nested Sets The nested set model is to number the nodes according to a tree traversal, which visits each node twice, assigning numbers in the order of visiting, and at both visits. This leaves two numbers for each node, which are stored as two attributes. Querying becomes inexpensive: hierarchy membership can be tested by comparing these numbers. Updating requires renumbering and is therefore expensive. Refinements that use rational numbers instead of integers can avoid renumbering, and so are faster to update, although much more complicated. When considering each node as a circle, Nested Set Model represents the parent-child relationship by nested circles.

15 Nested Sets Consider the same tree structure as for previous model In order to create the traversal numerotation of each node, the following algorithm has to be applied, starting with root node and value 1: If current node has no left number, give a number in left of current node. If the current node has a child node that has no left number, move to the left-most child node. Return to first step. If the current node has no child node which has no left number, assign to current node a right number and move to parent node. Returns to first step. Exit if the current node has no parent node.

16 Nested Sets

17 Nested Sets From the picture can be seen that the left and right numbers of each node are within the range of the left and right of its parent node. For example, the left and right number of [G] is (11, 16). And the left and right number of [H] and [I] is within the range of [G]. Therefore, can determine [H] and [I] to be a descendant node of [G]. Each set of nodes having a common ancestor is nested within the node of this ancestor. That's why this model is called "nested sets".

18 Nested Sets CREATE TABLE nested_set_models ( id integer NOT NULL UNIQUE, name varchar(225) DEFAULT NULL, lft integer NOT NULL, rgt integer NOT NULL, PRIMARY KEY (id) ); INSERT INTO nested_set_models (id, name, lft, rgt) VALUES (1, 'A', 1, 18), (2, 'B', 2, 7), (3, 'C', 8, 17), (4, 'D', 3, 4), (5, 'E', 5, 6), (6, 'F', 9, 10), (7, 'G', 11, 16), (8, 'H', 12, 13), (9, 'I', 14, 15); SELECT * FROM nested_set_models;

19 Nested Sets Retrieving the full tree SELECT node.name FROM nested_set_models AS node, nested_set_models AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND parent.name = 'A' ORDER BY node.lft;

20 Nested Sets Find all leaf nodes: SELECT name FROM nested_set_models WHERE rgt = lft + 1; Find root node: SELECT name FROM nested_set_models WHERE lft = 1;

21 Nested Sets Find children of a node: SELECT DISTINCT child.name, child.lft, child.rgt FROM nested_set_models as child, nested_set_models as parent WHERE parent.lft < child.lft AND parent.rgt > child.rgt GROUP BY child.name, child.lft, child.rgt HAVING max(parent.lft) = 1 ; or SELECT Child.name, Child.lft, Child.rgt FROM nested_set_models as Parent, nested_set_models as Child WHERE Child.lft BETWEEN Parent.lft+1 AND Parent.rgt-1 AND NOT EXISTS ( SELECT * FROM nested_set_models as Mid WHERE Mid.lft BETWEEN Parent.lft AND Parent.rgt AND Child.lft BETWEEN Mid.lft AND Mid.rgt AND Mid.name NOT IN (Parent.name, Child.name) ) AND Parent.lft = 1 ;

22 Nested Sets Pros Reporting is easy and lightweight Quick selection of downline sets Queries using nested sets can be expected to be faster than queries using a stored procedure to traverse an adjacency list, and so are the faster option for databases which lack native recursive query constructs Cons Nested sets are very slow for inserts because it requires updating left and right domain values for all records in the table after the insert Report details (such as finding the number of levels in the child-set of records) can require complex queries

23 Closure Table This model uses two tables for managing hierarchical data. First table stores information of each node, and second table stores the relationships between nodes. Second table is referred as "closure table". The closure table has two columns for modelling hierarchical relationship. The columns contain the relationship between the ancestor nodes and each node. The closure table stores all paths from one node in the tree to another. This solution allows for fast node traversals of the tree. The tradeoff is obviously space. Consider the same tree structure as for previous model

24 Closure Table CREATE TABLE closure_table_models ( id integer NOT NULL UNIQUE, parent_id integer, -- NOT NEEDED name varchar(225) DEFAULT NULL, PRIMARY KEY (id) ); CREATE TABLE closure_table_relations ( ancestor_id integer NOT NULL, descendant_id integer NOT NULL, depth integer NOT NULL DEFAULT 0, PRIMARY KEY (ancestor_id, descendant_id) );

25 Closure Table INSERT INTO closure_table_models (id, parent_id,name) VALUES (1, 0, 'A'),(2, 1, 'B'),(3, 1, 'C'),(4, 2, 'D'), (5, 2, 'E'), (6, 3, 'F'),(7, 3, 'G'),(8, 7, 'H'),(9, 7, 'I'); INSERT INTO closure_table_relations VALUES (1, 1, 0), (2, 1, 1), (2, 2, 0), (3, 1, 1), (3, 3, 0), (4, 1, 2), (4, 2, 1), (4, 4, 0), (5, 1, 2), (5, 2, 1), (5, 5, 0), (6, 1, 2), (6, 3, 1), (6, 6, 0), (7, 1, 2), (7, 3, 1), (7, 7, 0), (8, 1, 3), (8, 3, 2), (8, 7, 1), (8, 8, 0), (9, 1, 3), (9, 3, 2), (9, 7, 1), (9, 9, 0);

26 Closure Table Retrieve full tree: SELECT c.name FROM closure_table_relations AS cp JOIN closure_table_models AS c ON c.id = cp.ancestor_id WHERE cp.descendant_id = 1; Retrieve children of a node: SELECT c.name FROM closure_table_relations AS cp JOIN closure_table_models AS c ON c.id = cp.ancestor_id WHERE cp.descendant_id = 1 AND cp.depth=1;

27 Closure Table Pros Inserts to the tree do not trigger updates on other nodes. They instead, populate the paths to the inserted node in the closure table. Moving a node only triggers updates to the node's own children. Using a "depth" or "path_length" column makes it easy to represent the whole tree structure. You can also easily query any specific level in the tree. Cons Storing the hierarchy in a closure table requires an additional table with a large number of rows. The closure table also won t work if you need to sort items by hierarchy, and re-parenting items is slow and costly.

28 Materialized Path "Materialized Path Model" is the model designed to be easier to find the relational nodes (such as sub-tree, ancestor nodes, etc.). The table of model has a column to save "path from the root node" for each node. The column manages a parent-child relationship in hierarchical data comprehensive. Also known as "Path enumeration model". In this approach each record stores the whole path to the root. First, create a unique key, which distinguishes the node's children. Then, list all the ancestor unique keys as the node's encoding. This list encoded as string is referred to as materialized path. Modern databases allow representing a list of nodes as a single value, but since materialized path has been invented long before then, the convention stuck to plain character string of nodes concatenated with some separator; most often '.' or '/'.

29 Materialized Path CREATE TABLE path_enumeration_models ( id integer NOT NULL UNIQUE, name varchar(225) DEFAULT NULL, tree_path varchar(225) DEFAULT NULL, PRIMARY KEY (id)); INSERT INTO path_enumeration_models (id, name, tree_path) VALUES (1, 'A', '1/' ), (2, 'B', '1/2/' ), (3, 'C', '1/3/' ), (4, 'D', '1/2/4/'), (5, 'E', '1/2/5/' ), (6, 'F', '1/3/6/' ), (7, 'G', '1/3/7/'), (8, 'H', '1/3/7/8/'), (9, 'I', '1/3/7/9/'); SELECT * FROM path_enumeration_models;

30 Materialized Path Retrieve full tree: select t1.name from path_enumeration_models t1, path_enumeration_models t2 where t1.tree_path like t2.tree_path '%' and t2.name = 'A' Ancestors of a given node: select t1.name from path_enumeration_models t1, path_enumeration_models t2 where t1.tree_path like t2.tree_path '%' and t2.name = 'I'

31 Materialized Path Ancestors of a given node (better performance): select t1.name from path_enumeration_models t1, path_enumeration_models t2 where t2.tree_path between t1.tree_path and t1.tree_path chr(255) and t2.name = 'I' Another way to determine the ancestors is to write a function that parses the path of the given node

32 Materialized Path Pros Performs well numerous inserts and deletes. Selects performs well but has typical gotchas LIKE '%.%'. (LTREE improves this with gist support) Using SQL92, easily returns all levels of the tree per statement and can limit to immediate levels Cons Analyzing tree structure is complicated

33 Summary Adjacency List: Columns: ID, ParentID Easy to implement. Cheap node moves, inserts, and deletes. Expensive to find level (can store as a computed column), ancestry & descendants Use Common Table Expressions in those databases that support them to traverse. Nested Set: Columns: Left, Right Cheap level, ancestry, descendants Requires a specific sort order (e.g. created). Sorting all descendants in a different order requires additional work.

34 Summary Nested Intervals Like nested set, but with real/float/decimal so that the encoding isn't volatile (inexpensive move/insert/delete) Have to deal with real/float/decimal representation issues Bridge Table (Closure Table) Columns: ancestor, descendant Stands apart from table it describes. Use triggers for maintaining this approach. Cheap ancestry and descendants For complete knowledge of a hierarchy needs to be combined with another option.

35 Summary Flat Table A modification of the Adjacency List that adds a Level and Rank (e.g. ordering) column to each record. Expensive move and delete Cheap ancestry and descendants Good Use: threaded discussion - forums / blog comments Lineage Column (Materialized Path, Path Enumeration) Column: lineage (e.g. /parent/child/grandchild/etc...) Limit to how deep the hierarchy can be. Descendants cheap Ancestry tricky (database specific queries)

36 Summary Multiple lineage columns Columns: one for each lineage level, refers to all the parents up to the root, levels down from the items level are set to NULL Limit to how deep the hierarchy can be Cheap ancestors, descendants, level Cheap insert, delete, move of the leaves Expensive insert, delete, move of the internal nodes

Trees & Hierarchies in SQL. Joe Celko Copyright 2009

Trees & Hierarchies in SQL. Joe Celko Copyright 2009 Trees & Hierarchies in SQL Joe Celko Copyright 2009 Trees in SQL Trees are graph structures used to represent Hierarchies Parts explosions Organizational charts Three major methods in SQL Adjacency list

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path

More information

Practical object-oriented models in SQL. Bill Karwin July 22, OSCON

Practical object-oriented models in SQL. Bill Karwin July 22, OSCON Practical object-oriented models in SQL Bill Karwin July 22, 2009 - OSCON Object-Oriented vs. Relational Impedance mismatch OO operates on instances; RDBMS operates on sets Compromise either OO strengths

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Hierarchies. QlikView Technical Brief. 26 Nov 2013, HIC

Hierarchies. QlikView Technical Brief. 26 Nov 2013, HIC Hierarchies QlikView Technical Brief 26 Nov 2013, HIC www.qlikview.com Contents Contents... 2 Introduction... 3 Examples in real life... 3 Different types of hierarchies... 4 Balanced or Unbalanced?...

More information

CSE 530A. B+ Trees. Washington University Fall 2013

CSE 530A. B+ Trees. Washington University Fall 2013 CSE 530A B+ Trees Washington University Fall 2013 B Trees A B tree is an ordered (non-binary) tree where the internal nodes can have a varying number of child nodes (within some range) B Trees When a key

More information

PASSWORDS TREES AND HIERARCHIES. CS121: Relational Databases Fall 2017 Lecture 24

PASSWORDS TREES AND HIERARCHIES. CS121: Relational Databases Fall 2017 Lecture 24 PASSWORDS TREES AND HIERARCHIES CS121: Relational Databases Fall 2017 Lecture 24 Account Password Management 2 Mentioned a retailer with an online website Need a database to store user account details

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

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

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

Trees. CSE 373 Data Structures

Trees. CSE 373 Data Structures Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories

More information

DATABASE PERFORMANCE AND INDEXES. CS121: Relational Databases Fall 2017 Lecture 11

DATABASE PERFORMANCE AND INDEXES. CS121: Relational Databases Fall 2017 Lecture 11 DATABASE PERFORMANCE AND INDEXES CS121: Relational Databases Fall 2017 Lecture 11 Database Performance 2 Many situations where query performance needs to be improved e.g. as data size grows, query performance

More information

Approaches. XML Storage. Storing arbitrary XML. Mapping XML to relational. Mapping the link structure. Mapping leaf values

Approaches. XML Storage. Storing arbitrary XML. Mapping XML to relational. Mapping the link structure. Mapping leaf values XML Storage CPS 296.1 Topics in Database Systems Approaches Text files Use DOM/XSLT to parse and access XML data Specialized DBMS Lore, Strudel, exist, etc. Still a long way to go Object-oriented DBMS

More information

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

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree. The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012

More information

Outline. Approximation: Theory and Algorithms. Ordered Labeled Trees in a Relational Database (II/II) Nikolaus Augsten. Unit 5 March 30, 2009

Outline. Approximation: Theory and Algorithms. Ordered Labeled Trees in a Relational Database (II/II) Nikolaus Augsten. Unit 5 March 30, 2009 Outline Approximation: Theory and Algorithms Ordered Labeled Trees in a Relational Database (II/II) Nikolaus Augsten 1 2 3 Experimental Comparison of the Encodings Free University of Bozen-Bolzano Faculty

More information

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

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge. Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

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

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

[ DATA STRUCTURES ] Fig. (1) : A Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree [ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several

More information

Part XII. Mapping XML to Databases. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 321

Part XII. Mapping XML to Databases. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 321 Part XII Mapping XML to Databases Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 321 Outline of this part 1 Mapping XML to Databases Introduction 2 Relational Tree Encoding Dead Ends

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

EE 368. Weeks 5 (Notes)

EE 368. Weeks 5 (Notes) EE 368 Weeks 5 (Notes) 1 Chapter 5: Trees Skip pages 273-281, Section 5.6 - If A is the root of a tree and B is the root of a subtree of that tree, then A is B s parent (or father or mother) and B is A

More information

TREES Lecture 10 CS2110 Spring2014

TREES Lecture 10 CS2110 Spring2014 TREES Lecture 10 CS2110 Spring2014 Readings and Homework 2 Textbook, Chapter 23, 24 Homework: A thought problem (draw pictures!) Suppose you use trees to represent student schedules. For each student there

More information

Database Management System 2

Database Management System 2 Data Database Management System 2 Data Data Data Basic Building Hierarchical Network Relational Semi-structured School of Computer Engineering, KIIT University 2.1 Data Data Data Data Basic Building Data

More information

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1 Basic Concepts :- 1. What is Data? Data is a collection of facts from which conclusion may be drawn. In computer science, data is anything in a form suitable for use with a computer. Data is often distinguished

More information

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height = M-ary Search Tree B-Trees Section 4.7 in Weiss Maximum branching factor of M Complete tree has height = # disk accesses for find: Runtime of find: 2 Solution: B-Trees specialized M-ary search trees Each

More information

CPS352 Lecture - Indexing

CPS352 Lecture - Indexing Objectives: CPS352 Lecture - Indexing Last revised 2/25/2019 1. To explain motivations and conflicting goals for indexing 2. To explain different types of indexes (ordered versus hashed; clustering versus

More information

Cpt S 122 Data Structures. Data Structures Trees

Cpt S 122 Data Structures. Data Structures Trees Cpt S 122 Data Structures Data Structures Trees Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Motivation Trees are one of the most important and extensively

More information

amiri advanced databases '05

amiri advanced databases '05 More on indexing: B+ trees 1 Outline Motivation: Search example Cost of searching with and without indices B+ trees Definition and structure B+ tree operations Inserting Deleting 2 Dense ordered index

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International

More information

CSE 214 Computer Science II Introduction to Tree

CSE 214 Computer Science II Introduction to Tree CSE 214 Computer Science II Introduction to Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Tree Tree is a non-linear

More information

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea

More information

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss) M-ary Search Tree B-Trees (4.7 in Weiss) Maximum branching factor of M Tree with N values has height = # disk accesses for find: Runtime of find: 1/21/2011 1 1/21/2011 2 Solution: B-Trees specialized M-ary

More information

Garbage Collection: recycling unused memory

Garbage Collection: recycling unused memory Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an

More information

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

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010 Uses for About Binary January 31, 2010 Uses for About Binary Uses for Uses for About Basic Idea Implementing Binary Example: Expression Binary Search Uses for Uses for About Binary Uses for Storage Binary

More information

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structure. IBPS SO (IT- Officer) Exam 2017 Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data

More information

Chapter 5. Binary Trees

Chapter 5. Binary Trees Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children

More information

DDS Dynamic Search Trees

DDS Dynamic Search Trees DDS Dynamic Search Trees 1 Data structures l A data structure models some abstract object. It implements a number of operations on this object, which usually can be classified into l creation and deletion

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. Truong Tuan Anh CSE-HCMUT Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes

More information

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

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

Evaluating XPath Queries

Evaluating XPath Queries Chapter 8 Evaluating XPath Queries Peter Wood (BBK) XML Data Management 201 / 353 Introduction When XML documents are small and can fit in memory, evaluating XPath expressions can be done efficiently But

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The

More information

In the previous presentation, Erik Sintorn presented methods for practically constructing a DAG structure from a voxel data set.

In the previous presentation, Erik Sintorn presented methods for practically constructing a DAG structure from a voxel data set. 1 In the previous presentation, Erik Sintorn presented methods for practically constructing a DAG structure from a voxel data set. This presentation presents how such a DAG structure can be accessed immediately

More information

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures. Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,

More information

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

CS350: Data Structures B-Trees

CS350: Data Structures B-Trees B-Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction All of the data structures that we ve looked at thus far have been memory-based

More information

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the

More information

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology Chapter 11 Trees Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called

More information

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech Trees Dr. Ronaldo Menezes Hugo Serrano (hbarbosafilh2011@my.fit.edu) Introduction to Trees Trees are very common in computer science They come in different variations They are used as data representation

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree is a finite set of one or more nodes

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Trees. Tree Structure Binary Tree Tree Traversals

Trees. Tree Structure Binary Tree Tree Traversals Trees Tree Structure Binary Tree Tree Traversals The Tree Structure Consists of nodes and edges that organize data in a hierarchical fashion. nodes store the data elements. edges connect the nodes. The

More information

Chapter 17 Indexing Structures for Files and Physical Database Design

Chapter 17 Indexing Structures for Files and Physical Database Design Chapter 17 Indexing Structures for Files and Physical Database Design We assume that a file already exists with some primary organization unordered, ordered or hash. The index provides alternate ways to

More information

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Lecture 32 No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 1 Outline Introduction

More information

Chapter 12: Indexing and Hashing. Basic Concepts

Chapter 12: Indexing and Hashing. Basic Concepts Chapter 12: Indexing and Hashing! Basic Concepts! Ordered Indices! B+-Tree Index Files! B-Tree Index Files! Static Hashing! Dynamic Hashing! Comparison of Ordered Indexing and Hashing! Index Definition

More information

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices. Trees Trees form the most widely used subclasses of graphs. In CS, we make extensive use of trees. Trees are useful in organizing and relating data in databases, file systems and other applications. Formal

More information

CHAPTER 3 LITERATURE REVIEW

CHAPTER 3 LITERATURE REVIEW 20 CHAPTER 3 LITERATURE REVIEW This chapter presents query processing with XML documents, indexing techniques and current algorithms for generating labels. Here, each labeling algorithm and its limitations

More information

XML Query Processing. Announcements (March 31) Overview. CPS 216 Advanced Database Systems. Course project milestone 2 due today

XML Query Processing. Announcements (March 31) Overview. CPS 216 Advanced Database Systems. Course project milestone 2 due today XML Query Processing CPS 216 Advanced Database Systems Announcements (March 31) 2 Course project milestone 2 due today Hardcopy in class or otherwise email please I will be out of town next week No class

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

Data and File Structures Laboratory

Data and File Structures Laboratory Binary Trees Assistant Professor Machine Intelligence Unit Indian Statistical Institute, Kolkata September, 2018 1 Basics 2 Implementation 3 Traversal Basics of a tree A tree is recursively defined as

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

Chapter 12: Indexing and Hashing

Chapter 12: Indexing and Hashing Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQL

More information

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department Abstract Data Structures IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Introduction To Computers

Introduction To Computers Introduction To Computers Chapter No 7 Introduction To Databases Overview Introduction to database To make use of information, you have to be able to find the information Data files and databases are no

More information

Unit III - Tree TREES

Unit III - Tree TREES TREES Unit III - Tree Consider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. A folder may

More information

XML Systems & Benchmarks

XML Systems & Benchmarks XML Systems & Benchmarks Christoph Staudt Peter Chiv Saarland University, Germany July 1st, 2003 Main Goals of our talk Part I Show up how databases and XML come together Make clear the problems that arise

More information

An Improvement of an Approach for Representation of Tree Structures in Relational Tables

An Improvement of an Approach for Representation of Tree Structures in Relational Tables An Improvement of an Approach for Representation of Tree Structures in Relational Tables Ivaylo Atanassov Abstract: The paper introduces an improvement of an approach for tree representation in relational

More information

Chapter 12: Query Processing

Chapter 12: Query Processing Chapter 12: Query Processing Overview Catalog Information for Cost Estimation $ Measures of Query Cost Selection Operation Sorting Join Operation Other Operations Evaluation of Expressions Transformation

More information

Chapter 12: Indexing and Hashing

Chapter 12: Indexing and Hashing Chapter 12: Indexing and Hashing Database System Concepts, 5th Ed. See www.db-book.com for conditions on re-use Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files B-Tree

More information

Outline. Depth-first Binary Tree Traversal. Gerênciade Dados daweb -DCC922 - XML Query Processing. Motivation 24/03/2014

Outline. Depth-first Binary Tree Traversal. Gerênciade Dados daweb -DCC922 - XML Query Processing. Motivation 24/03/2014 Outline Gerênciade Dados daweb -DCC922 - XML Query Processing ( Apresentação basedaem material do livro-texto [Abiteboul et al., 2012]) 2014 Motivation Deep-first Tree Traversal Naïve Page-based Storage

More information

Advances in Data Management Principles of Database Systems - 2 A.Poulovassilis

Advances in Data Management Principles of Database Systems - 2 A.Poulovassilis 1 Advances in Data Management Principles of Database Systems - 2 A.Poulovassilis 1 Storing data on disk The traditional storage hierarchy for DBMSs is: 1. main memory (primary storage) for data currently

More information

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004 Computer Science 136 Spring 2004 Professor Bruce Final Examination May 19, 2004 Question Points Score 1 10 2 8 3 15 4 12 5 12 6 8 7 10 TOTAL 65 Your name (Please print) I have neither given nor received

More information

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1. Data Structures inary Trees number of leaves:?? height leaves Depth (Maximum level of the tree) leaves or nodes Root Level = 0 Level=1 57 feet root 2 Level=2 Number of nodes: 2 (2+1) - 1 = 7 2 inary Trees

More information

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

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms

More information

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition

More information

Organizing Spatial Data

Organizing Spatial Data Organizing Spatial Data Spatial data records include a sense of location as an attribute. Typically location is represented by coordinate data (in 2D or 3D). 1 If we are to search spatial data using the

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses

More information

CS 350 : Data Structures B-Trees

CS 350 : Data Structures B-Trees CS 350 : Data Structures B-Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction All of the data structures that we ve

More information

Advanced Set Representation Methods

Advanced Set Representation Methods Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation

More information

MySQL 5.0 Stored Procedures, VIEWs and Triggers MySQL UC 2005, Santa Clara

MySQL 5.0 Stored Procedures, VIEWs and Triggers MySQL UC 2005, Santa Clara MySQL 5.0 Stored Procedures, VIEWs and Triggers jan@mysql.com MySQL UC 2005, Santa Clara Trees in SQL The problem of how to handle trees in SQL has been talked about alot. The basic 3 ways are: store the

More information

Searching in Graphs (cut points)

Searching in Graphs (cut points) 0 November, 0 Breath First Search (BFS) in Graphs In BFS algorithm we visit the verices level by level. The BFS algorithm creates a tree with root s. Once a node v is discovered by BFS algorithm we put

More information

B-Trees. Introduction. Definitions

B-Trees. Introduction. Definitions 1 of 10 B-Trees Introduction A B-tree is a specialized multiway tree designed especially for use on disk. In a B-tree each node may contain a large number of keys. The number of subtrees of each node,

More information

Chapter 11: Indexing and Hashing

Chapter 11: Indexing and Hashing Chapter 11: Indexing and Hashing Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 11: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files B-Tree

More information

Parser: SQL parse tree

Parser: SQL parse tree Jinze Liu Parser: SQL parse tree Good old lex & yacc Detect and reject syntax errors Validator: parse tree logical plan Detect and reject semantic errors Nonexistent tables/views/columns? Insufficient

More information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

Trees, Part 1: Unbalanced Trees

Trees, Part 1: Unbalanced Trees Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more

More information

Midterm 1: CS186, Spring I. Storage: Disk, Files, Buffers [11 points] cs186-

Midterm 1: CS186, Spring I. Storage: Disk, Files, Buffers [11 points] cs186- Midterm 1: CS186, Spring 2016 Name: Class Login: cs186- You should receive 1 double-sided answer sheet and an 11-page exam. Mark your name and login on both sides of the answer sheet, and in the blanks

More information

XML databases. Jan Chomicki. University at Buffalo. Jan Chomicki (University at Buffalo) XML databases 1 / 9

XML databases. Jan Chomicki. University at Buffalo. Jan Chomicki (University at Buffalo) XML databases 1 / 9 XML databases Jan Chomicki University at Buffalo Jan Chomicki (University at Buffalo) XML databases 1 / 9 Outline 1 XML data model 2 XPath 3 XQuery Jan Chomicki (University at Buffalo) XML databases 2

More information

Motivation and basic concepts Storage Principle Query Principle Index Principle Implementation and Results Conclusion

Motivation and basic concepts Storage Principle Query Principle Index Principle Implementation and Results Conclusion JSON Schema-less into RDBMS Most of the material was taken from the Internet and the paper JSON data management: sup- porting schema-less development in RDBMS, Liu, Z.H., B. Hammerschmidt, and D. McMahon,

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

(Refer Slide Time: 06:01)

(Refer Slide Time: 06:01) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 28 Applications of DFS Today we are going to be talking about

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

Binary search trees (BST) Binary search trees (BST)

Binary search trees (BST) Binary search trees (BST) Tree A tree is a structure that represents a parent-child relation on a set of object. An element of a tree is called a node or vertex. The root of a tree is the unique node that does not have a parent

More information

Trees and Binary Trees

Trees and Binary Trees Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or

More information

birds fly S NP N VP V Graphs and trees

birds fly S NP N VP V Graphs and trees birds fly S NP VP N birds V fly S NP NP N VP V VP S NP VP birds a fly b ab = string S A B a ab b S A B A a B b S NP VP birds a fly b ab = string Grammar 1: Grammar 2: A a A a A a B A B a B b A B A b Grammar

More information

Chapter 11: Indexing and Hashing

Chapter 11: Indexing and Hashing Chapter 11: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files B-Tree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQL

More information