Binary Tree 1. Constructing a tree (algorithm and pseudocode)

This is the first in a series of videos about binary trees. It is an explanation of the dynamic data structure known as the Binary Tree. It describes the way in which a binary tree is constructed, and how it can be represented numerically using a system of left and right pointers. This video goes on to describe some pseudocode which takes an iterative approach to constructing a binary tree using 3 array variables.

Пікірлер: 51

  • @KuntaKinteToby
    @KuntaKinteToby3 жыл бұрын

    I paid $8600 for a bootcamp course and I've learned more from your channel for free. Honestly you've saved my career as a programmer because I was beginning to think I'm just not smart enough to get these concepts. But I was wrong, I just wasn't being taught properly. I understand this stuff really easily with proper explanation.

  • @ComputerScienceLessons

    @ComputerScienceLessons

    3 жыл бұрын

    Thanks for the lovely comment. Stick with it. There's lots of great material online :)KD

  • @KuntaKinteToby

    @KuntaKinteToby

    3 жыл бұрын

    @@ComputerScienceLessons You're very welcome, thank you for the excellent guidance!

  • @atlantic_love

    @atlantic_love

    Жыл бұрын

    I'm not understanding how you had any "career as a programmer" when you took a "bootcamp course".

  • @KuntaKinteToby

    @KuntaKinteToby

    Жыл бұрын

    @@atlantic_love Do I really have to explain to you how going to school and then building a portfolio works? I'm not going to, but thanks for replying to a 2 year old comment to say something nasty and condescending, I hope it feels real good. I'll be blocking you now.

  • @atlantic_love

    @atlantic_love

    Жыл бұрын

    @@KuntaKinteToby Nasty? Nobody was nasty. I simply questioned you. I don't believe you, in other words. Spreading misinformation is disingenuous.

  • @jibran6635
    @jibran66352 жыл бұрын

    Highly underrated channel!

  • @ComputerScienceLessons

    @ComputerScienceLessons

    2 жыл бұрын

    Thank you. :)KD

  • @jibran6635
    @jibran66352 жыл бұрын

    Damn, my code looked exactly like your pseudocode, except in one place: instead of using the pointers to the elements in the array/data structure whose elements you use to form the tree, I dynamically allocated memory everytime I needed it. Learnt a lot about pointers and dynamic memory allocation along the way!

  • @ComputerScienceLessons

    @ComputerScienceLessons

    2 жыл бұрын

    Excellent. Once you understand the fundamental principle of the algorithm, you can adapt and refine it in all kinds of ways when you implement it. :)KD

  • @filza18pk
    @filza18pk4 ай бұрын

    Such simple explanations, thank you so much

  • @ComputerScienceLessons

    @ComputerScienceLessons

    4 ай бұрын

    You're very welcome :)KD

  • @Dall1n
    @Dall1n4 жыл бұрын

    Brilliant explanation!!!

  • @Mlridge
    @Mlridge3 жыл бұрын

    sir you are a legend

  • @Domappcent
    @Domappcent4 жыл бұрын

    This Helped so much. Thankyou! :D

  • @ComputerScienceLessons

    @ComputerScienceLessons

    4 жыл бұрын

    You are very welcome. :) KD

  • @JulianSloman
    @JulianSloman Жыл бұрын

    JavaScript guy here - haven't worked with trees - the result looked like a mess to me (i.e. despite a lot of comparisons the tree isn't sorted such that you could easily find stuff) - is this really somehow more efficient for some things?

  • @Ashwinbta
    @Ashwinbta Жыл бұрын

    You just saved my school life

  • @ComputerScienceLessons

    @ComputerScienceLessons

    Жыл бұрын

    You are very welcome :)KD

  • @azkymohamed123
    @azkymohamed1237 жыл бұрын

    Thank you sir

  • @tendaijakutindi4175
    @tendaijakutindi41757 ай бұрын

    Thank you !

  • @roasted_guava5706
    @roasted_guava57063 жыл бұрын

    Do you have any advice of how to learn these ADTs quickly? I have my A levels exam (CIE 9608 P4) in one month and I left data structures last and I shouldn't have done that. (You have the best explanations on the internet btw)

  • @ComputerScienceLessons

    @ComputerScienceLessons

    3 жыл бұрын

    Stick to the ones mentioned in the specification of your course. Namely: stack, queue, linked list, dictionary, binary tree. Get to know the way they work first, i.e. what do they look like when you add and remove data items. You should be sketching lots of diagrams. Then move on the the written algorithms. Study the past papers and sample papers (and mark schemes) on the exam board's website and get a feel for the style of questions; often they ask the same questions year after year but with different data www.cambridgeinternational.org/programmes-and-qualifications/cambridge-international-as-and-a-level-computer-science-9608/past-papers/ Give it about an hour data and don't neglect the other stuff you need to know. Stay positive, a month is plenty of time if you use it wisely. Good luck. :)KD

  • @roasted_guava5706

    @roasted_guava5706

    3 жыл бұрын

    @@ComputerScienceLessons Thanks for the advice! I will try my best

  • @LyricalLemon

    @LyricalLemon

    Жыл бұрын

    @@roasted_guava5706 how did it goo lol??

  • @barrydevine
    @barrydevine Жыл бұрын

    Hi, are the 3 array variables defined in the form of a record? Then the array is is defined as Type record?

  • @ComputerScienceLessons

    @ComputerScienceLessons

    Жыл бұрын

    You could use three array variables, or you could use a two dimensional array. I'm not sure I understand your terminology. Personally, I would not describe these arrays as records.

  • @umer09
    @umer095 жыл бұрын

    Is this a Binary Tree or Binary Search Tree? As per my understanding, this should be a binary search tree since the data is being assigned according to rules. Can you please confirm?

  • @ComputerScienceLessons

    @ComputerScienceLessons

    5 жыл бұрын

    The name 'Binary Search Tree' is often shortened to 'Binary Tree'. Same thing.

  • @princesspranaya7123

    @princesspranaya7123

    4 жыл бұрын

    @@ComputerScienceLessons I dont think so. Every binary search tree is a binary tree but binary tree is not a binary search tree. Binary tree just keeps adding nodes to right and left.

  • @KuntaKinteToby

    @KuntaKinteToby

    3 жыл бұрын

    @@princesspranaya7123 They technically aren't the same that's true, but in practice when people say 'binary tree' they mean 'binary search tree', because a true/pure 'binary tree' isn't nearly as useful.

  • @Nanagos
    @Nanagos2 жыл бұрын

    There is only one question left... What is it used for and why do you need something like this?

  • @ComputerScienceLessons

    @ComputerScienceLessons

    2 жыл бұрын

    Good question. In hindsight, I wish I had said more about the applications of binary trees. Database indexes are constructed like this to enable fast retrieval of data. Network routers also store information about other routers on a network like this, for the same reason - fast retrieval. Some data compression algorithms (E.G. Huffman encoding) make use of binary trees. There's a good Stack Overflow article about it here. stackoverflow.com/questions/2130416/what-are-the-applications-of-binary-trees :)KD

  • @Nanagos

    @Nanagos

    2 жыл бұрын

    @@ComputerScienceLessons thank you :D Looks like stack overflow has always an answer for you 😂

  • @thasneemjaleel5911
    @thasneemjaleel59113 жыл бұрын

    do you have python version of this

  • @rassimsimou1594
    @rassimsimou1594 Жыл бұрын

    Good

  • @RyanGrange
    @RyanGrange Жыл бұрын

    What happens if you have two nodes with the same value?

  • @filza18pk

    @filza18pk

    4 ай бұрын

    Less than equal condition with left so it will be allocated to left

  • @Root3264
    @Root3264 Жыл бұрын

    I get the urge to reimpplement this lesson in code, use all of the Harry Potter cast and then see who comes out on top.

  • @armeenhossain665
    @armeenhossain6655 жыл бұрын

    I didn't the ptr part? Explain me the line "Do until ptr = 0"?

  • @ComputerScienceLessons

    @ComputerScienceLessons

    5 жыл бұрын

    Hi Armeen. The algorithm is trying to find an unoccupied position in which to put the new node. Imagine a tree that already has lots of nodes in it, and we are trying to add a new node. If a potential parent of the new node has a left pointer with a value of 0, it means that it doesn't already have a child on the left of it, so this position is free for the new node if it needs to go there. If a potential parent of the new node has a right pointer with a value of 0, it means that it doesn't already have a child on the right, so the right position is free if this is where it needs to go. This is why each loop in the pseudo-code runs until it finds a ptr value of 0. The exit condition of the loop (Do until ptr = 0) is a bit like saying "do until we find a parent with nothing already attached in place I need to go". Make sure you understand the diagram and the table first and the pseudo-code will start to make sense. Stick with it. It still amazes me how someone invented this in the first place.

  • @armeenhossain665

    @armeenhossain665

    5 жыл бұрын

    Correct me if I am wrong. Ptr works as a free pointer?

  • @silenthill8312
    @silenthill83122 жыл бұрын

    You do save lives

  • @ComputerScienceLessons

    @ComputerScienceLessons

    2 жыл бұрын

    I presume you have an exam. Good luck :)KD

  • @alanchen5400
    @alanchen5400 Жыл бұрын

    literal W programmer

  • @DX413RB8
    @DX413RB811 ай бұрын

    So terminal nodes can also mean infertile, yes? XD

  • @ComputerScienceLessons

    @ComputerScienceLessons

    11 ай бұрын

    Absolutely! :)KD

  • @ememmeme8722
    @ememmeme87223 жыл бұрын

    nice content. although I don't like the pseudocode. I code in python, java and cpp but still, your pseudocode looks like a Frankenstein's monster to me.

  • @ComputerScienceLessons

    @ComputerScienceLessons

    3 жыл бұрын

    I like VB because it looks most like the pseudocode used on UK exam papers :)KD