SlideShare a Scribd company logo
Algorithms and Data Structures
Week 5
TREES – Non linear data structure
By – Priyanka Rana
Do you have a non linear mind?
Breakthroughs come by thinking "nonlinearly“.
Data Structure - Trees
 Non linear.
 Faster than linear data structures.
Applications
Trees
 Is an abstract data type.
 Defined as a set of nodes that stores elements
hierarchically.
 Consists of nodes with a
parent- child relation.
Parent-child relationship
Tree terminology
 Root
 If tree T is nonempty, it has a special node, called the
root of T, that has no parent.
 Internal Node
 Node with at least one child.
 External Node (leaf)
 Node without children.
Tree terminology
 Siblings
 Nodes that are children of the same parent.
 Ancestors of a Node
 Parent, grandparent, grand-grandparent.
 Descendants of a Node
 Child, grand child, grand-grandchild etc.
Tree terminology
 Edge
 An edge of tree T is a pair of nodes (u, v) such
that u is the parent of v, or vice versa.
 Path
 Path of T is a sequence of nodes such that any
two consecutive nodes in the sequence form an
edge.
A
DCB
E F
I J K
G H
ROOT - A
Internal Node- A, B, F,
C
External Node- E, I, J, K, G,
H, D
Sibling of B – C,D
Ancestors of F - A, B,
Descendant of B - E, I, J, K, F
Path – A, B, F, K
Tree terminology
 Ordered Trees
If there is a linear ordering defined for the children of each
node, visualized by arranging siblings left to right.
Book
Prefac
e
Part A Part B
Referenc
es
Ch. 5 Ch.
6
^ ^ ^ ^
Sec.
1.1 Sec. 1.9
Ch.
1
Sec. 10.1 Sec. 10.9
Ch.
10
Tree Abstract Data Type
 positions in a tree are its nodes.
 position object for a tree supports the method:
position <E> element()
Return the object stored at this position.
Tree’s accessors methods
 position root():
 Return the tree’s root.
 position parent(p):
 Return the parent of p.
 Iterable children(p):
 Return an iterable collection containing the children of node
p.
Tree’s query methods
 boolean isInternal(p):
 Test whether node P is internal.
 boolean isExternal(p):
 Test whether node p is external.
 boolean isRoot(p):
 Test whether node p is the root
Tree’s generic methods
 int size()
 Return the number of nodes in the tree.
 boolean isEmpty()
 Indicates whether the tree has any nodes.
 iterator iterator()
 Return an iterator of all elements stored at nodes of the tree.
 iterable position()
 Return an iterable collection of all the nodes of the tree.
 element replace(v, e)
 Replace with e and return the element stored at node v.
A
DCB
E F
I J K
G H
Size() - 11
isInternal(A) -
True
isExternal(E) - True
isRoot(C) - False isEmpty - False
Method Running Time
Size, isEmpty() O(1)
iterator, positions O(n)
Replace O(1)
Root, parent O(1)
isExternal O(1)
isInternal O(1)
isRoot O(1)
Tree traversal Algorithms
 A traversal of a Tree T is a systematic way of
accessing(or visiting) all the nodes of T.
 How can we index node?
 Position
 public Iterable <Position<E>> positions();
 How can we index children of node?
 Iterable
 public Iterator<E> iterator();
Tree ADT
 Depth of a node
 number of ancestors.
 Height of a tree
 maximum depth of any node.
 Subtree
 tree consisting of a node and its descendants.
Algorithm depth(T, v):
if v is the root of T then
return 0
else
return 1 + depth(T,w)
Algorithm height(T)
h0
for each vertex v in T do
if v is an external node in T then
h max (h, depth(T,v))
return h
A
DCB
E F
I J K
G H
Depth of F – 2
Height of a Tree -
3
Subtree – C, G, H
Traversal schemes
 preorder traversal
 Node is visited before its descendants.
 Children of each node of an ordered tree are
ordered from left to right.
 postorder traversal
 Node is visited after its descendants.
 Children of each node of an ordered tree are
ordered from left to right.
Preorder traversal
 Algorithm preorder(T, v):
visit(v);
for each child w of v do
preorder(T, w);
 Application
 print a structured document
 Running Time
 O(n)
Preorder example
Journal
Abstra
ct
#1 #2
Referenc
es
Sec.
1.1 Sec. 1.9 Sec. 2.1 Sec. 2.9
Title
Postorder traversal
 Algorithm postorder(T, v):
