Binary Search Tree Insertion
Skills:
Algorithm Basics90%
Key Takeaways
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
Original Description
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
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 51 of 60
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
▶
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI