Advanced Tree Data Structures

Similar documents
DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Analysis of Algorithms

Self-Balancing Search Trees. Chapter 11

March 20/2003 Jayakanth Srinivasan,

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

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

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

Binary Trees, Binary Search Trees

Trees. Eric McCreath

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Data Structures and Algorithms for Engineers

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

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

TREES. Trees - Introduction

Algorithms. AVL Tree

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

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Binary Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Associate Professor Dr. Raed Ibraheem Hamed

CSI33 Data Structures

CISC 235: Topic 4. Balanced Binary Search Trees

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

CIS265/ Trees Red-Black Trees. Some of the following material is from:

CSCI2100B Data Structures Trees

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Balanced Search Trees. CS 3110 Fall 2010

AVL Trees Heaps And Complexity

Chapter 20: Binary Trees

CSC 421: Algorithm Design Analysis. Spring 2013

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

CSI33 Data Structures

Trees. Tree Structure Binary Tree Tree Traversals

Dynamic Access Binary Search Trees

CS350: Data Structures Tree Traversal

3137 Data Structures and Algorithms in C++

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

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

Cpt S 122 Data Structures. Data Structures Trees

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Balanced Binary Search Trees

Binary search trees (chapters )

Binary Search Trees. Analysis of Algorithms

CSI33 Data Structures

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Dynamic Access Binary Search Trees

Trees. CSE 373 Data Structures


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

CE 221 Data Structures and Algorithms

What is a Multi-way tree?

Binary Search Tree (2A) Young Won Lim 5/17/18

COSC 2011 Section N. Trees: Terminology and Basic Properties

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

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

CS Transform-and-Conquer

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Introduction to Computers and Programming. Concept Question

Augmenting Data Structures

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Programming II (CS300)

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

Search Trees. COMPSCI 355 Fall 2016

Friday Four Square! 4:15PM, Outside Gates

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

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Lecture 13: AVL Trees and Binary Heaps

IX. Binary Trees (Chapter 10)

Binary search trees (chapters )

CS350: Data Structures B-Trees

COMP171. AVL-Trees (Part 1)

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

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Trees. A tree is a directed graph with the property

COMP : Trees. COMP20012 Trees 219

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

CS 171: Introduction to Computer Science II. Binary Search Trees

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

CS350: Data Structures AVL Trees

CMPE 160: Introduction to Object Oriented Programming

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

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

Why Do We Need Trees?

COMP 250 Fall Solution - Homework #4

CS200: Balanced Search Trees

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Lecture 7. Binary Search Trees / AVL Trees

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

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

CS24 Week 8 Lecture 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

COMP 250 Fall Homework #4

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Table of Contents. Chapter 1: Introduction to Data Structures... 1

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Transcription:

Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park Binary trees Traversal order Balance Rotation Multi-way trees Search Insert Overview

Tree Traversal Goal Visit every node in binary tree Approaches Depth first Preorder parent before children Inorder left child, parent, right child Postorder children before parent Breadth first closer nodes first Tree Traversal Methods Pre-order. Visit node // first. Recursively visit left subtree. Recursively visit right subtree In-order. Recursively visit left subtree. Visit node // second. Recursively right subtree Post-order. Recursively visit left subtree. Recursively visit right subtree. Visit node // last

Tree Traversal Methods Breadth-first BFS(Node n) { Queue Q = new Queue(); Q.enqueue(n); // insert node into Q while (!Q.empty()) { n = Q.dequeue(); // remove next node if (!n.isempty()) { visit(n); // visit node Q.enqueue(n.Left()); // insert left subtree in Q Q.enqueue(n.Right());// insert right subtree in Q } } Tree Traversal Examples Pre-order (prefix) + / 8 4 In-order (infix) + 8 / 4 Post-order (postfix) 8 4 / + Breadth-first + / 8 4 + / 8 4 Expression tree

Tree Traversal Examples Pre-order 44, 7,, 78, 50, 48, 6, 88 In-order 7,, 44, 48, 50, 6, 78, 88 Post-order, 7, 48, 6, 50, 88, 78, 44 Breadth-first 44, 7, 78,, 50, 88, 48, 6 Sorted order! 44 7 78 50 48 6 Binary search tree 88 Tree Balance Degenerate Worst case Search in O(n) time Balanced Average case Search in O( log(n) ) time Degenerate binary tree Balanced binary tree 4

Question Tree Balance Can we keep tree (mostly) balanced? Self-balancing binary search trees AVL trees Red-black trees Approach Select invariant (that keeps tree balanced) Fix tree after each insertion / deletion Maintain invariant using rotations Provides operations with O( log(n) ) worst case Properties AVL Trees Binary search tree Heights of children for node differ by at most Example Heights of children shown in red 4 44 7 78 50 48 6 88 5

History AVL Trees Discovered in 96 by two Russian mathematicians, Adelson-Velskii & Landis Algorithm. Find / insert / delete as a binary search tree. After each insertion / deletion a) If height of children differ by more than b) Rotate children until subtrees are balanced c) Repeat check for parent (until root reached) Properties Red-black Trees Binary search tree Every node is red or black The root is black Every leaf is black All children of red nodes are black For each leaf, same # of black nodes on path to root Characteristics Properties ensures no leaf is twice as far from root as another leaf 6

Red-black Trees Example History Red-black Trees Discovered in 97 by Rudolf Bayer Algorithm Insert / delete may require complicated bookkeeping & rotations Java collections TreeMap, TreeSet use red-black trees 7

Tree Rotations Changes shape of tree Move nodes Change edges Types Single rotation Left Right Double rotation Left-right Right-left Tree Rotation Example Single right rotation 8

Tree Rotation Example Single right rotation 5 6 5 4 4 6 Node 4 attached to new parent Example Single Rotations single left rotation T 0 T T T 0 T T T T single right rotation T T 0 T 0 T T T T T 9

Example Double Rotations right-left double rotation T 0 T T T 0 T left-right double rotation T T T T T T 0 T T T 0 T T Properties Multi-way Search Trees Generalization of binary search tree Node contains k keys (in sorted order) Node contains k+ children Keys in j th child < j th key < keys in (j+) th child Examples 5 5 8 5 8 7 7 9 9 44 0

Types of Multi-way Search Trees - tree Internal nodes have or children Index search trie Internal nodes have up to 6 children (for strings) B-tree T = minimum degree Non-root internal nodes have T- to T- children All leaves have same depth 5 8 7 a c o T- T- s T Multi-way Search Trees Search algorithm. Compare key x to k keys in node. If x = some key then return node. Else if (x < key j) search child j 4. Else if (x > all keys) search child k+ Example Search(7) 5 5 0 40 8 7 7 6 44

Multi-way Search Trees Insert algorithm. Search key x to find node n. If ( n not full ) insert x in n. Else if ( n is full ) a) Split n into two nodes b) Move middle key from n to n s parent c) Insert x in n d) Recursively split n s parent(s) if necessary Multi-way Search Trees Insert Example (for - tree) Insert( 4 ) 5 5 8 7 4 8 7

Multi-way Search Trees Insert Example (for - tree) Insert( ) 5 5 4 8 7 4 8 7 Split node 5 Split parent 4 8 7 Characteristics B-Trees Height of tree is O( log T (n) ) Reduces number of nodes accessed Wasted space for non-full nodes Popular for large databases node = disk block Reduces number of disk blocks read