Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.

Similar presentations


Presentation on theme: "Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN."— Presentation transcript:

1 Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN

2 Lecture 82 Introduction Trees are a mathematical abstraction that play a central role in the design and analysis of algorithms We use trees to describe dynamic properties of algorithms We build and use explicit data structures that are concrete realisations of trees Definition of Trees Tree Rooted tree Ordered tree M-ary tree and binary tree

3 Lecture 83 Tree A tree is a non-empty collection of vertices and edges that satisfies certain requirements. A vertex (or node) is a simple object that can have a name and can carry other associated information An edge is a connection between two vertices A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree The defining property of a tree is that there is precisely one path connecting any two nodes. A disjoint set of trees is called a forest

4 Lecture 84 Rooted Tree A Rooted Tree is one where we designate one node as the root In computer science we normally reserve the term tree to refer to rooted trees. The more general structure is a free tree In a rooted tree, any node is the root of a subtree consisting of it and the nodes below it There is exactly one path between the root and each of the other nodes in the tree Each node except the root has exactly one node above it in the tree, called its parent, and we extend the family analogy talking of children, siblings, or grandparents Nodes with no children are leaves or terminal nodes

5 Lecture 85 Ordered Tree An ordered tree is a rooted tree in which the order of the children at every node is specified. If each node must have a specific number of children appearing in a specific order, then we have an M-ary tree The simplest type of M-ary tree is the binary tree A binary tree is an ordered tree consisting of nodes that can have at most two children As with any Abstract Data Structure we can implement a binary tree in a number of ways, using arrays, strings, or structures and pointers

6 Lecture 86 Examples of Trees  A free tree A rooted binary tree A forest

7 Lecture 87 Mathematical Properties Nodes on a tree are internal or external An internal node is a node in the tree An external node is a node that could be added to the existing nodes A binary tree with N internal nodes has N+1 external nodes A binary tree with N internal nodes has 2N links N-1 links to internal nodes and N+1 links to external nodes internal nodes external nodes

8 Lecture 88 Terminology The level of a node in a tree is one higher than the level of its parent (with the root at level 0) The height of a tree is the maximum of the levels of the tree’s nodes The height of a complete binary tree with N internal nodes is  lg N  The path length of a tree is the sum of the levels of all the trees nodes

9 Lecture 89 Tree Traversal Given a tree we want to process each node in the tree systematically For binary trees, we have two links, and we therefore have three basic orders in which we might visit the nodes: Preorder, where we visit the node, then visit the left and right subtrees Inorder, where we visit the left subtree, then visit the node, then visit the right subtree Postorder, where we visit the left and right subtrees, then visit the node These can be implemented easily using a recursive implementation

10 Lecture 810 Pre-Order Traversal Preorder(T) if (T <> nil) then visit(T) Preorder(left(T)) Preorder(right(T)) endif endalg Assuming that ‘visit’ means print and we have the following tree: 5 37 2 5 8 The output of the call Preorder(T) will be, 5 3 2 5 78

11 Lecture 811 Post and In order Changing to Postorder or Inorder simply involves changing the order of the calls Preorder visits the root before calling the function for either subtree Postorder visits the root after calling the function for the subtrees Inorder calls the function for the left subtree, then visits the root, and then calls the function for the right subtree

12 Lecture 812 Postorder and Inorder Algorithms Postorder(T) Inorder(T) if (T <> nil) then Postorder(left(T)) Inorder(left(T)) Postorder(right(T)) visit(T) visit(T) Inorder(right(T)) endif endalg

13 Lecture 813 Traversal Example Given the following tree above the traversals will give output as shown E DH BF A CG Preorder : E D B A C H F G Inorder : A B C D E F G H Postorder: A C B D G F H E

14 Lecture 814 Binary Search Tree A binary search tree is a binary tree in which every item in the left subtree is less than or equal to the item at the root, and every item in the right subtree is greater than the item in the root. 5 37 2 58 An inorder traversal of a binary search tree will print the nodes in sorted order The most common operation performed on a binary search tree is searching for a key.

15 Lecture 815 Binary Search Tree Algorithms Let x be a reference to the root of the tree, and k be the key we are looking for Tree-Search(x, k) if x = nil or k = key[x] then return x endif if k < key[x] then return Tree-Search( left[x], k) else return Tree-Search( right[x], k) endif endalg

16 Lecture 816 Binary Search Tree Algorithms Tree Minimum and Maximum are simple procedures on a binary search tree Tree-Minimum(x)Tree-Maximum(x) while left[x] <> nil while right[x] <> nil x = left[x] x = right[x] endwhile return x endalg

17 Lecture 817 Summary Trees are a mathematical abstraction that play a central role in the design and analysis of algorithms We build and use explicit data structures that are concrete realisations of trees Trees can be free, rooted, ordered or M-ary Trees have height, level and path length, all of which are defined Tree Traversal is an important operation For a binary tree we looked a preorder, postorder and inorder traversal Introduced a binary search tree and some simple algorithms. Will return to this.


Download ppt "Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN."

Similar presentations


Ads by Google