– for each child w of v do
– postorder(T, w);
– visit(v);
 Application
 computer space used by files in a directory and its subdirectories.
 Running Time
 O(n)
Postorder example
CP2410
Tutorial
Lectur
es
Week 10
Subject Outline
Tut 1 Tut 10 Week 1 Week 5
Exercise
What is the output of preorder and postorder
traversal of this tree?
What’s special about this tree?
Binary Trees
A binary tree is an ordered tree :
 Every node has at most two children.
 Each child node is labelled as being either a left child or
a right child.
 A left child precedes a right child in the
ordering of children of a node.
Binary trees Application
 A binary tree is proper or full
 If each node has either zero or two children.
 Application
 Representation of arithmetic expressions
 Internal nodes : operators
 External nodes : operands
Arithmetic expression tree
(2 x (a -1) + (3 x
b))
2
a 1
3 b
+
-
x x
Binary trees Application
 Representation of decision process
 Internal nodes : questions with yes/no answer
 External nodes : decisions
Binary Tree ADT - Accessor methods
 position left(p):
 Return the left child of p.
 position right(p):
 Return the right child of p.
 boolean hasLeft(p):
 Test whether p has a left child.
 boolean hasRight(p):
 Test whether p has a right child.
Binary Tree Implementation
 Linked Lists
Binary Tree Implementation
........ .... .
.
A B D G H
Array
If v is the root of T, then p(v) = 1.
If v is the left child of node u, then p(v) = 2p(u).
If v is the right child of node u, then p(v) = 2p(u)
Inorder traversal of a Binary Tree
Algorithm inorder(T, v):
if v has a left child u in T then
inorder(T, u) {recursively traverse left subtree}
perform the “visit action for node v
if v has a right child w in T then
inorder(T, w) {recursively traverse right subtree}
Inorder traversal of a Binary Tree
(3+1)
(3+1) * 4
((3+1)*4)/
((3+1)*4)/ (9-5)
((3+1)*4)/ ((9-5) + 2)
Answer = 16/6
Binary search
 x(v) – element stored in any internal node.
 For each internal node v of T:
 The elements stored in the left subtree of v are less than or
equal to x(v).
 The elements stored in the right subtree of v are greater
than or equal to x(v).
 The external nodes of T do not store any element.
Binary search tree
The blue solid path is traversed when searching (successfully) for 36.
The blue dashed path is traversed when searching (unsuccessfully)
for 70.

More Related Content

What's hot

Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
PREEYANKAV
 
Unit 2 linked list
Unit 2   linked listUnit 2   linked list
Unit 2 linked list
DrkhanchanaR
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
United International University
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Unit I-Data Structures_Intoduction.pptx
Unit I-Data Structures_Intoduction.pptxUnit I-Data Structures_Intoduction.pptx
Unit I-Data Structures_Intoduction.pptx
DrkhanchanaR
 
Red black tree
Red black treeRed black tree
Red black tree
Dr Sandeep Kumar Poonia
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Saurabh Mishra
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
ArunaP47
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5
DrkhanchanaR
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
Waheed Khalid
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Arboles
ArbolesArboles

What's hot (20)

Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
 
Unit 2 linked list
Unit 2   linked listUnit 2   linked list
Unit 2 linked list
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Unit I-Data Structures_Intoduction.pptx
Unit I-Data Structures_Intoduction.pptxUnit I-Data Structures_Intoduction.pptx
Unit I-Data Structures_Intoduction.pptx
 
Red black tree
Red black treeRed black tree
Red black tree
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Arboles
ArbolesArboles
Arboles
 

Similar to Trees - Non Linear Data Structure

Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
Edhole.com
 
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
lecture 13
lecture 13lecture 13
lecture 13sajinsc
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
V.V.Vanniapermal College for Women
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
shashankbhadouria4
 
Tree
TreeTree
Tree
bhumish
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
210 trees
210 trees210 trees
210 trees5
210 trees5210 trees5
210 trees5
Tanweer Page
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)
FariaFerdowsy
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
Tree
TreeTree
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
 
Data structures algoritghm for binary tree method.ppt
Data structures algoritghm for binary tree method.pptData structures algoritghm for binary tree method.ppt
Data structures algoritghm for binary tree method.ppt
Johny139575
 

Similar to Trees - Non Linear Data Structure (20)

Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Tree of Data Structure
Tree of Data StructureTree of Data Structure
Tree of Data Structure
 
