Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight.
Advertisements

AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
CS202 - Fundamental Structures of Computer Science II
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.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Balanced Trees Balanced trees have height O(lg n).
Binary Search Trees CSE 331 Section 2 James Daly.
 Dimitar Ivanov Introduction to programming with microcontrollers.
C# Advanced Topics Methods, Classes and Objects SoftUni Team Technical Trainers Software University
Advanced Tree Structures
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Teamwork and Personal Skills Course Introduction Software University SoftUni Team Technical Trainers.
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Methods, Arrays, Lists, Dictionaries, Strings, Classes and Objects
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.
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms 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.
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
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
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 with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
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,
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
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
ASP.NET MVC Course Program, Trainers, Evaluation, Exams, Resources SoftUni Team Technical Trainers Software University
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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
C# OOP Advanced Course Introduction SoftUni Team Technical Trainers Software University
Data Structures Course Overview SoftUni Team Data Structures
AA Trees.
Heaps and Priority Queues
Balancing Binary Search Trees, Rotations
Fast String Manipulation
Combining Data Structures
Red Black Trees
Presentation transcript:

Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University

Table of Contents 1.Balanced Binary Search Trees  AVL-Tree  Red-Black Tree  AA-Tree  Rope 2

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

 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?

5 Balanced Binary Search Tree – Example 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

Binary Tree Implementation Live Demo

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 (very rigidly balanced)  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, …splay tree treaptop treeweight-balanced tree Most Popular Binary Trees

8  AVL tree is a self-balancing binary-search tree (visualization) AVL treevisualization  Named after Soviet inventors Adelson-Velskii and Landis  Height of two subtrees can differ by at most 1 AVL Tree AverageWorst case SpaceO(n) SearchO(log n) InsertO(log n) DeleteO(log n)

9  Balance is preserved with a balance factor (BF) in each node  BF of any node is in the range [-1, 1]   If BF becomes -2 or 2  rebalance  Rebalancing is done by retracing  Start from inserted node's parent and go up to root  Perform rotations to restore balance AVL Tree Rebalancing BF == left subtree height – right subtree height

10  Left-Right -> Left-Left Case AVL Tree Rotations D A C B A B C D D A B C 1. Left-Right2. Left-Left3. Balanced

11  Right-Left -> Right-Right Case AVL Tree Rotations D A C B D A B C D A B C 1. Right-Left2. Right-Right3. Balanced

12 AVL Tree – Example  

13 1.Insert node N like in any ordinary BST 2.Begin retracing from N's parent P up to the root 1. Modify balance factors as you go up ∉ [-1,1]  rebalance 2. If balance factor ∉ [-1,1]  rebalance AVL Tree Insertion Algorithm

14  Insert 11 Insertion - #

15  go left  11 <  go left Insertion - #

16  go left  11 <  go left Insertion - #

17  go right  11 >  go right Insertion - #

18  go right  11 >  go right Insertion - #

19   Right node is null  insert  reduce  Right subtree grows  reduce 's BF  Did the subtree starting from increase its height?   Yes  begin retracing   No  over Insertion - #

20  Node –  Parent –  BF - 1 because right subtree increased height  Go up Retracing - #

21  Node –  Parent –  BF + 1 because left subtree increased height  Go up Retracing - #

22  Node –  Parent –  BF + 1 because left subtree increased height  rotate  BF = 2  rotate  Follow Left Left Case Retracing - #

23  Node –  Parent –  's right child becomes left child of  becomes right child of Left Left Rotation

24  Invalid Balance factor restored   Over  More: Left Left Rotation #

Lab Exercise AVL Tree Implementation (Insertion + Search)

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

27 1.A node is either red or black 2.The root is black 3.All leaves ( ) are black 4.If a node is red, then both its children are black 5.Every path from a given node to its descendant leaf nodes contains the same number of black nodes  E.g. All paths from have exactly 1 black node Red-Black Tree Properties NIL NIL NIL NILNIL NIL NIL NIL NIL NIL NIL 13 NIL

Red-Black Tree Implementation Live Demo

29  AA tree (Arne Andersson) AA tree  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

AA Tree Implementation Live Demo

31  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

32  Ropes are efficient for very large strings  E.g. length >  For small strings ropes are slower!  List and StringBuilder performs better for 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?

Rope (Wintellect BigList ) Live Demo

34  Balanced binary search trees provide fast add / search / remove operations – O(log n)  AVL trees are rigidly balanced BSTs  Fast search, slower add/remove  slower search time  Red-Black trees are less balanced  slower search time  Provide faster add/remove  Ropes are used for manipulation of very long strings  Provide fast insertion/deletion – O(log n) Summary

? ? ? ? ? ? ? ? ? Advanced Tree Structures

License  This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 36  Attribution: this work may contain portions from  "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with C#CC-BY-SA  "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA licenseData Structures and AlgorithmsCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg