Advanced Tree Structures

Slides:



Advertisements
Similar presentations
Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer Wintellect Power Collections, C 5 Collections.
Advertisements

AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
 Dimitar Ivanov Introduction to programming with microcontrollers.
C# Advanced Topics Methods, Classes and Objects SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Design Patterns: Structural Design Patterns
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Starting at Binary Trees
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Very Basic Mathematical Concepts for Programmers
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Heaps & Priority Queues
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
Internal and External Sorting External Searching
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Advanced C# Course Introduction SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Data Structures Curriculum, Trainers, Evaluation, Exams SoftUni Team Technical Trainers Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Sets, Dictionaries SoftUni Team Technical Trainers Software University
SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees,
Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Data Structures Course Overview SoftUni Team Data Structures
COMP261 Lecture 23 B Trees.
AA Trees.
Heaps and Priority Queues
Balancing Binary Search Trees, Rotations
Fast String Manipulation
Combining Data Structures
Hashing Exercises.
Presentation transcript:

Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees Advanced Tree Structures SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents Balanced Binary Search Trees B-Trees Heaps Tries AA-Tree, AVL-Tree, Binary Tree, Rope B-Trees B-Tree, B+ Tree Heaps Binary Heap Tries Trie, Suffix Tree Space-Partitioning Trees BPS-Tree, K-d Tree © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Binary Tree, AA-Tree, AVL-Tree, Rope Balanced Binary Trees Binary Tree, AA-Tree, AVL-Tree, Rope

What is Binary Tree? Binary tree is a tree data structure Binary tree has a root node Each node has at most two children Left and right child Binary search trees are ordered trees Binary search trees can be balanced Subtrees hold nearly equal number of nodes Subtrees are with nearly the same height

Balanced Binary Search Tree – Example The left subtree holds 7 nodes 33 18 15 24 3 17 20 29 54 42 60 37 43 85 The right subtree holds 6 nodes The right subtree has height of 3 The left subtree has height of 3

Binary Tree Implementation Live Demo

Most Popular Binary Trees Binary tree – tree with at most 2 children Binary search tree – ordered binary tree Balanced binary search trees AA-tree – simple balanced search tree (fast add / find / delete) AVL-tree – self-balancing binary search tree Red-black tree – colored self-balancing binary search tree Rope – balanced binary tree that preserves the order of elements Provides fast access by index / add / edit / delete operations Others – splay tree, treap, top tree, weight-balanced tree, …

AVL Tree – Example AVL tree (Adelson-Velskii and Landis) Self-balancing binary-search tree (see the visualization)

AVL Tree Implementation Live Demo

Red-Black Tree Red-Black tree – binary search tree with red and black nodes Not perfectly balanced, but has height of O(log(n)) Used in C# and Java See the visualization AVL vs. Red-Black AVL has faster search (it is better balanced) Red-Black has faster insert / delete

Red-Black Tree Implementation Live Demo

AA Tree AA tree (Arne Andersson) Simple self-balancing binary-search tree Simplified Red-Black tree Easier to implement than AVL and Red-Black Some Red-Black rotations are not needed Slower than AVL & RB

AA Tree Implementation Live Demo

Rope Rope == balanced tree for indexed items with fast insert / delete Allows fast string edit operations on very long strings Rope is a binary tree having leaf nodes Each node holds a short string Each node has a weight value equal to length of its string

Ropes in Practice: When to Use Rope? Ropes are efficient for very large strings E.g. length > 10 000 000 For small strings ropes are slower! List<T> and StringBuilder performs better for 100 000 chars Ropes provide: Faster insert / delete operations at random position – O(log(n)) Slower access by index position – O(log(n)) Arrays provide O(1) access by index

Rope (Wintellect BigList<T>) Live Demo

B-Trees B-Tree, B+ Tree

What are B-Trees? B-trees are generalization of the concept of ordered binary search trees – see the visualization B-tree of order b has between b and 2*b keys in a node and between b+1 and 2*b+1 child nodes The keys in each node are ordered increasingly All keys in a child node have values between their left and right parent keys If the B-tree is balanced, its search / insert / add operations take about log(n) steps B-trees can be efficiently stored on the hard disk

B-Tree – Example B-Tree of order 2 (also known as 2-3-4-tree): 17 21 7 11 18 20 26 31 4 5 6 8 9 12 16 22 23 25 27 29 30 32 35

B-Trees vs. Other Balanced Search Trees B-Trees hold a range of child nodes, not single one B-trees do not need re-balancing so frequently Unlike other self-balancing search trees (like AVL, AA and Red-Black) B-trees may waste some space (memory) Since nodes are not entirely full B-Trees are good for database indexes Because a single node is stored in a single cluster of the hard drive Minimize the number of disk operations (which are very slow)

Implementation of B-Tree Live Demo

B+ Tree B+ tree is a special kind of B-tree Internal nodes hold keys + children + links Leaf nodes hold keys only + links Nodes at each level are linked in a doubly-linked list B+ tree is used for storing data for efficient retrieval in block- oriented storage context, e.g. in file systems and databases B+ tree has a lot of pointers to child nodes in a node Reduces the number of I/O operations to find an element Many file systems and RDBMS use B+ trees for efficiency

B+ Tree – Example

Priority Queue and Heaps Heap, Binary Heap

Priority Queue Priority queue in an abstract data type (ADT) that supports: Insert-with-Priority(element, priority) Pull-Highest-Priority-Element()  element Peek-Highest-Priority-Element()  element In C# and Java usually the priority is passed as comparator E.g. IComparable<T> in C# and Comparable<T> in Java Priority queue can be efficiently implemented as heap Any balanced search tree could work as well (e.g. AVL)

What is Heap? Heap is a special type of balanced binary tree stored in array Heap holds the "heap property": parent ≤ children Each child node should be greater or smaller than its parent Max Heap The parent nodes are always greater or equal to the child nodes Min Heap The parent nodes are always less than or equal to the child nodes

Binary Heap Binary heap is a heap data structure representing a binary tree Efficiently stored in a single array (no pointers at all) Binary heap have two constraints: Shape property: а binary heap is a complete binary tree Heap property: all nodes are either greater than or equal to or less than or equal to each of its children Binary heap efficiently implements a priority queue by binary tree stored as array

Binary Heap – Array Implementation Binary heap can be efficiently stored in an array Nodes 2*k and 2*k+1 have parent k Operations: Insert, Extract-Max, Build-Max-Heap Heapify-Up, Heapify-Down

Binary Heap in Array: Tree Node Indexes How to calculate the parent and children of given node i? parent(i) = (i - 1) / 2 leftChild(i) = 2 * i + 1 rightChild(i) = 2 * i + 2

Binary Heap: Heapify-Down Apply the "heap property" down from given node: void Heapify-Down(heapArr, i) left = leftChild(i); // 2*i + 1 right = rightChild(i); // 2*i + 2 largest = i; if left < length(heapArr) && heapArr[left] > heapArr[largest] largest = left; if right < length(heapArr) && heapArr[right] > heapArr[largest] largest = right; if largest ≠ i Swap(heapArr[i], heapArr[largest]); Heapify-Down(largest);

Binary Heap: Heapify-Up Apply the "heap property" up from given node: Insert a new node: void Heapify-Up(heapArr, i) while hasParent(i) // i > 0 && heapArr(i) > heapArr(parent(i)) // (i - 1) / 2 Swap(heapArr[i], heapArr[parent(i)]); i = parent; void Insert(heapArr, node) heapArr.Append(node); Heapify-Up(heapArr, lastElement(heapArr));

Binary Heap: Build-Max-Heap and Insert Build a binary heap from array of elements: Extract the max element from the heap: void Build-Max-Heap(heapArr) for i = length(heapArr) / 2 downto 1 Heapify-Down(heapArr, i) void Extract-Max(heapArr) max = heapArr[0]; heapArr[0] = delete last element from heapArr; if length(heapArr) > 0 Heapify-Down(0); return max;

Implementing Binary Heap Lab Exercise

Other Heap Data Structures Binomial heap Fibonacci heap Pairing heap Treap Skew heap Soft heap …

Tries Trie and Suffix Tree

What is Trie? Trie (radix tree or prefix tree) is an ordered tree data structure Special tree structure used for fast multi-pattern matching Used to store a dynamic set where the keys are usually strings Applications: Dictionaries Text searching Compression

Suffix Tree Suffix tree (position tree) is a compressed trie Represents the suffixes of given string as their keys and positions in the text as their values Used to implement fast search in string Applications String search Finding substrings Searching for patterns

Trie – Implementation Live Demo

Space-Partitioning Trees BSP-Tree, K-d Tree, Interval Tree

What is Space-Partitioning Tree? Tree data structures used for: Space partitioning – process of dividing a space into two or more subsets Binary space partitioning – method for recursively subdividing a space into convex sets by hyperplanes Applications: Computer graphics Ray tracing Collision detection

BSP-Tree BSP tree is a hierarchical subdivisions of n dimensional space into convex subspaces Each node has a front and back leaf Starting off with the root node, all subsequent insertions are partitioned by the hyperplane of the current node In 2D space, a hyperplane is a line In 3D space, a hyperplane is a plane Useful for real time interaction with displays of static images BSP trees can be traversed very quickly (linear time) for its purposes

K-d Tree K-d tree is a space-partitioning data structure for organizing points in a k-dimensional space Еvery node is a k-dimensional point Еvery non-leaf can be thought of as implicitly generating a splitting hyperplane  Hyperplane divides the space into two parts, known as half-spaces

Interval Tree Interval tree Balanced tree data structure to hold intervals Allows to efficiently find all intervals that Overlap with any given interval or point https://en.wikipedia.org/wiki/Interval_tree

Summary Balanced binary search trees provide fast add / search / remove operations – O(log(n)) AA-Tree, AVL-Tree, Binary Tree, Rope B-Trees are ordered trees that hold multiple keys in a single node Heaps provide fast add / find-min / remove-min operations Tries and suffix trees provide fast string pattern matching Space-partitioning trees partition the space into hyperplanes BPS-tree, K-d tree, interval tree © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Advanced Tree Structures https://softuni.bg/trainings/1147/Data-Structures-June-2015 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.