Everything you need to know about tree data structure
In this blog you will learn about tree data structure.
Before you learn about tree data structure let me tell you why this is useful and a practical use of data structure.
The practical example
If you are using a browser, that means you are using a tree data structure. If you don't believe open the developer tools of browser using F12 key. Go to inspect tab. If you are a front end developer you know these.
The things you are seeing is a tree. Actually DOM(Document Object Model) is a tree. Even though it doesn't look like one. let's take a look of tree representation of what you are seeing right now.
First we have something called a html tag. Inside the html tag there are two more tags called head
and body
. Inside head
and body tags there are more tags. And again inside them there are more tags. You can also notice that these tags are getting nested more and more. Each tag is connected to a previous tag without the first tag. And you might have noticed some heirarchy in here. This is what we call a tree
data structure.
What is a tree data structure?
A tree data structure is a structure where each node is connected to a previous or parent node in a heirarchical order.
Ok. But that doesn't look like a tree. Let me prove it to you.
I hope this picture looks like a tree to you.
let's apply some css.
.tree {transform: rotate(180deg);}
We have just rotated our tree to 180 degrees. But it is still a tree. We have just rotated our tree to 180 degrees.
Let's see the properties and features of trees.
Node: You can think of a node as a leaf in a tree. A node is nothing but just a element of a tree.
Root Node: A root node is the top node that you see in the tree. It does not have any previous node.
Parent Node: Parent node is immediate previous node connected to the current node. It is just like our real life parent. A parent can have multiple child.
Child Node: Child node is the children of the parent node. And that makes sense right. A child can also be a parent.
Leaf Node: Less node is a child node but not a parent node. Or simply, a leaf node is a child node that no other node is connected to it from bottom.
Sibling Node: A sibling node is a child node connected to the same parent node. And that's what sibling means.
Direction: The connection direction of a tree is always unidirectional or one way. It always go from top to bottom. From root node to child node and its child node and so on. If you notice the picture you will get that.
Edge: An edge in a tree is the connection between two nodes. Suppose the connection between you and your parent.
Path: A path is nothing but consecutive edges between source node to given node.
Ancestor Node: All the nodes that are in the path of a source node and the target node are called Ancestor node. Like your Father ----> GrandFather -----> Great GrandFather -----> so on
Desecendant Node: Every node in the path of your current node to the leaf node are called ancestor nodes.
Level: Count the number of edges in the path from the root node to your current node. That number of edges are called level.
Depth of a Node: From the root node to the given node path, count the number of edges. Total Number of edges is the depth of that node.
Height of a Node: The total number of edges on the path from your current node to the most distant leaf node is the height of the node.
Degree of a node: The total number of children of a node is the degree of a node.
Height of a Tree: Maximum number of edges from the root node to the leaf node. A tree might have many leaf node. But you have to look for the most distant node. Then count the edges.
Some facts about Tree
- The height of a tree is always equal to the level of the tree
- Number of edges in the tree = (n - 1) [where n is the total number of nodes in the tree]
Common types of Data Structures.
- General tree data
- Binary tree
- Binary Search Tree
- AVL tree
- Red-Black tree
- Splay tree
- Treap tree and ...so on
So that's it for this blog. Soon I will create a video about tree data structure. So, SUBSCRIBE to Cules Coding
Until then stay safe.
Shameless Plug
Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.
Lessons
- Intro & Setup
- Build Homepage UI
- How our app will work
- MDX, MongoDB, Static Homepage
- Generate Static Blog Page
- Style Blog page with Chakra-UI and MDX-embed
- Build a real-time view counter
- Autocomplete search form with MongoDB Atlas Search Index
- Deploy application to Vercel
Demo
You can demo the website from here
Features
- Static Blog pages will make the website load faster.
- Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
- Autocomplete search feature for the blog posts.
- Real-time view counter and so on.
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full-stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project. I am available on Upwork
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Twitter: @thatanjan
- Upwork: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: