Swift 3 Fun Algorithms: Recursive Search through Binary Tree

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ •

  • @rogerwprice
    @rogerwprice 7 років тому +2

    What a fabulous step by step explanation and demonstration - THANKS!

  • @everton_dev
    @everton_dev 7 років тому

    By far the best channel to learn swift and iOS development. Very well explained concepts, I've learned so much with your videos. Thank you!

  • @young_vz
    @young_vz 7 років тому +1

    Another great video!
    I love watching your algorithm vidoes!

  • @captainTahir
    @captainTahir 7 років тому

    Great!! You are No1 Developer ..

  • @cesaredecal2230
    @cesaredecal2230 7 років тому +1

    first :3 thank you so much for everything you're doing Brian! I'm currently following your Swift: Firebase 3 course :) will check this recursive search algorithm out!

  • @巴貝里奇
    @巴貝里奇 4 роки тому

    pretty useful tutorial
    and you made the algorithm easy to learn

  • @fabianhaglund5792
    @fabianhaglund5792 7 років тому

    Nice and easy to understand, cheers

  • @SRX875
    @SRX875 7 років тому

    Great video! It would be cool if you upload more videos like this!

  • @doublegdog
    @doublegdog 7 років тому

    Very well articulated video. Keep up the great work!

  • @marekchojecki4746
    @marekchojecki4746 7 років тому +12

    Love fridays because of this series :)

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +2

      Great to hear that, friday's are great indeed.

    • @gerryramosftw
      @gerryramosftw 7 років тому

      I gotta agree with this comment, I hope every friday has good data structures and algorithms like this one

  • @ahopdanzer
    @ahopdanzer 7 років тому

    nice video for introducing recursive

  • @amirrezaavani2234
    @amirrezaavani2234 7 років тому

    It was really helpful and now I can feel sweets of codding
    Thank you.

  • @piyushsharma1664
    @piyushsharma1664 7 років тому

    thanks for sharing this useful video, hoping to learn more data structures from you.

  • @kenny_speaks
    @kenny_speaks 7 років тому +1

    seeing list.index(where: {$0 == 20}) was neat! Never used it before, will come in handy! Great video, I've been confused about the binary tree and the Node() class implementation I've seen in Swift examples. This helped a ton! :) thanks

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      Yep, the thing with algorithms is that more often than not we forget to explain why it's important to know how to do these things. An example of comparing efficiencies is extremely helpful.

  • @misc.2331
    @misc.2331 7 років тому

    Loving these algorithm videos.

  • @Zakawaka45
    @Zakawaka45 7 років тому

    These are amazing! Please do more

  • @huslerbling
    @huslerbling 7 років тому

    I am so glad I've subscribed to your channel! You're awesome!

  • @rogerantonell9658
    @rogerantonell9658 7 років тому

    Really good job, improving every video👍

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      Great to hear you guys learning so much from these short lessons.

  • @doktoren99
    @doktoren99 7 років тому

    This was superb man. I like these shorts :)

  • @CodeWithChris
    @CodeWithChris 7 років тому +5

    Algorithms class from university back to haunt me! haha Great lesson, Brian!

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +5

      These algorithms are quite fun and challenging, definitely very rewarding when coding this out on the interview whiteboard. I've found myself using similar coding techniques when solving tree structures at work.

    • @CodeWithChris
      @CodeWithChris 7 років тому

      Lets Build That App it's been ages since conducting or taking any interview for me so I'm pretty rusty with them. Thanks for doing the series!

  • @pe04rh
    @pe04rh 7 років тому +9

    Hi there, I really like your video but I have an honest question, in your first implementation i.e. before you implement a solution for BST, isn't the complexity of your search O(n)? As your first solution does not appear to care whether or not there is any order, it searches every node from left (first) to right and since 20 is furthest right you should get 6 iterations, which is no better than a linear search on an array. Sorry if I am wrong or mistaken, and I would love for someone to explain why this is not the case.

  • @PythonPlusPlus
    @PythonPlusPlus 5 років тому

    12:45 You seem to be a little mistaken, your tree search iterated over every element in the array which made it take just as long (probably even longer due to dereferencing). To make a fair comparison you must also take into account the iteration which returns true at the end, so you are actually seeing 6 iterations, same as the list search. The list search said 7 times but it is also including the call for index, so it's actually 6.

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +1

      Yes the video took a while to record and I’m no longer sure I was able to show the original intended result. Recording live coding is a nightmare that I don’t recommend people get into.

  • @neelharsh56
    @neelharsh56 5 років тому

    Nicely explained

  • @jincat3057
    @jincat3057 7 років тому

    Great video! Love algorithm lecture from your channel! Wish we could learn more lessons about data structure and algorithm in Swift here! Thank you Brian.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      Yeah, every Friday we'll have an algorithms exercise. Stay tuned.

    • @jincat3057
      @jincat3057 7 років тому

      Great!! I will wait for the happy Friday night d==(^o^)b Maybe we could use some questions from Leetcode for reference? Such as from sorting, array, linked list, string, then to tree, queue, searching, etc.

  • @piyushsharma1638
    @piyushsharma1638 6 років тому

    very well explained

  • @zigabesal8254
    @zigabesal8254 7 років тому +1

    Excellent video with really good explanation 👌

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +2

      +Ziga Besal thanks, explaining this took a few tries but glad I finally got it down.

  • @PythonPlusPlus
    @PythonPlusPlus 5 років тому

    I decided to use a switch because it looks nicer. I couldn't decide wether or not to use the guard statement, I decided to just put everything in the switch in the end because it looks neater that way. My attempt at the optimised search function:
    func search(node: Node?, searchValue: Int) -> Bool {
    switch node?.value {
    case nil:
    return false
    case let value where value! > searchValue:
    return search(node: node!.leftChild, searchValue: searchValue)
    case let value where value! < searchValue:
    return search(node: node!.rightChild, searchValue: searchValue)
    default:
    return true
    }
    }

  • @trishalapatne6094
    @trishalapatne6094 7 років тому +1

    Thank you for this great video. You made BST easy now. Please do sorting algorithms and other data structures too.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +1

      +trishala patne I haven't decided what to do for next Friday yet.

  • @MegaGandalf99
    @MegaGandalf99 7 років тому

    Great video! Thanks to you!

  • @mirsha3054
    @mirsha3054 7 років тому +1

    Hi brian, very interesting , learned many new concepts! cheers ;)

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +2

      Many more fun algorithms to come in the future.

  • @developerios6096
    @developerios6096 7 років тому

    Something different from twitter series and others, alghoritms are very important IMO... keep it up mate

  • @calebdavis308
    @calebdavis308 7 років тому

    Your videos are great man, thank you so much. Would you maybe consider doing a video on security with iOS? A lot of jobs really love when a developer understands how to have secure data in apps and what not and a lecture from you would be so awesome to have.

  • @geomichelon
    @geomichelon 7 років тому

    another greate video...What you think about crossover company ?

  • @1Watchmerock
    @1Watchmerock 7 років тому

    great video man!

  • @PraduhTV
    @PraduhTV 7 років тому

    Great video!

  • @ronyfhebrian2629
    @ronyfhebrian2629 7 років тому

    This is so cool thank you Brian!
    Could you please make Bitap algorithm and Horspool algorithm in string matching too? Thank you.

  • @taylormaxwell1277
    @taylormaxwell1277 5 років тому

    Very very helpful

  • @timjohnson3913
    @timjohnson3913 7 років тому

    Awesome video Brian! This is helping me prepare for my first live coding interview on Thursday. I wonder if the reason the "(4 times)" is shown when search value is 11 and "(5 times)" is shown with a search value of 20 is because the entire non-nil left side of the tree is searched before the right side. So in the case of searching 11... 5, 1, 14 & 11 nodes are searched.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +1

      Hi Tim, someone explained why in the comments below but I can't remember the answer.

    • @timjohnson3913
      @timjohnson3913 7 років тому

      Hmm... I looked through all the comments and I think it may have been left in the comments of another video. If I come across the explanation, I'll copy/reference it here.

  • @pariveshsharma
    @pariveshsharma 7 років тому +4

    I was once asked in a interview and I could not write the algorithm, I knew how it works though. :(
    thanks for this video.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +1

      Yeah, this is a very very common interview question for students right out of college. If you can understand and write out similar algorithms to this, you should be able to get past a lot of interviews.

  • @MrYoklmn
    @MrYoklmn 7 років тому

    Hi Brian! Great videos! Can you make a tutorial video and explain, how to use requests with cookies parsing and set them back? It is very difficult to understand that using stack overflow etc...

  • @AmitBiswastunebox
    @AmitBiswastunebox 4 роки тому

    Hi can you please create videos for DFS / BFS in Swift ? Thanks

  • @GulamALI-ni4ot
    @GulamALI-ni4ot 4 роки тому

    Thanks for the amazing tutorial Brian, I have a question what if we want to search for an item which does not present in tree ? how we handle that case ? its getting crash when I search for a value which does not present in Tree.

    • @dambar011
      @dambar011 4 роки тому

      if node == nil { return false }

  • @soywil
    @soywil 7 років тому

    This is really good, thanks for the great video.
    I got an interview question once of how to balance and unbalance tree and then find the number, any thoughts?

  • @ElouahhabyAhmed
    @ElouahhabyAhmed 7 років тому

    Hello, thanks for your tutorials are simple.
    I have one request , you could do other videos: Heap, Hash table, BST, red-black, Kruskal., prim, Belmann-Ford, Dijkstra and Rabin-Karp. Thanks

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +2

      All these terms from college which is a decade ago for me...

  • @studentprogrammer9115
    @studentprogrammer9115 6 років тому

    InterView question: Recursion Definition: is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first: in simple english: its a step by step repetition way of solving programming question using algorithm /class / function etc ultimately getting to a solution..

  • @joshburt35
    @joshburt35 7 років тому

    Great video Brian, thank you! I have 2 questions and a possible error. First, what is a practical application of data being structured in a tree...maybe a flat database like Firebase? Second, who would do the structuring of the data? And finally, when I enter 6-9 as the searchValue, it is false but doesn't show the number of times it runs through the function. Every other number in the series 1...20 works. Thanks again for the great exercises.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      You can't trust playgrounds, you should go through the iterations in your head, similar to a live interview.

  • @ronyfhebrian2629
    @ronyfhebrian2629 7 років тому +2

    Could you do sorting algorithm? THank you!

  • @mellet89
    @mellet89 7 років тому

    I would assume that the list.index operation on an array is a O(N). But since its sorted you could do a binary search on the array to get it to log N. Not sure tho if swift has a method for a binary search on an array.
    Also as a sidenote; If the binary tree is unbalanced, in worst case your recursion could be as slow as the array search.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      O(n) is also my guess for list.index from what the iteration count shows. For binary searching log(n) is quite ok on the sorted list.
      Good analysis on the unbalanced tree scenario, I had already forgotten about that case.

  • @jessicapeng6270
    @jessicapeng6270 5 років тому

    Thank you!

  • @LautaroGalanCid
    @LautaroGalanCid Рік тому

    Id like to know how you create it dinamically instead of manually creating instances of a case you know

  • @mohamethseck
    @mohamethseck 7 років тому

    Yo Brian is a MacBook Air good enough to run on Xcode and swift. What's the appropriate hardware? Do I need more RAM? More SSD? A better processor? I wanna get into app development.

  • @iamsanketray
    @iamsanketray 7 років тому

    I am kinda new to binary trees...Yes this method looks cool but ....The example shown above has only 6 elements....and you constructed each node and each element. What if there were 100 elements? Do we write down each and every case? Won't binary searching method b much more less code?

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      When you search on google, it could be binary searching through all possible matches for your keywords, which are millions and millions of pages.

  • @AdnanKhan-ug1qn
    @AdnanKhan-ug1qn 7 років тому

    hello sir, I hope you are well. first of all i would like to thank you for the tutorials I love to see them. I have a request for you, can you make tutorial about converting any website or WordPress blog into iPhone app if you do it would be very helpful for me I want to learn that.

  • @nananatd3631
    @nananatd3631 7 років тому

    what is the "let" ? is that a java function ?

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      +Nat D it's just swifts way of declaring a constant variable

  • @PythonPlusPlus
    @PythonPlusPlus 5 років тому

    Indirect enums are great for representing trees:
    indirect enum BinaryTree {
    case Branch(value: Int, left: BinaryTree, right: BinaryTree)
    case Leaf(value: Int)
    case Stump
    static func LeftBranch(value: Int, child: BinaryTree) -> BinaryTree {
    return .Branch(value: value, left: child, right: .Stump)
    }
    static func RightBranch(value: Int, child: BinaryTree) -> BinaryTree {
    return .Branch(value: value, left: .Stump, right: child)
    }
    }
    let oneNode = BinaryTree.Leaf(value: 1)
    let fiveNode = BinaryTree.LeftBranch(value: 5, child: oneNode)
    let elevenNode = BinaryTree.Leaf(value: 11)
    let twentyNode = BinaryTree.Leaf(value: 20)
    let fourteenNode = BinaryTree.Branch(value: 14, left: elevenNode, right: twentyNode)
    let tenRootNode = BinaryTree.Branch(value: 10, left: fiveNode, right: fourteenNode)
    func search(_ tree: BinaryTree, for searchValue: Int) -> Bool {
    switch (tree) {
    case .Branch(let value, _, _) where value == searchValue,
    .Leaf(let value) where value == searchValue:
    return true
    case .Branch(let value, let child, _) where value > searchValue,
    .Branch(let value, _, let child) where value < searchValue:
    return search(child, for: searchValue)
    default:
    return false
    }
    }
    search(tenRootNode, for: 20)

  • @gabrieligliozzi9621
    @gabrieligliozzi9621 7 років тому +1

    Best videos ever

  • @ladedadedaschlobonmeknob7850
    @ladedadedaschlobonmeknob7850 7 років тому

    Mmmm i learned this in college with Python, but we were looking for files in a directory. Very identical!

  • @hanisster
    @hanisster 7 років тому

    maybe you should do a video on xcode keyboard shortcuts...

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому

      I don't think I use a whole lot of shortcuts, just the typical tabbing and CMD + LEFT or CMD + RIGHT. Perhaps I tend to type a little fast so I don't put you guys to sleep.

  • @Xyrer
    @Xyrer 2 роки тому

    My question is... Why? Why do companies do this? What is the real world application of this and why I want to learn about this aside from interviews? I'm sure there is a case for this but I have never found it

    • @LetsBuildThatApp
      @LetsBuildThatApp  2 роки тому

      This is literally used to search through google results to make it faster.

  • @SenorQuichotte
    @SenorQuichotte 6 років тому

    If the distribution is unknown or random, this algorithm will not work. Use pivot points to divide and conquer the array.

  • @swathys9820
    @swathys9820 4 роки тому

    But the search algorithm will get called 5 times when you search for 20.

  • @arehsan623
    @arehsan623 7 років тому

    One thing I don't like about this channel
    WHY can't we leave infinite likes on each video!?