Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 TREES

2 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 manner Nodes higher in levels are called parent, while the ones in lower levels are called child nodes Each node can have at most one parent and multiple number of children A link between two nodes is called an edge Levels Parent Children Node Edges

3 Possible Uses Organization Charts File Systems Artificial Intelligence – Decision Trees – Evolutionary Algorithms (Genetic Programming) Class Hierarchy in Object Oriented Programming environment

4 Organizational Charts

5 Using trees in file system

6 Inheritance in OOP

7 Decision Trees in Tic-Tac-Toe

8 Decision Tree in an expert system

9 Genetic Programming Tree

10 Tree Terminology Root Node – The node without any parent – A tree can have only one root node A node referencing nodes lower in the hierarchy is called a parent node (labeled p in the figure) The node(s) referred by a parent node is(are) called child nodes (labeled c in figure) p c Root

11 Tree Terminology (Siblings) Child nodes of the same parent are called siblings (shown red in the figure)

12 Tree Terminology (Internal Nodes) A node have one or more children is termed as internal node (labeled in red in the figure)

13 Tree Terminology (Leaf/External Node) A node is an external or leaf node if it has no child nodes (labeled red in the figure)

14 Tree Terminology (Ancestor/Descendent) An ancestor of a node is either the node’s parent, grand parent, grand grand parent and so) A Descendent of a node is child, grand child, grand grand child and so on u v u v

15 Tree Terminology (Subtree) A tree may be divided into subtrees. A subtree is a tree that has the child of a node as its root. Hence, a subtree is composed of a node and all of that node’s descendants. The first node in a subtree is known as the root of the subtree and is used to name the subtree. Subtrees themselves can be further divided into other subtrees.

16 Tree Terminology (Subtree) The subtree of T rooted at node v is the tree consisting of all the descendents of v in T (including v) tree consisting of a node and its descendants v v Root of subtree

17 Tree Terminology (Depth of a node) – The depth of a node v in T is the number of ancestors of v, excluding v itself. – More formally: If v is the root, the depth of v is 0 depth of v = 1 depth of v = 3 v v

18 Tree Terminology( Height /depth of a tree) The depth of a tree is the maximum depth of any of its leaves maximum levels of a tree tree depth = 3 tree depth = 2 tree depth = 0

19 Terminology Two nodes are adjacent if a branch connects them. A path is a sequence of nodes in which each node is adjacent to the next one. Every node in the tree can be reached by following a unique path starting from the root. The length of this path is the number of edges on the path. There is a path of length 0 from every node to itself.

20 Terminology The path from the root, A, to the leaf, I, is denoted as AFI and has a length of 2. ABD is the path from the root, A, to the leaf, D, and also has a length of 2.

21 Types of Trees General tree – a node can have any number of children Binary tree – a node can have at most two children

22 Binary Tree

23 Binary Trees The simplest form of tree is a Binary Tree A Binary Tree consists of – (a) A node (called the root node) and – (b) Left and right subtrees – Both the subtrees are themselves binary trees Note: this is a recursive definition (A node can’t have more than 2 children) General tree Binary tree

24 Binary Trees Finite set of nodes that is either empty, or consists of a root and two disjoint binary trees called the left subtree and right subtree. – Node contains information and two pointers to other nodes – Each node has at most two children

25 A binary tree is either empty or has the following form: Where Tleft and Tright are binary trees. root TLTL TRTR

26 Binary Trees Full binary tree: All leaves on the same level and every node has either zero or two children. Complete binary tree: Leaves are filled from left to right on one level before moving to next level.

27 Binary Trees

28 Skewed binary tree: Contains only left or right children. Similar: Two trees with same structure and different data. Copy or identical: Same structure and same data.

29 Tree Height and Full Binary Tree If h = height of a binary tree, max # of leaves = 2 h max # of nodes = 2 h + 1 - 1 A binary tree with height h and 2 h + 1 - 1 nodes (or 2 h leaves) is called a full binary tree Binary tree

30 Visiting and Traversing a Node Many applications require that all of the nodes of a tree be “visited”. Visiting a node may mean printing contents, retrieving information, making a calculation, etc. Traverse: To visit all the nodes in a tree in a systematic fashion. – A traversal can pass through a node without visiting it at that moment.

31 Binary Tree Structure The representation of a binary tree structure is relatively straightforward. We need a variable to store the data at the node and 2 pointers to the left and right subtrees. struct Node { int data Node *left Node *right }

32 Binary Tree Structure

33 Traversing Binary Trees Traversing means visiting each node in a specified order Traversing is a slow process as compared to insertion, deletion There are many applications where traversing a tree is required

34 Possible ways of traversal Pre-order (Depth-First) – Visit the parent node, then the left subtree and lastly the right subtree In-order – Traverse the left subtree first then the parent node and then at last the right subtree Post-order – Visit the left subtree then the right subtree and finally the parent node Level-order (Breadth-First) – Subtrees are visited successively starting the root node and then nodes are visited from left to right on each level.

35 Graphical Representation

36 Preorder Traversal

37 Inorder Traversal

38 In an inorder traversal a node is visited after its left subtree and before its right Subtree Application: draw a binary tree or Arithmetic expression printing ((2 × (a − 1)) + (3 × b))

39 Example of Binary Tree (inorder)

40 Post Order Traversal

41 Expression Trees + a bc × ×+ × de f g + Expression tree for ( a + b × c) + ((d ×e + f) × g) There are three notations for a mathematical expression: 1) Infix notation : ( a + (b × c)) + (((d ×e) + f) × c) 2) Postfix notation: a b c × + d e × f + g * + 3) Prefix notation : + + a × b c × + × d e f g

42 Expression Tree traversals Depending on how we traverse the expression tree, we can produce one of these notations for the expression represented by the three. Inorder traversal produces infix notation. » This is a overly parenthesized notation. » Print out the operator, then print put the left subtree inside parentheses, and then print out the right subtree inside parentheses. Postorder traversal produces postfix notation. » Print out the left subtree, then print out the right subtree, and then printout the operator. Preorder traversal produces prefix notation. » Print out the operator, then print out the right subtree, and then print out the left subtree.

43 Summary Preorder (Depth-First) traversal sequence: F, B, A, D, C, E, G, I, Inorder traversal sequence: A, B, C, D, E, F, G, H,I Postorder traversal sequence: A, C, E, D, B, H, I, G, F Breadth-First traversal sequence: F, B, G, A, D, I, C, E, H


Download ppt "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."

Similar presentations


Ads by Google