lecture 10,11
TRANSCRIPT
-
8/6/2019 Lecture 10,11
1/46
FUIEMS Malik Imran Daud
Data Structures
Trees
Lecture 10,11
-
8/6/2019 Lecture 10,11
2/46
FUIEMS Malik Imran Daud
What is a tree?What is a tree?
In computer science, a tree is an abstract model of
a hierarchical structure
A tree consists ofnodes with aparent-child
relation
Previous data structures placed data in linear
order.
Some data organizations require categorizing datainto groups, subgroups
-
8/6/2019 Lecture 10,11
3/46
FUIEMS Malik Imran Daud
Tree TerminologyTree Terminology Trees are a hierarchical data structure ofTrees are a hierarchical data structure ofnodesnodes
Each position in the tree is called aEach position in the tree is called a nodenode
Nodes are linked byNodes are linked by edgesedges
4
2 5
1 3
edge
nodes
-
8/6/2019 Lecture 10,11
4/46
FUIEMS Malik Imran Daud
Applications
Organization charts
File systems
Programming environments A class hierarchy in programming languages that support singleA class hierarchy in programming languages that support single
inheritance (e.g. Java)inheritance (e.g. Java)
Document Object Model (for HTML and XMLDocument Object Model (for HTML and XML
Artificial Intelligence (Decision making etc)
-
8/6/2019 Lecture 10,11
5/46
FUIEMS Malik Imran Daud
tree is hierarchical classification
A tree is hierarchical classification.
Data items appear at various levels within the organization
E.g., directory structure:
-
8/6/2019 Lecture 10,11
6/46
FUIEMS Malik Imran Daud
tree is hierarchical classification
-
8/6/2019 Lecture 10,11
7/46
FUIEMS Malik Imran Daud
Decision TreesDecision Trees
-
8/6/2019 Lecture 10,11
8/46
FUIEMS Malik Imran Daud
Decision tree based upon expertDecision tree based upon expert
systemsystem
-
8/6/2019 Lecture 10,11
9/46
FUIEMS Malik Imran Daud
Tree Terminology (Parent /Tree Terminology (Parent /
Child)Child) Root: node without parent
AAparent nodeparent node references one or morereferences one or more
nodesnodes (childrennodes(childrennodes) that are lower) that are lower
in the tree hierarchyin the tree hierarchy
Ifnode u is the parent ofnode v, then vIfnode u is the parent ofnode v, then v
is the child of uis the child of u
Except for the root (no parent), everyExcept for the root (no parent), every
node has exactly one parent (bynode has exactly one parent (by
definition)definition)
A tree has only one root nodeA tree has only one root node
u
v
root
-
8/6/2019 Lecture 10,11
10/46
-
8/6/2019 Lecture 10,11
11/46
FUIEMS Malik Imran Daud
Tree Terminology (InternalTree Terminology (Internal
node)node) Anode is internal if it has one or more childrenAnode is internal if it has one or more children
-
8/6/2019 Lecture 10,11
12/46
FUIEMS Malik Imran Daud
Tree Terminology (Leaf /ExternalTree Terminology (Leaf /External
node)node)
Anode is a leaf if it has no childrenAnode is a leaf if it has no children
-
8/6/2019 Lecture 10,11
13/46
FUIEMS Malik Imran Daud
Tree Terminology (Ancestor /Tree Terminology (Ancestor /
Descendent)Descendent)
An ancestor of a node is either the nodes parent or anyAn ancestor of a node is either the nodes parent or anyancestor of the nodes parent (this is recursive)ancestor of the nodes parent (this is recursive)
The root is an ancestor of each othernodeThe root is an ancestor of each othernode
Descendant of a node: child, grandchild, grand-grandchild
u
v
u
v
-
8/6/2019 Lecture 10,11
14/46
FUIEMS Malik Imran Daud
Tree Terminology (Subtree)Tree Terminology (Subtree)
Anode plus the collection ofnodes beneath it is called aAnode plus the collection ofnodes beneath it is called asubtreesubtree
A tree may be divided intoA tree may be divided into subtreessubtrees..
Hence, a subtree is composed of aHence, a subtree is composed of a node and all of that nodesnode and all of that nodesdescendants.descendants.
Subtrees themselves can be further divided into other subtrees.Subtrees themselves can be further divided into other subtrees.
-
8/6/2019 Lecture 10,11
15/46
FUIEMS Malik Imran Daud
Tree TerminologyTree Terminology
(Subtree)(Subtree) The subtree of T rooted at node v is the tree consisting of all theThe subtree of T rooted at node v is the tree consisting of all thedescendents of v in T (including v)descendents of v in T (including v)
tree consisting of a node and its descendants
v
v
Root of subtree
-
8/6/2019 Lecture 10,11
16/46
FUIEMS Malik Imran Daud
Tree Terminology (Depth of aTree Terminology (Depth of a
node)node)
The depth of a node v in T is the number of ancestorsThe depth of a node v in T is the number of ancestors
of v, excluding v itself.of v, excluding v itself.
More formally:More formally:
If v is the root, the depth of v is 0If v is the root, the depth of v is 0
depth ofv = 1 depth ofv = 3
v
v
-
8/6/2019 Lecture 10,11
17/46
FUIEMS Malik Imran Daud
Tree Terminology( HeightTree Terminology( Height
/depth of a tree)/depth of a tree) The number of edges in the longest path from the root to a leafThe number of edges in the longest path from the root to a leafis theis the depthdepth (or(orheightheight) of the tree) of the tree
maximum levels of a tree
tree depth = 3
tree depth = 2
tree depth = 0
-
8/6/2019 Lecture 10,11
18/46
FUIEMS Malik Imran Daud
Types of TreesTypes of Trees
General treeGeneral tree a node cana node can
have any number of childrenhave any number of children
Binary treeBinary tree a node cana node can
have at most two childrenhave at most two children
-
8/6/2019 Lecture 10,11
19/46
FUIEMS Malik Imran Daud
Binary TreeBinary Tree
-
8/6/2019 Lecture 10,11
20/46
FUIEMS Malik Imran Daud
Binary TreesBinary Trees The simplest form ofThe simplest form oftreetree is a Binary Tree in which eachis a Binary Tree in which each
node has at most 2 child nodes.node has at most 2 child nodes.
ABinary Tree consists ofABinary Tree consists of
(a) A(a) Anodenode (called the(called the rootroot node) andnode) and
(b)(b) LeftLeft andand right subtreesright subtrees
Both the subtrees are themselves binary treesBoth the subtrees are themselves binary trees Note: this is a recursive definitionNote: this is a recursive definition
(Anode cant have more than 2 children)(Anode cant have more than 2 children)
General treeBinary tree
-
8/6/2019 Lecture 10,11
21/46
FUIEMS Malik Imran Daud
A binary tree is either empty or has the following
form:
Where Tleft and Tright are binary trees.
root
TLTR
-
8/6/2019 Lecture 10,11
22/46
FUIEMS Malik Imran Daud
Binary TreesBinary 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.
-
8/6/2019 Lecture 10,11
23/46
FUIEMS Malik Imran Daud
Binary TreesBinary Trees
-
8/6/2019 Lecture 10,11
24/46
-
8/6/2019 Lecture 10,11
25/46
FUIEMS Malik Imran Daud
Binary TreesBinary Trees
Full binary tree of height (depth)Full binary tree of height (depth) hh: all nodes at a: all nodes at a
height less thanheight less thanhh have exactly two childrenhave exactly two children
Balanced binary tree: for each node, theBalanced binary tree: for each node, the
difference in depth of the right and left subtrees isdifference in depth of the right and left subtrees isno more than oneno more than one
Completely balanced tree: left and right subtreesCompletely balanced tree: left and right subtrees
of every node have the same heightof every node have the same height
-
8/6/2019 Lecture 10,11
26/46
FUIEMS Malik Imran Daud
Binary Trees StorageBinary Trees Storage
Linked List based implementationLinked List based implementation
Array based implementationArray based implementation
The tree is a conceptual structureThe tree is a conceptual structure The data can be stored either in aThe data can be stored either in a dynamic linked treedynamic linked tree
structure, or in contiguous memory cells (array) according to astructure, or in contiguous memory cells (array) according to a
set patternset pattern; in other words, implementation can be pointer; in other words, implementation can be pointer--
based or arraybased or array--basedbased
-
8/6/2019 Lecture 10,11
27/46
FUIEMS Malik Imran Daud
Binary Tree: as a linkedBinary Tree: as a linked
structurestructure Each node in the tree consists of:Each node in the tree consists of:
The data, or value contained in the elementThe data, or value contained in the element
A left child pointer (pointer to first child)A left child pointer (pointer to first child)
A right child pointer (pointer to second child)A right child pointer (pointer to second child)
-
8/6/2019 Lecture 10,11
28/46
FUIEMS Malik Imran Daud
Binary Tree StructureBinary Tree Structure
The representation of a binary tree structure is relativelyThe representation of a binary tree structure is relativelystraight forward.straight forward.
We need a variable to store the data at the node and 2We need a variable to store the data at the node and 2
poin
ters to the left an
d right subtrees.poin
ters to the left an
d right subtrees.
struct Node {struct Node {int dataint dataNode *leftNode *leftN
ode *rightN
ode *right}}
-
8/6/2019 Lecture 10,11
29/46
FUIEMS Malik Imran Daud
Binary Tree StructureBinary Tree Structure
-
8/6/2019 Lecture 10,11
30/46
FUIEMS Malik Imran Daud
Binary Tree: contiguous storageBinary Tree: contiguous storage
Value in root node stored first, followed by left child, thenValue in root node stored first, followed by left child, thenright childright child
Each successive level in the tree stored left to right; unusedEach successive level in the tree stored left to right; unusednodes in tree represented by a bit pattern to indicate nothingnodes in tree represented by a bit pattern to indicate nothing
stored therestored there Children of any givennodeChildren of any givennode nn is stored in cellsis stored in cells 2n2n andand 2n + 12n + 1
(If array index starts at 1)(If array index starts at 1)
Can calculate position for any givennodeCan calculate position for any givennode
Storage allocated as for full tree, even if many nodes emptyStorage allocated as for full tree, even if many nodes empty
For a tree ofFor a tree ofdepthdepth hh we need array of 2we need array of 2hh+1+1
--1 cells1 cells
-
8/6/2019 Lecture 10,11
31/46
FUIEMS Malik Imran Daud
Common OperationsCommon Operations
Tree traversalTree traversal
Node additionNode addition
Node deletionNode deletion
DestroyDestroy
-
8/6/2019 Lecture 10,11
32/46
FUIEMS Malik Imran Daud
Binary Tree TraversalsBinary Tree Traversals AAbinary tree traversalbinary tree traversalrequires that each node of the tree berequires that each node of the tree be
processed once and only once in a predetermined sequence.processed once and only once in a predetermined sequence.
-
8/6/2019 Lecture 10,11
33/46
FUIEMS Malik Imran Daud
Traversal of Binary TreesTraversal of Binary Trees
Pass through all nodes of treePass through all nodes of tree
Inorder (symmetric traversal)Inorder (symmetric traversal)
Preorder (depth first traversal)Preorder (depth first traversal)
PostorderPostorder
-
8/6/2019 Lecture 10,11
34/46
FUIEMS Malik Imran Daud
Tree Traversal TypesTree Traversal Types
-
8/6/2019 Lecture 10,11
35/46
FUIEMS Malik Imran Daud
Preorder TraversalPreorder Traversal
-
8/6/2019 Lecture 10,11
36/46
FUIEMS Malik Imran Daud
Preorder TraversalPreorder Traversal
Root Left Right mannerRoot Left Right manner
++ LeftLeft RightRight
++ [*Left Right][*Left Right] [+Left Right][+Left Right]
++ (*AB)(*AB) [+ *Left Right E][+ *Left Right E] +*AB+*AB+ *C D E+ *C D E
+
* +
A B * E
C D
-
8/6/2019 Lecture 10,11
37/46
FUIEMS Malik Imran Daud
Inorder TraversalInorder Traversal
In the inorder traversal, the left subtree is processed first,In the inorder traversal, the left subtree is processed first,
followed by the root node, and then the right subtree.followed by the root node, and then the right subtree.
Inorder = root node in betweenthe left and right subtrees.
-
8/6/2019 Lecture 10,11
38/46
FUIEMS Malik Imran Daud
In order TraversalIn order Traversal
In an inorder traversal a node is visited after its left subtree and
before its right Subtree
Application: draw a binary tree orArithmetic expression
prin
tin
g
((2 (a 1)) + (3 b))
-
8/6/2019 Lecture 10,11
39/46
FUIEMS Malik Imran Daud
Inorder TraversalInorder Traversal
-
8/6/2019 Lecture 10,11
40/46
FUIEMS Malik Imran Daud
Example of Binary TreeExample of Binary Tree
(inorder)(inorder)
-
8/6/2019 Lecture 10,11
41/46
FUIEMS Malik Imran Daud
Post Order TraversalPost Order Traversal
In a postorder traversal, a node is visited after its descendants
Application: compute space used by files in a directory and its
subdirectories
-
8/6/2019 Lecture 10,11
42/46
FUIEMS Malik Imran Daud
Postorder TraversalPostorder Traversal
In the postorder traversal, the left subtree is processedIn the postorder traversal, the left subtree is processed
first, followed by the right subtree, and then the rootfirst, followed by the right subtree, and then the root
node.node.
Postorder = root node afterthe left and right subtrees.
-
8/6/2019 Lecture 10,11
43/46
FUIEMS Malik Imran Daud
Postorder TraversalPostorder Traversal
-
8/6/2019 Lecture 10,11
44/46
FUIEMS Malik Imran Daud
Postorder TraversalPostorder Traversal
Left Right Root mannerLeft Right Root manner
LeftLeft RightRight ++
[Left Right *][Left Right *] [Left Right+] +[Left Right+] +
(AB*)(AB*) [Left Right * E + ]+[Left Right * E + ]+
(AB*)(AB*) [C D * E + ]+[C D * E + ]+
AB*AB* C D * E + +C D * E + +
+
* +
A B * E
C D
-
8/6/2019 Lecture 10,11
45/46
FUIEMS Malik Imran Daud
Binary TreesBinary Trees
Finite set ofnodes 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 othernodes
Each node has at most two children
-
8/6/2019 Lecture 10,11
46/46
FUIEMS Malik Imran Daud
Visiting and Traversing a NodeVisiting 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.