As we continue learning more about abstract data types, or ADTs, we were introduced about a brand new ADT called trees. It was a very difficult data structure to understand at the first try for me since there were a lot of new terminologies and techniques that I did not understand, but now that I think I more or less got the hang of it, I thought I'd give my two cents regarding this topic. I'd start with a brief introduction of what trees are and some of the terminologies behind it. Then, I'll talk about some actions that you can carry out with the tree data structure. Lastly, I'll also talk about binary trees and binary search trees.
Now, first of all, we can confirm that trees are an abstract data type because just like any other ADT, we specify the kind and meaning behind the data type we store, and various methods we can carry out on that data type in a tree. The way a tree works is that there is a parent node, which may or may not contain data, that is connected to one or more child nodes, or children through edges. All trees begin from a single node, which we call the root. The interesting thing about trees is that except for the root, all nodes have exactly one parent. We also have paths in a tree, which is basically a sequence of nodes from one node to another. In order for paths to exist, all consecutive nodes from the beginning node to the end node must have an edge connecting them. The number of edges in a path are what we called the length of the path. Since we know that all nodes except the root have exactly one parent, we can conclude that all nodes have exactly one path to its parent node as well as the root. Another interesting thing about trees is that it flows continuously from the root to the leaf (a node with no children), thus avoiding any possibilities of loops in a tree. If you want to find the height of a tree, you can do it by adding 1 to the length of the entire tree. Easy, isn't it? The last important terminology would be the arity, or the branching factor of the tree, which basically refers to the number of maximum number of children for any node.
There are lots of interesting functions and methods that we can carry out using the tree data structure. However, dealing with this data structure requires a solid grasp of recursion, so learning this topic is mandatory. First and foremost, we can carry out the basic methods like __repr__, __str__ and __eq__. As we can see, in the __repr__ and __str__ function, once we carry out the operation for the parent node, we call the same function again for the upcoming children node, which have their children node. Yeah, I think we know it now. By calling the function again within the same function, we are applying recursion. Sweet. We can also carry out operations like finding the height of the tree, and check to see if a certain value is present in any possible node of the tree or not, though it requires a strong understanding of recursion just like the other functions. One can also carry out traversal, which simply means visiting the nodes in order to carry out certain operations or functions in them. There are three types of traversals. They are:
i) in-order traversal: visiting a node between its children. Note that this is only applicable for trees that only have two children.
ii) pre-order traversal: visiting a node before its children.
iii) post-order traversal: visiting a node after its children.
Of course, one can also be creative and come up with many other unique operations and functions for trees.
Now I'd like to take a moment and talk about binary trees and binary search trees. A binary tree is a tree with the additional property that it has an arity of 2. In simple words, any node in a binary tree can have an utmost of two children, where the children in the left is called the left child, and the children on the right are called right child. A binary search tree is just a binary tree with the additional property that the value for every node is greater than all values of nodes in the left sub-tree and smaller than all values of nodes in the right sub-tree. This property of a binary search tree makes searching for values faster and more efficient since now, we can filter out a large chunk of values without having to go through each and every unimportant data individually.
Trees are a very important way of storing data and they can be used to represent sorted data, make data more easily searchable, and make the process of searching data faster and more efficient. I'm really interested in learning more about this data structure and I'm looking forward towards learning more. This is it for me today. See you guys later. Till then, stay awesome and keep coding.
i) in-order traversal: visiting a node between its children. Note that this is only applicable for trees that only have two children.
ii) pre-order traversal: visiting a node before its children.
iii) post-order traversal: visiting a node after its children.
Of course, one can also be creative and come up with many other unique operations and functions for trees.
Now I'd like to take a moment and talk about binary trees and binary search trees. A binary tree is a tree with the additional property that it has an arity of 2. In simple words, any node in a binary tree can have an utmost of two children, where the children in the left is called the left child, and the children on the right are called right child. A binary search tree is just a binary tree with the additional property that the value for every node is greater than all values of nodes in the left sub-tree and smaller than all values of nodes in the right sub-tree. This property of a binary search tree makes searching for values faster and more efficient since now, we can filter out a large chunk of values without having to go through each and every unimportant data individually.
Trees are a very important way of storing data and they can be used to represent sorted data, make data more easily searchable, and make the process of searching data faster and more efficient. I'm really interested in learning more about this data structure and I'm looking forward towards learning more. This is it for me today. See you guys later. Till then, stay awesome and keep coding.
No comments:
Post a Comment