advanced tree structures binary trees, b-trees, heaps, tries, suffix trees, space-partitioning trees...

47
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

Upload: aleesha-greer

Post on 28-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Advanced Tree StructuresBinary Trees, B-Trees, Heaps, Tries,

Suffi x Trees, Space-Parti ti oning Trees

AdvancedTreeStructures

SoftUni TeamTechnical TrainersSoftware Universityhttp://softuni.bg

Page 2: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Table of Contents

1. Balanced Binary Search Trees AA-Tree, AVL-Tree, Binary Tree, Rope

2. B-Trees B-Tree, B+ Tree

3. Heaps Binary Heap

4. Tries Trie, Suffix Tree

5. Space-Partitioning Trees BPS-Tree, K-d Tree 2

Page 3: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

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

Page 4: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

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

What is Binary Tree?

Page 5: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

5

Balanced Binary Search Tree – Example

33

18

15 24

3 17 20 29

54

42 60

37 43 85

The left subtree holds 7 nodes

The right subtree holds 6 nodes

The right subtree has height of 3

The left subtree has height of 3

Page 6: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Binary Tree ImplementationLive Demo

Page 7: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

7

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, …

Most Popular Binary Trees

Page 8: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

8

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

AVL Tree – Example

Page 9: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

AVL Tree ImplementationLive Demo

Page 10: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

10

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

Page 11: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Red-Black Tree ImplementationLive Demo

red black

Page 12: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

12

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 notneeded

Slower than AVL & RB

AA Tree

Page 13: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

AA Tree ImplementationLive Demo

Page 14: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

14

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

Rope

Page 15: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

15

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

Ropes in Practice: When to Use Rope?

Page 16: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Rope (Wintellect BigList<T>)Live Demo

Page 17: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

B-TreesB-Tree, B+ Tree

Page 18: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

18

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

What are B-Trees?

Page 19: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

19

B-Tree of order 2 (also known as 2-3-4-tree):

B-Tree – Example

17 21

7 11 18 20 26 31

4 5 6 8 9 12 16 22 23 25 27 29 30 32 35

Page 20: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

20

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)

B-Trees vs. Other Balanced Search Trees

Page 21: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Implementation of B-TreeLive Demo

Page 22: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

22

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

Page 24: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Priority Queue and HeapsHeap, Binary Heap

Page 25: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

25

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)

Priority Queue

Page 26: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

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

What is Heap?

Page 27: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

27

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

Page 28: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

28

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 – Array Implementation

Page 29: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

29

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 in Array: Tree Node Indexes

Page 30: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

30

Apply the "heap property" down from given node:

Binary Heap: Heapify-Down

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);

Page 31: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

31

Apply the "heap property" up from given node:

Insert a new node:

Binary Heap: Heapify-Up

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));

Page 32: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

32

Build a binary heap from array of elements:

Extract the max element from the heap:

Binary Heap: Build-Max-Heap and Insert

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;

Page 33: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Implementing Binary HeapLab Exercise

Page 35: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

TriesTrie and Suffi x Tree

Page 36: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

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

What is Trie?

Page 37: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

37

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

Suffix Tree

Page 38: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Trie – ImplementationLive Demo

Page 39: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

Space-Partitioning TreesBSP-Tree, K-d Tree, Interval Tree

Page 40: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

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

What is Space-Partitioning Tree?

Page 41: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

41

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

BSP-Tree

Page 42: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

42

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

K-d Tree

Page 43: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

43

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

Interval Tree

Page 44: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

44

1. Balanced binary search trees provide fastadd / search / remove operations – O(log(n))

AA-Tree, AVL-Tree, Binary Tree, Rope

2. B-Trees are ordered trees thathold multiple keys in a single node

3. Heaps provide fast add / find-min / remove-min operations

4. Tries and suffix trees provide fast string pattern matching

5. Space-partitioning trees partition the space into hyperplanes BPS-tree, K-d tree, interval tree

Summary

Page 46: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

License

This course (slides, examples, labs, videos, homework, etc.)is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license

46

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

Page 47: Advanced Tree Structures Binary Trees, B-Trees, Heaps, Tries, Suffix Trees, Space-Partitioning Trees SoftUni Team Technical Trainers Software University

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