Introduction to Trees (Data Structures & Algorithms #9)

CS Dojo · Beginner ·⚡ Algorithms & Data Structures ·5y ago

Key Takeaways

This video teaches the tree data structure and its applications

Full Transcript

hey everyone in my last few videos i made a q a website for quarters and i still work on it from time to time but in this video i decided to switch topics and restart my data structures and algorithms series now in a previous video which i published a long time ago we learned the linked list data structure it looked like this one where we had a bunch of nodes that are connected to each other in a single direction and each node in the structure had a class like this it's called node and it has two attributes uh integer data and next which is also a node now a tree is a similar data structure to a linked list and the only difference is that in a linked list each node can only link to one other node but in a tree each node can link to multiple other nodes so here's one example of a tree as you can see each node here is linking to multiple nodes here each node is linking to at least three other nodes and so for this particular structure the class of each node might look like this one as you can see this one is called node again and it has four attributes uh integer data just like before sometimes this one is called value but we're calling data here and we have three children uh all of them are nose and so for example if you look at this particular node here the three children are set to these three other nodes and if you look at this node right here this one has only two children this one and this one so if you want to express that in code you can just set the two of the children to those nodes and the last child to null or none to show that it doesn't exist and if you look at this node it doesn't have any children so you can set all of these children knows to null or none depending on the language that you're using now let's take a look at another example of a tree so this one is drawn from top to bottom instead of left to right but it has the same kind of structure the more important difference is that each node has almost two children here so the class of each node might look like this one instead as you can see we have integer data just like before but now we have only two children which we're calling left and right and just like this when a tree has at most true truth it's called a binary tree now to help you understand what a tree is exactly we're going to call this little game called is this a tree basically i'm going to show you a structure and you just need to answer if it's a tree or not so let me start with this one this one uh doesn't have integers inside them instead it has strings but of course it's still a tree and what about this one well it's a linked list but it's also a tree and the way i think about it is that each node could have multiple children just like that but it just doesn't so it's kind of a boring example but technically speaking it's also a tree and what about this one well it might look like a tree but it's not because one constraint of something being a tree is that there are no two references that link to the same node and these two references violate that condition and what about this last one well again it violates the definition of being something being a tree for the same reason these two references point to the same node so it's not a tree and another way to see that this is not a tree is that it has a cycle here and whenever there's a cycle that's not a tree so you might say well what is a tree exactly then well a tree is a structure in which there are nodes that are connected to each other and there's a way to go from the root node to every other node in the structure so the root node in this particular tree is this one and there's a way for us to get to every other node from the root node in this structure and it's the same thing with this tree or this linked list there's this root node and there's a way to get to every other node from the root node but as soon as there are two references in this structure that refer to the same node for example this one then it's not a tree anymore so that's basically what a tree is and by the way the root node of a tree is a node without any parents so what that means is that whatever the root node is no other node refers to that one okay now that you hopefully have a clear idea of what a tree is let's practice using a tree with this problem you're given a tree for example this one with the root being here and this is a binary tree so the class of each node will look like this it has an integer data and it has two children left and right and the problem is writing a function which we're going to call find sum which takes the root of this tree as the input and returns the sum of all the values within the street so if you're given this particular root you want to be able to return 20 from this function because we have 2 plus 3 plus 5 plus 6 plus 4 which is 20. and try solving this problem in of and in time where n is the number of nodes in this tree okay and here's my solution and by the way if you want to try running my solution in either python or java you can find that at this url csojo.io tree like i said before we're calling this function find sum and it's taking the root of whatever tree that you're given and we're going to implement this function recursively here and the first thing we're going to do is we're going to define the base case and that's going to be when the given root is null or none which is that the given root is just an empty tree for example this empty tree right here then the sum of all the values in this empty tree is of course zero so we want to return zero in that case and otherwise uh for example if we're given this blue height right here in this recursive function then the sum of all the nodes in this tree is the sum of this current value plus the sum of all the nodes in the right subtree and the sum of all the nodes in the left subtree we can express that with this one line here we're returning the current value or root.data plus the sum of uh all the nodes in the left subtree so let's find sum of red dot left and the sum of all the nodes in the right subtree so let's find some of that right this function would only take of n in time to execute where n is the number of nodes in the given tree and let's think about why well first we need to count the number of times this function is going to be called and that's going to be the number of nodes in the given tree because for each node this function is going to be called once plus all the empty nodes or these empty trees that i didn't draw earlier in this representation of the tree and so this function is going to be called at most about 2n times or of 2n times which is the same thing as of n times and so this function is going to be called of n times and each time this function is called let's think about how how much time it takes to execute well if you look at each line uh if you look at this line it only takes a constant amount of time or of one because we're simply checking uh this simple if condition and it's the same thing as this line it only takes off one in time returning zero and this line as well we're simply adding up these three numbers so once we have the results for these two recursive calls adding up these three numbers would only take off one in time or a constant amount of time and so each time this function is called it only takes one and it's called o of n times so multiplying them together we get the total amount of time this function takes to execute or the time complexity of this function and that's going to be of n now that's it for this problem and my introduction to trees but if you need more practice using trees and there's another interesting problem that i think is much harder and it's a problem i talked about a while ago as a coding interview question so i'm gonna put a link to that video in the description below anyway thank you as always for watching my videos and i'll see you guys in the next one

Original Description

Here is my intro to the tree data structure! And here's another interesting tree problem: https://youtu.be/7HgsS8bRvjo You can download my sample code in Python and Java here: https://www.csdojo.io/tree
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from CS Dojo · CS Dojo · 0 of 60

← Previous Next →
1 4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
2 Dynamic Programming Tutorial with Fibonacci Sequence
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
3 Kadane's Algorithm to Maximum Sum Subarray Problem
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
4 Longest Common Subsequence (Dynamic Programming)
Longest Common Subsequence (Dynamic Programming)
CS Dojo
5 0-1 Knapsack Problem (Dynamic Programming)
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
6 Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
7 Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
8 Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
9 Radix Sort Algorithm Introduction in 5 Minutes
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
10 Coding Interview Question and Answer: Longest Consecutive Characters
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
11 Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
12 Coding Interview Question: Tower Hopper Problem
Coding Interview Question: Tower Hopper Problem
CS Dojo
13 Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
14 Google Coding Interview Question and Answer #1: First Recurring Character
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
15 Facebook Coding Interview Question and Answer #1: All Subsets of a Set
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
16 Think you're not smart enough to work at Google? Well, think again.
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
17 How to Crack a Google Coding Interview - An Ex-Googler’s Guide
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
18 Amazon Coding Interview Question - K Closest Points to the Origin
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
19 How I Got an Internship at Microsoft
How I Got an Internship at Microsoft
CS Dojo
20 How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
21 Why I Left My $100,000+ Job at Google
Why I Left My $100,000+ Job at Google
CS Dojo
22 Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
23 How I Learned to Code - and Got a Job at Google!
How I Learned to Code - and Got a Job at Google!
CS Dojo
24 Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
25 What Is Dynamic Programming and How To Use It
What Is Dynamic Programming and How To Use It
CS Dojo
26 Python Tutorial for Absolute Beginners #1 - What Are Variables?
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
27 What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
28 How to Use If Else Statements in Python (Python Tutorial #2)
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
29 Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
30 How To Use Functions In Python (Python Tutorial #3)
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
31 What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
32 Introduction To Lists In Python (Python Tutorial #4)
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
33 Introduction to For Loops in Python (Python Tutorial #5)
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
34 What Programming Language Should I Learn First?
What Programming Language Should I Learn First?
CS Dojo
35 What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
36 While Loops and The Break Statement in Python (Python Tutorial #6)
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
37 More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
38 How to Learn to Code - Best Resources, How to Choose a Project, and more!
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
39 How To Use Dictionaries In Python (Python Tutorial #8)
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
40 Data Structures & Algorithms #1 - What Are Data Structures?
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
41 An Overview of Arrays and Memory (Data Structures & Algorithms #2)
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
42 Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
43 Classes and Objects with Python - Part 1 (Python Tutorial #9)
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
44 Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
45 Classes and Objects with Python - Part 2 (Python Tutorial #10)
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
46 Introduction to Linked Lists (Data Structures & Algorithms #5)
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
47 Introduction to Recursion (Data Structures & Algorithms #6)
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
48 Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
49 Amazon Coding Interview Question - Recursive Staircase Problem
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
50 Using Boolean in Python (Python Tutorial #11)
Using Boolean in Python (Python Tutorial #11)
CS Dojo
51 Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
52 What Can You Do with Python? - The 3 Main Applications
What Can You Do with Python? - The 3 Main Applications
CS Dojo
53 Facebook Coding Interview Question - How Many Ways to Decode This Message?
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
54 List Comprehension Basics with Python (Python Tutorial #12)
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
55 How To Use Sets in Python (Python Tutorial #13)
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
56 Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
57 Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
58 6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
59 Google Coding Interview - Universal Value Tree Problem
Google Coding Interview - Universal Value Tree Problem
CS Dojo
60 Best laptops for programming? How to get a job at Google? - And other FAQ’s!
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo

Related Reads

📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Learn to optimize palindrome detection using Manacher's Algorithm with mirror boundary optimization, reducing time complexity to O(N)
Dev.to · Dipaditya Das
📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Learn to build a power grid inside Minecraft using BFS algorithms and understand its relevance to cloud services
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Learn how Run-Length Encoding (RLE) compresses simple strings, a fundamental technique in programming and data compression, and apply it to optimize storage and transmission of data
Medium · Programming
📰
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Solve the Product of Array Except Self problem on LeetCode to improve coding skills and learn array manipulation techniques
Medium · AI
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →