SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Trees and Graphs
Spencer Moran
12/3/13
Properties of a Tree
• Tree is a graph where:
– n-1 edges
– Connected
– No cycles

• If two of the above are true, then all 3 are
true.
• Tree questions are usually asked to test your
understanding of recursion.
Binary Search Trees
• Binary tree is one where each node as at most
2 children.
• Binary Search tree is a binary tree where all
the descendants to the left of a node are
smaller than it and all the descendants to the
right are greater than it.
• Most interview questions focus on binary or
binary search trees.
Binary Search Tree Complexity
•
•
•
•
•
•

Lookup: O(log n)
Insertion: O(log n)
Deletion: O(log n)
Smallest/Largest element: O(n)
Traversal: O(n)
Print in sorted order: O(n)
Heaps
• (Usually) binary tree where each node’s
children are smaller (or larger in a Min-heap)
than it.
• Allows for constant time extraction of the
largest element in the tree.
• Lookup becomes O(n)
• Used if extracting the smallest or largest node
is paramount
Searching
BFS

• Queue
• Searches minimum
depths first, always
searches lowest depth
last
• High memory
requirement
• Shortest path

DFS

• Stack
• Explores full depth,
doesn’t search any
particular depth last
• Low memory
requirement
Traversals
• Pre-order:
– Explore node, then left children, then right

• In-order:
– Explore left children, then node, then right

• Post-order:
– Explore left children, then right, then node

• Implemented using simple recursion
Preorder Traversal
Sample Question

Find the height of a binary tree.
Strategy
• Use recursion
• Think of each child as its own separate subtree
with that node as the root
• Return 1 + the max of left subtree’s height and
right subtree’s height
Sample Question

Given the value of two nodes in a BST, find the
lowest common ancestor. You may assume both
values exist in the tree.
Strategy
• Uses the left/right descendant property of a
BST to traverse through the nodes.
• Starting at the root, traverse through the tree.
• If both values are greater than the current
node, go right.
• If both are smaller than the current, go left.
• LCA will be the first node you encounter that
is between the two nodes you are given.
Other Common Tree Questions
• Binary tree to heap
• Is Binary tree a BST?
• Balance an unbalanced BST (left or right
rotation)
• Count # of leaves
Graphs
• Graph questions are much more complicated
and less common.
• We will cover graphs during the group
meeting.

Weitere ähnliche Inhalte

Was ist angesagt?

Tree data structure
Tree data structureTree data structure
Tree data structure
Dana dia
 

Was ist angesagt? (15)

Trees data structure
Trees data structureTrees data structure
Trees data structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Trees krw
Trees krwTrees krw
Trees krw
 
Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
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
 
Trees
TreesTrees
Trees
 
Trees
TreesTrees
Trees
 
My Math Project for Mr. Medina's Class
My Math Project for Mr. Medina's ClassMy Math Project for Mr. Medina's Class
My Math Project for Mr. Medina's Class
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 

Ähnlich wie Treesandgraphs

4a searching-more
4a searching-more4a searching-more
4a searching-more
Shahzad Ali
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
KamranAli649587
 

Ähnlich wie Treesandgraphs (20)

Tree
TreeTree
Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
TREES34.pptx
TREES34.pptxTREES34.pptx
TREES34.pptx
 
Binary trees
Binary treesBinary trees
Binary trees
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Treesandgraphs

  • 1. Trees and Graphs Spencer Moran 12/3/13
  • 2. Properties of a Tree • Tree is a graph where: – n-1 edges – Connected – No cycles • If two of the above are true, then all 3 are true. • Tree questions are usually asked to test your understanding of recursion.
  • 3. Binary Search Trees • Binary tree is one where each node as at most 2 children. • Binary Search tree is a binary tree where all the descendants to the left of a node are smaller than it and all the descendants to the right are greater than it. • Most interview questions focus on binary or binary search trees.
  • 4. Binary Search Tree Complexity • • • • • • Lookup: O(log n) Insertion: O(log n) Deletion: O(log n) Smallest/Largest element: O(n) Traversal: O(n) Print in sorted order: O(n)
  • 5. Heaps • (Usually) binary tree where each node’s children are smaller (or larger in a Min-heap) than it. • Allows for constant time extraction of the largest element in the tree. • Lookup becomes O(n) • Used if extracting the smallest or largest node is paramount
  • 6. Searching BFS • Queue • Searches minimum depths first, always searches lowest depth last • High memory requirement • Shortest path DFS • Stack • Explores full depth, doesn’t search any particular depth last • Low memory requirement
  • 7. Traversals • Pre-order: – Explore node, then left children, then right • In-order: – Explore left children, then node, then right • Post-order: – Explore left children, then right, then node • Implemented using simple recursion
  • 9. Sample Question Find the height of a binary tree.
  • 10. Strategy • Use recursion • Think of each child as its own separate subtree with that node as the root • Return 1 + the max of left subtree’s height and right subtree’s height
  • 11.
  • 12. Sample Question Given the value of two nodes in a BST, find the lowest common ancestor. You may assume both values exist in the tree.
  • 13. Strategy • Uses the left/right descendant property of a BST to traverse through the nodes. • Starting at the root, traverse through the tree. • If both values are greater than the current node, go right. • If both are smaller than the current, go left. • LCA will be the first node you encounter that is between the two nodes you are given.
  • 14.
  • 15. Other Common Tree Questions • Binary tree to heap • Is Binary tree a BST? • Balance an unbalanced BST (left or right rotation) • Count # of leaves
  • 16. Graphs • Graph questions are much more complicated and less common. • We will cover graphs during the group meeting.