Trees
TreesTrees
Trees
 
Review session2
Review session2Review session2
Review session2
 
lecture 13
lecture 13lecture 13
lecture 13
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Tree
TreeTree
Tree
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
210 trees
210 trees210 trees
210 trees
 
210 trees5
210 trees5210 trees5
210 trees5
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Tree
TreeTree
Tree
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
Data structures algoritghm for binary tree method.ppt
Data structures algoritghm for binary tree method.pptData structures algoritghm for binary tree method.ppt
Data structures algoritghm for binary tree method.ppt
 

More from Priyanka Rana

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority Queues
Priyanka Rana
 
Scrum values
Scrum valuesScrum values
Scrum values
Priyanka Rana
 
Usability testing
Usability testing  Usability testing
Usability testing
Priyanka Rana
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
Priyanka Rana
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective
Priyanka Rana
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototyping
Priyanka Rana
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboarding
Priyanka Rana
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototype
Priyanka Rana
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketing
Priyanka Rana
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of website
Priyanka Rana
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertising
Priyanka Rana
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposal
Priyanka Rana
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1
Priyanka Rana
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
Priyanka Rana
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
Priyanka Rana
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Priyanka Rana
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
Priyanka Rana
 
Linked list
Linked listLinked list
Linked list
Priyanka Rana
 

More from Priyanka Rana (19)

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority Queues
 
Scrum values
Scrum valuesScrum values
Scrum values
 
Usability testing
Usability testing  Usability testing
Usability testing
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototyping
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboarding
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototype
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketing
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of website
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertising
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposal
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Linked list
Linked listLinked list
Linked list
 

Recently uploaded

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 

Recently uploaded (20)

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 

Trees - Non Linear Data Structure

  • 1. Algorithms and Data Structures Week 5 TREES – Non linear data structure By – Priyanka Rana
  • 2. Do you have a non linear mind?
  • 3. Breakthroughs come by thinking "nonlinearly“.
  • 4. Data Structure - Trees  Non linear.  Faster than linear data structures.
  • 6.
  • 7.
  • 8. Trees  Is an abstract data type.  Defined as a set of nodes that stores elements hierarchically.  Consists of nodes with a parent- child relation.
  • 10. Tree terminology  Root  If tree T is nonempty, it has a special node, called the root of T, that has no parent.  Internal Node  Node with at least one child.  External Node (leaf)  Node without children.
  • 11. Tree terminology  Siblings  Nodes that are children of the same parent.  Ancestors of a Node  Parent, grandparent, grand-grandparent.  Descendants of a Node  Child, grand child, grand-grandchild etc.
  • 12. Tree terminology  Edge  An edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice versa.  Path  Path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge.
  • 13. A DCB E F I J K G H ROOT - A Internal Node- A, B, F, C External Node- E, I, J, K, G, H, D Sibling of B – C,D Ancestors of F - A, B, Descendant of B - E, I, J, K, F Path – A, B, F, K
  • 14. Tree terminology  Ordered Trees If there is a linear ordering defined for the children of each node, visualized by arranging siblings left to right. Book Prefac e Part A Part B Referenc es Ch. 5 Ch. 6 ^ ^ ^ ^ Sec. 1.1 Sec. 1.9 Ch. 1 Sec. 10.1 Sec. 10.9 Ch. 10
  • 15. Tree Abstract Data Type  positions in a tree are its nodes.  position object for a tree supports the method: position <E> element() Return the object stored at this position.
  • 16. Tree’s accessors methods  position root():  Return the tree’s root.  position parent(p):  Return the parent of p.  Iterable children(p):  Return an iterable collection containing the children of node p.
  • 17. Tree’s query methods  boolean isInternal(p):  Test whether node P is internal.  boolean isExternal(p):  Test whether node p is external.  boolean isRoot(p):  Test whether node p is the root
  • 18. Tree’s generic methods  int size()  Return the number of nodes in the tree.  boolean isEmpty()  Indicates whether the tree has any nodes.  iterator iterator()  Return an iterator of all elements stored at nodes of the tree.  iterable position()  Return an iterable collection of all the nodes of the tree.  element replace(v, e)  Replace with e and return the element stored at node v.
  • 19. A DCB E F I J K G H Size() - 11 isInternal(A) - True isExternal(E) - True isRoot(C) - False isEmpty - False
  • 20. Method Running Time Size, isEmpty() O(1) iterator, positions O(n) Replace O(1) Root, parent O(1) isExternal O(1) isInternal O(1) isRoot O(1)
  • 21. Tree traversal Algorithms  A traversal of a Tree T is a systematic way of accessing(or visiting) all the nodes of T.
  • 22.  How can we index node?  Position  public Iterable <Position<E>> positions();  How can we index children of node?  Iterable  public Iterator<E> iterator();
  • 23. Tree ADT  Depth of a node  number of ancestors.  Height of a tree  maximum depth of any node.  Subtree  tree consisting of a node and its descendants.
  • 24. Algorithm depth(T, v): if v is the root of T then return 0 else return 1 + depth(T,w)
  • 25. Algorithm height(T) h0 for each vertex v in T do if v is an external node in T then h max (h, depth(T,v)) return h
  • 26. A DCB E F I J K G H Depth of F – 2 Height of a Tree - 3 Subtree – C, G, H
  • 27. Traversal schemes  preorder traversal  Node is visited before its descendants.  Children of each node of an ordered tree are ordered from left to right.  postorder traversal  Node is visited after its descendants.  Children of each node of an ordered tree are ordered from left to right.
  • 28. Preorder traversal  Algorithm preorder(T, v): visit(v); for each child w of v do preorder(T, w);  Application  print a structured document  Running Time  O(n)
  • 30. Postorder traversal  Algorithm postorder(T, v): – for each child w of v do – postorder(T, w); – visit(v);  Application  computer space used by files in a directory and its subdirectories.  Running Time  O(n)
  • 32. Exercise What is the output of preorder and postorder traversal of this tree?
  • 34. Binary Trees A binary tree is an ordered tree :  Every node has at most two children.  Each child node is labelled as being either a left child or a right child.  A left child precedes a right child in the ordering of children of a node.
  • 35. Binary trees Application  A binary tree is proper or full  If each node has either zero or two children.  Application  Representation of arithmetic expressions  Internal nodes : operators  External nodes : operands
  • 36. Arithmetic expression tree (2 x (a -1) + (3 x b)) 2 a 1 3 b + - x x
  • 37. Binary trees Application  Representation of decision process  Internal nodes : questions with yes/no answer  External nodes : decisions
  • 38. Binary Tree ADT - Accessor methods  position left(p):  Return the left child of p.  position right(p):  Return the right child of p.  boolean hasLeft(p):  Test whether p has a left child.  boolean hasRight(p):  Test whether p has a right child.
  • 40. Binary Tree Implementation ........ .... . . A B D G H Array If v is the root of T, then p(v) = 1. If v is the left child of node u, then p(v) = 2p(u). If v is the right child of node u, then p(v) = 2p(u)
  • 41. Inorder traversal of a Binary Tree Algorithm inorder(T, v): if v has a left child u in T then inorder(T, u) {recursively traverse left subtree} perform the “visit action for node v if v has a right child w in T then inorder(T, w) {recursively traverse right subtree}
  • 42. Inorder traversal of a Binary Tree (3+1) (3+1) * 4 ((3+1)*4)/ ((3+1)*4)/ (9-5) ((3+1)*4)/ ((9-5) + 2) Answer = 16/6
  • 43. Binary search  x(v) – element stored in any internal node.  For each internal node v of T:  The elements stored in the left subtree of v are less than or equal to x(v).  The elements stored in the right subtree of v are greater than or equal to x(v).  The external nodes of T do not store any element.
  • 44. Binary search tree The blue solid path is traversed when searching (successfully) for 36. The blue dashed path is traversed when searching (unsuccessfully) for 70.

