 Hello everyone today. We'll talk about our last data structure in this course, which is the binary search tree and trees in general, which are a really important concept in computing and computer science And you'll see them again in future courses. So when we're talking about a tree We have to start by talking about graphs as in graph theory maybe you've heard about graph theory at least in the context of mathematics and We're talking about graphs. We're not talking about those simple things where you've got, you know, a chart with some lines on it Instead graphs, at least in mathematics, are a set of objects in which some pairs of the objects are related. Okay, so I've got a picture of a graph here or graphic of a graph up here and graphs are Consists of vertices which are kind of the nodes here in this graph and the vertices are connected by edges, right? So graphs and and that's it. It's the intuition graphs show up everywhere in computing You can probably guess that this has a lot to do with networking, right with maps Things like that. And that's in fact where graph and got it's or graph theory got its start Way back in the 1700s Leonard Euler one of the most famous mathematicians He gave us the little e the natural logarithm He lived in a place called Konigsberg and he liked to take walks to kind of clear his head And Konigsberg had seven bridges in it. There's a little island in the center of this river and He posed the question to himself, you know, I wonder Is there a way that I can cross all seven of these bridges Exactly one time so without going across one another And what he realized was, you know, maybe I can formulate this as a mathematical problem And so he sort of abstracted the problem into this problem of graphs Okay, so What the way he thought of it was well, there's actually four land masses here in the north the island in the middle of the south And then there's another kind of island off to the right here And he drew, you know, so he translated these into four nodes and then they were connected by the bridges, right? And He said, well, you know, he said about coming up with this What became the foundation for modern graph theory of reasoning about problems like this And so I ask you the same question he posed himself. Can you find can you get Uh across each connection here With only visiting each connection one time in other words, can you only traverse each edge One time and the answer is no, right? So try that out Um, so this is a graph, right? Now trees come from this idea of graph theory A tree is a special type of graph that is an acyclic connected graph. So what in the world does that mean? Well connected means that you can get to any node From any other node. In other words, there's no there's no little island of nodes down here, right? There's a path between every node Uh, you have you may have to go through multiple nodes, but that's fine It's just that you can get from a to h and b to a you can get to every one of these Nodes from every other node. That's what connected means acyclic means There's a every node is connected or any two nodes are connected by exactly one path. Okay, so C is kind of the nexus of this tree here of this graph um, and all Almost all paths go through c, but there's no like Triangles in here or squares you have to there's only one way to get between any pair of nodes That's what a tree is. Okay, so you're familiar with trees, uh, both outdoors hopefully and in science right here's a tree for, um Animal classification plant and animal classification the taxonomy um File systems are organized according to trees. You have the root of the file system And then it descends into different things. So this is a linux file system or even a mac file system Uh windows the root is c colon backslash, right? But there Trees in a file uh file systems and computers follow trees, right? So trees show up everywhere in computer science including in this class including in data structures So we are going to implement a tree data structure and it's going to be very useful for us um The reason is so first of all trees are going to be a non-linear data structure They're different from say an array based list where everything is kind of packed in Together or even a linked list where everything is, you know, there's previous there's next there's things packed in It's a little bit more like a hash table in a way We're going to implement it differently. But, you know, things just kind of go here and there. They're not always clumped in right next to one another But the idea behind trees the thing that makes them so useful Is the same thing that makes merge sort and quicksort and binary search useful Which is if we divide up the work intelligently We can use divide and conquer algorithms that maybe are better than big o event Okay, so before we get into the code and the data structure and I know a lot of you just want to get into the code We're going to be really well served if we have a good conceptual understanding of trees. Okay, so first things first Uh, let's turn to the tree handout that you have and let's make sure that we understand the terminology that's going to be used Okay, so every tree has a root Uh, let me get my pen up here, right? So every tree has a root Uh, the root of this tree is a right, you know tree a is the root Oh, yeah, look at those awesome mouse drawings mouse letters a is the root of the tree meaning that everyone Comes out everyone is connected to the root in some way right the root is the base of the tree Uh trees also have leaves, okay Actually, we'll define the leaf in a second every Well, not every many nodes in the tree are going to have children Okay, so the children of a are its descendants so, uh C is a descendant b is a descendant and i is a descendant or a child. Excuse me. I should use the word child C, b, and i are child children of the root a Okay, c also has children who are c's children Well, that's j and f right b has three children d, g, and e now i'm not including Grandchildren here when I talk about children um the so A's children are c, b, and i and furthermore a is the parent of c, b, and i Okay, um collectively Though let's say that we're looking at everybody down here Okay, we can say that everybody down here are descendants Of a Everybody down here is a descendant of a Who's the descendants of c? Well the descendants of c are This group here. Okay, so uh descendants all have a common ancestor in this case. It's c um We can also say that c is the ancestor of f j and h Okay, so clearly the metaphor here is the family tree So if you understand the concept of a family tree, you will also understand You know most of the these terms over here, right? Um, who are h's ancestors? Okay, look at this for a minute and think who are h's ancestors? Well h's ancestors is everybody Who is connected to between h and the root of the tree? Okay, so h to f to c to a All right h's ancestors are f c and a right All right, who are the siblings of c? Well, what is a sibling in real life? A sibling is someone are people with common parents, right? So c siblings are b and i These people d g e These are not siblings of c. Okay c siblings are his you know, just like you your cousins are not Your siblings your siblings are only your brothers and sisters if you have them. Okay So that's the concepts here Some more tree specific concepts. Let me erase what's on the slide here for a second Okay, so some more tree specific terminology path, okay A path between two nodes is you know the list of edges that connect those nodes So let's say we want a path from h to E Okay, this is a tree. So there's only one path from h to e that path is h to f f to c c to a a to b b to e Okay, that's a path There's going to be only one path for each pair of nodes. You just need to know that term path All right the depth What is the depth of a node the depth of a node? Is the length of the path? From that node To the root. So remember the root is a up here Okay, so what is the depth of? Let's say i Well, the depth of i is the length of this path. So what's the length? It's just a count How many edges are between myself and the root? Well in the case of i its depth is one Okay How about p? What's the depth of p? Well, we got to count the edges between p and the root. So there are one two Three edges between p and the root. So the depth of p Is three What's the depth of the root? Okay, the depth of the root Well, there are no edges between the root and itself. So the depth of the root Is zero Okay So nodes have depth The tree Has a height Okay, the height of the tree Is the maximum depth among all the nodes in the tree So have a look at this tree. What is its height? The height is the maximum depth Well, um, I believe you know just kind of looking very quickly we can see here that the height of this tree Is going to be three, right? That's the maximum depth of any of these nodes So the height of this tree Is three All right So a path know the constant know all these terms that are on this slide. If you have any questions Let me know check out your book all right So let's move into thinking about a data structure First a binary tree So a binary tree is a tree in which each node has at most two children, right? So this guy over here is a binary tree if you go back and look at the previous one This is not a binary tree because some of these nodes a node The b node they have more than two children A binary tree is just a tree where each node has at most two children You can have one child like f And a node can have no children like h By the way, I think I forgot to define this didn't I yes nodes that have no children Are leaf nodes Okay, leaves are going to be very important for us when we go to use this thing in code, all right Binary trees can solve a variety of problems in computer science And the one that we're going to focus on is what's called a binary search tree So a binary search tree is a binary search tree with a very important property Here I've got the abbreviations of states The important property of a binary search tree is that Pick any node in this tree Let's say Georgia The property of a binary search tree is that the value of the node is greater than All of its left descendants And less than all of its right descendants Okay, so Georgia We're sort of let's say that the comparison that we use here is alphabetical order, right We see that Georgia is greater than a k c t d e But it's less than h i Similarly, let's take a look at c t a binary search tree any node it's going to be Greater than its left descendants less than its right descendants Ct is greater than a k, but less than D Okay, take a look at the root node K s is greater than all of these descendants over here But it's less than all of these descendants over here So that's a by that's what makes a binary search tree a binary search tree Now we're going to code one of these things and it's going to be up to us the programmer to make sure Of this property, right? But the cool thing is if we maintain this property, we can do really interesting things First of all these contents are sorted, right? If you kind of look at this just kind of going from left to right It's sorted Which is kind of neat, right? We can use that to our advantage The other thing is when we go to put things in this tree If we think of it like a tree We can divide and conquer this problem just like binary search merge sort quicksort And if the tree is nice and balanced in other words, if it's not, you know, raggedy if everything's kind of flat and Fluffy here and the tree is kind of filled in Our algorithms for inserting for deleting and for searching this tree are going to be log base two event All right, so conceptually this is what we're aiming for we're going to go code it in a second. So Again pause for a minute make sure you really understand conceptually what this tree is Okay, uh binary tree is a tree in which any nodes value is greater than its left descendants Uh and less than its right descendants. All right, so we are let me get my uh big head out of the way here for a second Okay, turn off the webcam. So we are here, right? We have implemented All of these different data structures and data types in adt So we are going to create a binary search tree data structure that We will use to implement the map adt So you created an assignment number seven a hash map using hash tables We're going to create a map that works just the same Has the same Interface but using a binary search tree. Okay So how in the world are we going to implement this thing? Well, we could use a list an array based list We're not going to go there. It could waste a lot of space. It's pretty complex Instead what we're going to use is to think of this tree as comprised of a root node That is connected to Children and these children may have their own sub trees Okay So the key word there is node and what other data structure did we have nodes in that's right? A linked list So we're kind of going back to linked list land where we're going to have nodes But instead of a node dot next We are going to have node dot left that kind of contains a connection to the left subtree And the node dot right Can they contains the reference or the connection to the right subtree? Okay, so we are going to have tree nodes Not three nodes tree nodes and They're going to look like this Okay Every node is going to have some data Maybe more than one piece of data a left child and a right child And then our goal whenever we go to put things in the tree to remove things from the tree to search through the tree Is we're going to take advantage of these links here these left and these right children to kind of go through the tree And do the work we need to do all right So that's the introduction to tree and tree concepts Make sure you're really familiar with all the vocabulary make sure you're really Comfortable in understanding What it means to be a binary search tree now We're going to switch over to the binary search tree worksheet and kind of dig into this concept a little bit more