trees. what is a tree ? an abstract data type which emulates a tree structure with a set of linked...
TRANSCRIPT
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 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
Possible Uses
• Organization Charts• File Systems• Artificial Intelligence
– Decision Trees– Evolutionary Algorithms (Genetic Programming)
• Class Hierarchy in Object Oriented Programming environment
Organizational Charts
Using trees in file system
Inheritance in OOP
Decision Trees in Tic-Tac-Toe
Decision Tree in an expert system
Genetic Programming Tree
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
Tree Terminology (Siblings)
• Child nodes of the same parent are called siblings (shown red in the figure)
Tree Terminology (Internal Nodes)
• A node have one or more children is termed as internal node (labeled in red in the figure)
Tree Terminology (Leaf/External Node)
• A node is an external or leaf node if it has no child nodes (labeled red in the figure)
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
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.
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
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
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
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.
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.
Types of Trees
• General tree – a node can have any number of children
• Binary tree – a node can have at most two children
Binary Tree
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
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
• A binary tree is either empty or has the following form:
• Where Tleft and Tright are binary trees.
root
TLTR
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.
Binary Trees
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.
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
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.
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
}
Binary Tree Structure
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
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.
Graphical Representation
Preorder Traversal
Inorder Traversal
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))
Example of Binary Tree (inorder)
Post Order Traversal
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
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.
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