43
TREES

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

Embed Size (px)

Citation preview

Page 1: 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

TREES

Page 2: 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

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

Page 3: 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

Possible Uses

• Organization Charts• File Systems• Artificial Intelligence

– Decision Trees– Evolutionary Algorithms (Genetic Programming)

• Class Hierarchy in Object Oriented Programming environment

Page 4: 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

Organizational Charts

Page 5: 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

Using trees in file system

Page 6: 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

Inheritance in OOP

Page 7: 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

Decision Trees in Tic-Tac-Toe

Page 8: 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

Decision Tree in an expert system

Page 9: 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

Genetic Programming Tree

Page 10: 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

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

Page 11: 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

Tree Terminology (Siblings)

• Child nodes of the same parent are called siblings (shown red in the figure)

Page 12: 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

Tree Terminology (Internal Nodes)

• A node have one or more children is termed as internal node (labeled in red in the figure)

Page 13: 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

Tree Terminology (Leaf/External Node)

• A node is an external or leaf node if it has no child nodes (labeled red in the figure)

Page 14: 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

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

Page 15: 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

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.

Page 16: 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

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

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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.

Page 20: 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

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.

Page 21: 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

Types of Trees

• General tree – a node can have any number of children

• Binary tree – a node can have at most two children

Page 22: 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

Binary Tree

Page 23: 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

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 treeBinary tree

Page 24: 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

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

Page 25: 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

• A binary tree is either empty or has the following form:

• Where Tleft and Tright are binary trees.

root

TLTR

Page 26: 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

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.

Page 27: 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

Binary Trees

Page 28: 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

Binary Trees• 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.

Page 29: 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

Tree Height and Full Binary Tree• If h = height of a binary tree,

max # of leaves = 2h max # of nodes = 2h + 1 - 1

• A binary tree with height h and 2h + 1 - 1 nodes (or 2h leaves) is called a full binary tree

Binary tree

Page 30: 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

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.

Page 31: 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

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 dataNode *leftNode *right

}

Page 32: 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

Binary Tree Structure

Page 33: 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

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

Page 34: 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

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.

Page 35: 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

Graphical Representation

Page 36: 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

Preorder Traversal

Page 37: 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

Inorder Traversal

Page 38: 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

Inorder Traversal

• 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))

Page 39: 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

Example of Binary Tree (inorder)

Page 40: 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

Post Order Traversal

Page 41: 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

Expression Trees

+

a

b c

×

× +

×

d e

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

Page 42: 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

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.

Page 43: 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

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