Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com.

Slides:



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

Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Compiled by: Dr. Mohammad Omar Alhawarat
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees,
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
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.
Graphs and Graph Algorithms
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Basic Tree Data Structures
Data Structures and Design in Java © Rick Mercer
Fundamentals of Programming II Introduction to Trees
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
Trees.
Presentation transcript:

Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer

1. Tree-like Data Structures 2. Trees and Related Terminology 3. Implementing Trees 4. Traversing Trees  DFS and BFS Traversals 5. Balanced Search Trees  Balanced Trees in.NET 2

Trees, Balanced Trees, Graphs, Networks

 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 4

5 Project Manager Team Leader De- signer 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

Node, Edge, Root, Children, Parent, Leaf, Binary Search Tree, Balanced Tree

 Tree data structure – terminology  Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height, subtree Height = 3 Depth 0 Depth 1 Depth

 Binary trees: the most widespread form  Each node has at most 2 children Root node Left subtree Right child Left child 8

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

 Example of balanced binary search tree  If the tree is balanced, add / search / delete operations take approximately log(n) steps

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# 12 public class TreeNode public class TreeNode { private T value; private T value; private List > children; private List > children; …} The value contained in the node List of child nodes, which are of the same type

13 7children 19children21children14children TreeNode<int> 1children 12children 31children23children6children int value List > children

14 public TreeNode(T value) { this.value = value; this.value = value; this.children = new List >(); this.children = new List >();} public T Value { get { return this.value; } get { return this.value; } set { this.value = value; } set { this.value = value; }} public void AddChild(TreeNode child) { child.hasParent = true; child.hasParent = true; this.children.Add(child); this.children.Add(child);} public TreeNode GetChild(int index) { return this.children[index]; return this.children[index];}

 The class Tree keeps tree's root node 15 public class Tree public class Tree { private TreeNode root; private TreeNode root; public Tree(T value, params Tree [] children) : this(value) public Tree(T value, params Tree [] children) : this(value) { foreach (Tree child in children) foreach (Tree child in children) { this.root.AddChild(child.root); this.root.AddChild(child.root); } } public TreeNode Root public TreeNode Root { get { return this.root; } get { return this.root; } }} Flexible constructor for building trees

16 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)));  Constructing a tree by nested constructors:

DFS and BFS Traversals

 Traversing a tree means to visit each of its nodes exactly one in particular order  Many traversal algorithms are known  Depth-First Search (DFS)  Visit node's successors first  Usually implemented by recursion  Breadth-First Search (BFS)  Nearest nodes visited first  Implemented by a queue 18

 Depth-First Search first visits all descendants of given node recursively, finally visits the node itself  DFS algorithm pseudo code DFS(node){ for each child c of node for each child c of node DFS(c); DFS(c); print the current node; print the current node;}

 Stack: 7  Output: (empty)

 Stack: 7, 19  Output: (empty)

 Stack: 7, 19, 1  Output: (empty)

 Stack: 7, 19  Output:

 Stack: 7, 19, 12  Output:

 Stack: 7, 19  Output: 1,

 Stack: 7, 19, 31  Output: 1,

 Stack: 7, 19  Output: 1, 12,

 Stack: 7  Output: 1, 12, 31,

 Stack: 7, 21  Output: 1, 12, 31,

 Stack: 7  Output: 1, 12, 31, 19,

 Stack: 7, 14  Output: 1, 12, 31, 19,

 Stack: 7, 14, 23  Output: 1, 12, 31, 19,

 Stack: 7, 14  Output: 1, 12, 31, 19, 21,

 Stack: 7, 14, 6  Output: 1, 12, 31, 19, 21,

 Stack: 7, 14  Output: 1, 12, 31, 19, 21, 23,

 Stack: 7  Output: 1, 12, 31, 19, 21, 23, 6,

 Stack: (empty)  Output: 1, 12, 31, 19, 21, 23, 6, 14, Traversal finished

 Breadth-First Search first visits the neighbor nodes, later their neighbors, etc.  BFS algorithm pseudo code BFS(node){ queue  node queue  node while queue not empty while queue not empty v  queue v  queue print v print v for each child c of v for each child c of v queue  c queue  c}

 Queue: 7  Output:

 Queue: 7, 19  Output:

 Queue: 7, 19, 21  Output:

 Queue: 7, 19, 21, 14  Output:

 Queue: 7, 19, 21, 14  Output: 7,

 Queue: 7, 19, 21, 14, 1  Output: 7,

 Queue: 7, 19, 21, 14, 1, 12  Output: 7,

 Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7,

 Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19,

 Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19, 21,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23  Output: 7, 19, 21,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23,

 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23, The queue is empty  stop

 DFS traversal of binary trees can be done in pre- order, in-order and post-order  Pre-order: root, left, right  17, 9, 6, 12, 19, 25  In-order: left, root, right  6, 9, 12, 17, 19, 25  Post-order: left, right, root  6, 12, 9, 25, 19,

 What will happen if in the Breadth-First Search (BFS) algorithm a stack is used instead of queue?  An iterative Depth-First Search (DFS) – in-order 58 BFS(node){ queue  node queue  node while queue not empty while queue not empty v  queue v  queue print v print v for each child c of v for each child c of v queue  c queue  c}DFS(node){ stack  node stack  node while stack not empty while stack not empty v  stack v  stack print v print v for each child c of v for each child c of v stack  c stack  c}

Live Demo

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

 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 61

 Balanced binary search trees are hard to implement  Rebalancing the tree after insert / delete is complex  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 63

 B-trees are generalization of the concept of ordered binary search trees  B-tree of order d has between d and 2*d keys in a node and between d+1 and 2*d+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 64

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

.NET Framework has several built-in implementations of balanced search trees:  SortedDictionary  SortedDictionary  Red-black tree based map of key-value pairs  OrderedSet  OrderedSet  Red-black tree based set of elements  External libraries like "Wintellect Power Collections for.NET" are more flexible 

 Trees are recursive data structure – node with set of children which are also nodes  Binary Search Trees are ordered binary trees  Balanced trees have weight of log(n)  Graphs are sets of nodes with many-to-many relationship between them  Can be directed/undirected, weighted / unweighted, connected / not connected, etc.  Tree / graph traversals can be done by Depth-First Search (DFS) and Breadth-First Search (BFS) 67

Questions?

1. You are given a tree of N nodes represented as a set of N- 1 pairs of nodes (parent node, child node), each in the range ( 0..N- 1 ). Example: Write a program to read the tree and find: a)the root node b)all leaf nodes c)all middle nodes d)the longest path in the tree e)* all paths in the tree with given sum S of their nodes f)* all subtrees with given sum S of their nodes

2. Write a program to traverse the directory C:\WINDOWS and all its subdirectories recursively and to display all files matching the mask *.exe. Use the class System.IO.Directory. 3. Define classes File { string name, int size } and Folder { string name, File[] files, Folder[] childFolders } and using them build a tree keeping all files and folders on the hard drive starting from C:\WINDOWS. Implement a method that calculates the sum of the file sizes in given subtree of the tree and test it accordingly. Use recursive DFS traversal. 70

 C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com