Editor's Notes

  1. &amp;lt;number&amp;gt;
  2. Do YOU have a non-linear mind? frequently turn 5 minute tasks into &amp;apos;all nighters&amp;apos;, can&amp;apos;t put something down until it&amp;apos;s finished, and there are many other symptoms as well, &amp;lt;number&amp;gt;
  3. according to productivity experts , breakthroughs come by thinking &amp;quot;nonlinearly&amp;quot; , &amp;lt;number&amp;gt;
  4. non-linear data structure in computing known as TREES.// Tree structures are a breakthrough in data organization, as they implement a host of algorithms much faster than when using linear data structures, such as list. &amp;lt;number&amp;gt;
  5. Trees provide a natural organization for data, and consequently have become ubiquitous structures in //file systems, graphical user interfaces, //databases, Web sites, and other computer systems. //Moves in a game are also put in a tree as well. &amp;lt;number&amp;gt;
  6. When we say that trees are &amp;quot;nonlinear,&amp;quot; we are referring to an organizational relationship that is more than &amp;quot;before&amp;quot; and &amp;quot;after&amp;quot; relationships, relationships in a tree are hierarchical, being &amp;quot;above&amp;quot; and &amp;quot;below&amp;quot;. //Actually, the main terminology for tree data structures comes from family trees, with the terms &amp;quot;parent,&amp;quot; &amp;quot;child,&amp;quot; &amp;quot;ancestor,&amp;quot; and &amp;quot;descendent&amp;quot; being the most common words used to describe relationships. &amp;lt;number&amp;gt;
  7. &amp;lt;number&amp;gt;
  8. &amp;lt;number&amp;gt;
  9. elaborate the parent child relationship in a tree with this example.// // // // this is how tree is extended from one level to another. A very good example of organisational hierarchy. &amp;lt;number&amp;gt;
  10. &amp;lt;number&amp;gt;
  11. Ordered trees: A tree is ordered if there is a linear ordering defined for the children of each node; that is, we can identify the children of a node as being the first, second, third, and so on. Such an ordering is usually visualized by arranging siblings left to right, according to their order. &amp;lt;number&amp;gt;
  12. In a tree abstract data type, positions in a tree are its nodes, and it supports a method called element() that returns the object stored at this position. &amp;lt;number&amp;gt;
  13. Is A internal , yes it is as it has children, is E external yes, as it has no children, is// // &amp;lt;number&amp;gt;
  14. Here is the running time for all the nodes, its all O(1) except iterator and position which has O(n) where running time does depend on data size. &amp;lt;number&amp;gt;
  15. &amp;lt;number&amp;gt;
  16. , in simple words, tree traversal is a systematic way of accessing all the nodes. // Do you think we can access nodes? if yes, how? position method, that returns an iterable collection of the nodes. if we have to index children of node, then iterator method can be used., iterator() method returns an iterator of the elements stored in the tree &amp;lt;number&amp;gt;
  17. &amp;lt;number&amp;gt;
  18. The depth of a node v can also be recursively defined as follows: • If v is the root, then the depth of v is 0 , • Otherwise, the depth of v is one plus the depth of the parent of v. this algorithm works recursively. height of a node v in a tree T is also defined recursively: • If v is an external node, then the height of v is 0 • Otherwise, the height of v is one plus the maximum height of a child of v. &amp;lt;number&amp;gt;
  19. Alorithm postorder: recursively traverse the subtree rooted at w., perform the visit action for node v. &amp;lt;number&amp;gt;
  20. Algoritm preorder: perform the &amp;quot;visit&amp;quot; action for node v, here it recursively traverse the subtree rooted at w. The preorder traversal algorithm is useful for producing a linear ordering of the nodes of a tree where parents must always come before their children in the ordering. Such orderings have several different applications. &amp;lt;number&amp;gt;
  21. &amp;lt;number&amp;gt;
  22. Alorithm postorder: recursively traverse the subtree rooted at w., perform the visit action for node v. &amp;lt;number&amp;gt;
  23. takes O(n) time, assuming that visiting each node takes O(1) time &amp;lt;number&amp;gt;
  24. If observe closely, each node has two children, and hence this type of tree is called as Binary tree. &amp;lt;number&amp;gt;
  25. // // natural way to realize a binary tree T is to use a linked structure, // A node is represented by an object storing element, parent node, left child nod, right child node. //And when we make a tree out of these nodes , it looks like something like this// &amp;lt;number&amp;gt;
  26. &amp;lt;number&amp;gt;
  27. inorder traversal: To traverse a binary tree in Inorder, we traverse the left most subtree starting at the left external node, (ii) Visit the root, and (iii) Traverse the right subtree starting at the left external node. &amp;lt;number&amp;gt;
  28. // /// // Lets see the example of the binary search tree storing integers. try to find 36 and 70 are in S, by traversing a path down the tree starting at the root. &amp;lt;number&amp;gt;
  29. Lets do it for 36 first, At each internal node we encountered, we compare our 36 with the element stored at node. If y = x(v), then the search continues in the left subtree of v. If y = x(v), then the search terminates successfully. If y ≥ x(v), then the search continues in the right subtree of v. Finally, if we reach an external node, the search terminates unsuccessfully. &amp;lt;number&amp;gt;