Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com."— Presentation transcript:

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

2 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

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

5 5 Project Manager Team Leader De- signer QA Team Leader Developer 1 Developer 2 Tester 1 Developer 3 Tester 2 Tree Graph 7 19 21 14 1 12 31 4 11 2 3 6 1 4 5 5 (20) 5 (10) 15 (15) 15 (30) 5 (5) 20(20) 10 (40) Network

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

7  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 2 17 15 14 9 6 5 8 7

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

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

10  Example of balanced binary search tree  If the tree is balanced, add / search / delete operations take approximately log(n) steps 17 19 9 6 12 25 10

11 Recursive Tree Data Structure

12  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 13 7children 19children21children14children TreeNode<int> 1children 12children 31children23children6children int value List > children

14 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];}

15  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 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))); 7 14 19 23 6 21 31 1 12  Constructing a tree by nested constructors:

17 DFS and BFS Traversals

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

19  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;} 7 14 19 23 6 21 31 1 12 1 2 3 4 5 8 6 7 9 19

20  Stack: 7  Output: (empty) 20 7 14 19 23 6 21 31 1 12

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

38  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} 7 14 19 23 6 21 31 1 12 5 6 7 2 3 4 8 9 1 38

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

57  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, 17 17 199 6 12 25 57

58  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}

59 Live Demo

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

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

62 62 33 18 1524 3172029 54 4260 37435985

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

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

65  B-Tree of order 2 (also known as 2 - 3 - 4 -tree): 65 1721 71118202631 24568912162223252729303235

66 .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  http://powercollections.codeplex.com http://powercollections.codeplex.com 66

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

68 Questions? http://academy.telerik.com

69 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: 69 7 2 4 3 2 5 0 3 5 5 6 5 1 3 2 0 1 4 5 6 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

70 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

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


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

Similar presentations


Ads by Google