SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees,

Slides:



Advertisements
Similar presentations
Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com.
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
 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
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
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Discrete Mathematics Chapter 5 Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design 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
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
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
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
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Graphs and Graph Algorithms
Data Structures Course Overview SoftUni Team Data Structures
Balanced Binary Search Trees
Basic Tree Data Structures
Balancing Binary Search Trees, Rotations
Fast String Manipulation
Multidimensional Arrays, Sets, Dictionaries
Tree.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Trees.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees, Balanced Trees, Implementations

2 1.Tree-like Data Structures 2.Trees and Related Terminology 3.Implementing Trees 4.Binary Trees and Traversals  Pre-order (node, left, right)  In-order (left, node, right)  Post-order (left, right, node) 5.Balanced Search Trees 6.Graphs Table of Contents

Tree-like Data Structures Trees, Balanced Trees, Graphs, Networks

4  Tree-like data structures are:  Branched recursive data structures  Consisting of nodes  Each node connected to other nodes  Examples of tree-like structures  Trees: binary, balanced, ordered, etc.  Graphs: directed / undirected, weighted, etc.  Networks: graphs with multiple relations between nodes Tree-like Data Structures

5 Project Manager Team Leader Designer QA Team Leader Developer 1 Developer 2 Tester 1 Developer 3 Tester 2 Tree Graph (20) 5 (10) 15 (15) 15 (30) 5 (5) 20(20) 10 (40) Network

Trees and Related Terminology Node, Edge, Root, Children, Parent, Leaf, Binary Search Tree, Balanced Tree Project Manager Team Leader Designer QA Team Leader

7  Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height, subtree Tree Data Structure – Terminology Height = 3 Depth 0 Depth 1 Depth

Implementing Trees Recursive Tree Data Structure

 The recursive definition for tree data structure:  A single node is tree  Tree nodes can have zero or multiple children that are also trees  Tree node definition in C# public class Tree public class Tree { private T value; private T value; private IList > children; private IList > children; …} 9 Recursive Tree Definition The value stored in the node List of child nodes, which are of the same type

10 Tree Structure – Example 7children 19children21children14children Tree 1children 12children 31children23children6children int value IList > children

public class Tree public class Tree { public T Value { get; set; } public T Value { get; set; } public IList > Children { get; private set; } public IList > Children { get; private set; } public Tree(T value, params Tree [] children) public Tree(T value, params Tree [] children) { this.Value = value; this.Value = value; this.Children = new List >(); this.Children = new List >(); foreach (var child in children) foreach (var child in children) { this.Children.Add(child); this.Children.Add(child); } }} 11 Implementing Tree Flexible constructor for building trees

12  Constructing a tree by nested constructors: Building a Tree Tree tree = new Tree (7, new Tree (7, new Tree (19, new Tree (19, new Tree (1), new Tree (1), new Tree (12), new Tree (12), new Tree (31)), new Tree (31)), new Tree (21), new Tree (21), new Tree (14, new Tree (14, new Tree (23), new Tree (23), new Tree (6)) new Tree (6)));

13  Printing a tree at the console – recursive algorithm Printing a Tree public class Tree public class Tree { … public void Print(int indent = 0) public void Print(int indent = 0) { Console.Write(new string(' ', 2 * indent)); Console.Write(new string(' ', 2 * indent)); Console.WriteLine(this.Value); Console.WriteLine(this.Value); foreach (var child in this.Children) foreach (var child in this.Children) child.Print(indent + 1); child.Print(indent + 1); }}

Lab Exercise Trees and Trees Traversal

Binary Trees and Traversals Preorder, In-Order, Post-Order

16  Binary trees: the most widespread form  Each node has at most 2 children (left and right) Binary Trees Root node Left subtree Right child Left child

Binary Trees Traversal  Binary trees can be traversed in 3 ways:  Pre-order: root, left, right  In-order: left, root, child  Post-order: left, right, root  Example:  Arithmetic expression stored as binary tree: (3 + 2) * (9 - 6) *  Pre-order: *  In-order: *  Post-order: *

Lab Exercise Binary Trees and Traversal

19  Binary search trees are ordered  For each node x in the tree  All the elements in the left subtree of x are ≤ x  All the elements in the right subtree of x are > x  Binary search trees can be balanced  Balanced trees have for each node  Nearly equal number of nodes in its subtrees  Balanced trees have height of ~ log(x) Binary Search Trees

20  Example of balanced binary search tree  If the tree is balanced, add / search / delete operations take approximately log(n) steps Binary Search Trees (2)

Balanced Search Trees AVL Trees, B-Trees, Red-Black Trees, AA-Trees

22  Ordered Binary Trees (Binary Search Trees)  For each node x the left subtree has values ≤ x and the right subtree has values > x  Balanced Trees  For each node its subtrees contain nearly equal number of nodes  nearly the same height  Balanced Binary Search Trees  Ordered binary search trees that have height of log 2 (n) where n is the number of their nodes  Searching costs about log 2 (n) comparisons Balanced Binary Search Trees

23  Balanced BST characteristics: 1. It has a maximum of two children per node (left and right) 2. It has a set of rules for determining when a branch is too deep or too wide after insertion or deletion 3. It has a procedure for rearranging the tree when the rules are violated (i.e. rebalancing) Balanced Binary Search Trees (2) NIL NIL NIL NILNIL NIL NIL NIL NIL NIL NIL

24 Balanced Binary Search Tree – Example The left subtree has 7 nodes The right subtree has 6 nodes

25  Balanced binary search trees are hard to implement  Rebalancing the tree after insert / delete is complex  Rotations change the structure without interfering the nodes' order  Well-known implementations of balanced binary search trees:  AVL Trees – ideally balanced, very complex  Red-black Trees – roughly balanced, more simple  AA-Trees – relatively simple to implement  Find / insert / delete operations need log(n) steps Balanced Binary Search Trees

26  AVL tree is a self-balancing binary-search tree AVL tree  See visualizationvisualization  Named after Adelson-Velskii and Landis  Height of two subtrees can differ by at most 1  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 AVL Tree – Example

27  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 – Example NIL NIL NIL NILNIL NIL NIL NIL NIL NIL NIL

28  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 AA Tree – Example

29  B-trees are generalization of the concept of ordered binary search trees – see the visualization B-treesvisualization  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 disk B-Trees

30  B-Tree of order 4 (max number of child nodes) B-Tree – Example

Balanced Search Trees AVL Trees, B-Trees, Red-Black Trees, AA-Trees

32 .NET Framework has several built-in implementations of balanced search trees:  SortedDictionary  Red-black tree based dictionary (map) of key-value pairs  SortedSet  Red-black tree based set of elements  External libraries like "Wintellect Power Collections for.NET" are more flexible  Balanced Trees in.NET

33  In.NET Framework SortedSet is based on red-black tree  Holds an ordered set of non-repeating elements  Insert / delete / find / subset has O(log(n)) running time SortedSet : Red-Black Tree in.NET var set = new SortedSet (); set.Add(5); // 5 set.Add(8); // 5, 8 set.Add(-2); // -2, 5, 8 set.Add(30); // -2, 5, 8, 30 set.Add(20); // -2, 5, 8, 20, 30 var subset = set.GetViewBetween(5, 20); subset.ToList().ForEach(Console.WriteLine); // 5, 8, 20

Graphs Set of Nodes Connected with Edges

35  Graph (denoted as G(V, E) )  Set of nodes V with many-to-many relationship between them (edges E )  Each node (vertex) has multiple predecessors and multiple successors  Edges connect two nodes (vertices) Graph Data Structure Node with multiple predecessors Node with multiple successors Self-relationship Node Edge

36  Node (vertex)  Element of graph  Can have name or value  Keeps a list of adjacent nodes  Edge  Connection between two nodes  Can be directed / undirected  Can be weighted / unweighted  Can have name / value Graph Definitions A Node A Edge B

37  Directed graph  Edges have direction Graph Definitions (2)  Undirected graph  Edges have no direction G J F D A E C H

38  Weighted graph  Weight (cost) is associated with each edge: Graph Definitions (3) G J F D A E C H Q K N

39  Typically graphs are stored as lists of descendant nodes  Instead of nodes, usually their index (number) is stored Graphs – Implementation public class Graph { List [] childNodes; List [] childNodes; public Graph(List [] nodes) public Graph(List [] nodes) { this.childNodes = nodes; this.childNodes = nodes; }} Graph g = new Graph(new List [] { new List {3, 6}, // successors of vertex 0 new List {3, 6}, // successors of vertex 0 new List {2, 3, 4, 5, 6},// successors of vertex 1 new List {2, 3, 4, 5, 6},// successors of vertex 1 new List {1, 4, 5}, // successors of vertex 2 new List {1, 4, 5}, // successors of vertex 2 new List {0, 1, 5}, // successors of vertex 3 new List {0, 1, 5}, // successors of vertex 3 new List {1, 2, 6}, // successors of vertex 4 new List {1, 2, 6}, // successors of vertex 4 new List {1, 2, 3}, // successors of vertex 5 new List {1, 2, 3}, // successors of vertex 5 new List {0, 1, 4} // successors of vertex 6 new List {0, 1, 4} // successors of vertex 6});

40  Trees are recursive data structures  A tree is a node holding a set of children (which are also nodes)  Binary search trees are ordered binary trees  Balanced trees have weight of log(n)  AVL trees, Red-Black trees and AA trees are self-balancing binary search trees, used to implement ordered sets, bags and dictionaries  Graph == set of nodes with many-to-many relationships  Can be directed / undirected, weighted / unweighted, connected / not connected, etc. Summary

? ? ? ? ? ? ? ? ? Trees and Tree-Like 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 42  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