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