Binary Search Tree Insertion

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·9y ago
Related Videos: Binary search tree intro: https://youtu.be/JfSdGQdAzq8 Binary search tree insertions: https://youtu.be/LwpLXm3eb6A Binary search tree removals: https://youtu.be/8K7EO7s_iFE Binary search tree traversals: https://youtu.be/k7GkEbECZK0 Binary search tree code: https://youtu.be/QwrZcySUxK8 Data Structures Source Code: https://github.com/williamfiset/algorithms My website: http://www.williamfiset.com =================================== Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5 A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix

What You'll Learn

Teaches Binary Search Tree insertion operations

Full Transcript

okay let's have a look at how to insert some elements into a binary search tree so let's dive right in so first to add elements to our binary search tree we need to make sure that the elements were adding are actually comparable meaning that we can order them in some way inside the tree meaning at every step we know whether we need to place the element in the left subtree or the right subtree and we're going to encounter essentially four cases so lend ensuing an element we want to compare the value to the value of the current node we're considering to do one of the following things either we're going to recurse down the left subtree because our element is smaller than the current element or we're going to recurse down the right subtree because our element is greater than the current element or there might be a cat case that the current element has same value as the one we're considering and so we need to handle duplicate values if we're deciding to add duplicate values to a tree or just ignoring that and lastly we have the case that we've hit a null node in which case it's time to create a new node and inserting our tree let's look at some animation now so on the Left I have a bunch of insert instructions so we have all these values you want to insert into our binary search tree and currently the search tree or the binary search tree is empty so first we want to insert 7 so 7 becomes the root of the tree view with the first node next we want to insert 20 so 20 is greater than 7 so we insert it to the right next we want to insert five so we always start the rear twin or inserting that's an important point so you start the root and then you move your down a tree to figure out where you want to insert the note so we start at the root and then we're like 05 is left and seven so we're going to fit it to the left now 15 start the root go to the right because 15 is greater than 7 then to the left PS 15 is less than 20 at 10 now 4 so 4 is less than seven move left 4 is less than five move left create the new node now we have four again so let's see what happens here so start seven move to the left we move to the left now we've encountered a value that already exists in our treat so as I said before if your tree sports duplicate values as the time to add another node and you would either picked by convention if you want it on the left on the right otherwise you'd do nothing and i'm going to choose to do nothing i want to insert 33 so start the root go to the right give 33 is greater go to the right again now insert two so two smaller than everything in our tree so it would go all the way to the left now try and see where 25 would go so 25 sings go to the right then we're going to go to the right again because it's greater than 20 but we're going to go left now because it's less than 33 and finally say so once left once right and we've inserted everything into our binary search tree so on average the insertion time is going to be logarithmic but in the worst case this behavior could degrade to linear time let's have a look at that so if our instructions are the following insert one two three four five and six so we start at one then I'm going to insert two so that's when we go to the right eight okay now let's insert three well 3 is greater than everything so I have to place the right began now how about four ok force also greater than everything Oh looks like recurring this line now and now 66 is still greater than everything so as you can see this type of linear behavior is really bad and we don't want to create lions like this because if we want a query where six is in a tree if we want to remove five I do anything on our buying your surgery you first need to find the note that's going to take linear time and this is bad that's one of the reasons why people have invented things like balanced binary search trees or self balancing trees which bounced themselves as you insert nodes will beginning to that later but that's it for insertion it's really simple don't over complicate it so the next video we're going to look at removals for binary search trees but also if you want some source code for binary search tree I've implemented one just go to my github repository and look under the binary search tree folder I'm going to be reviewing the code at the end of this series so stay tuned for that guys thanks so much for watching any questions just drop a comment I love reading them alright see ya
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 51 of 60

1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →