Hope you guys are having a great fourth of July! I couldn't be more excited to bring this episode to you because trees are one of my favorite data structures! In my opinion, this data structure really helps bring you from a beginner developer to a more intermediate one and really helps open up a world of possibilities due to their hierarchical structure. Since there is SOO much you can cover with trees, I tried my best to give you guys a firm foundation of all the terminology and examples that trees can be used for. As always feel free to contact me if you have any questions regarding the information covered in this podcast
Email: [email protected]
SHOW NOTES
----------------
Tree ADT
So far we have focused on linear data structures
We want to minimize the most frequent operations
- so if we are looking for something, then we would want to use a sorted array and probably implement a binary search
Hierarchal data
This would be a place where trees would be used for
The root of the tree is at the top
- we are then branching out in the downward direction
A collection of entities called nodes linked together to simulate a hierarchy. Non-linear. The topmost node is called the ROOT
each node will contain some data
each node may also contain a link to a reference to another node called its children
Each arrow pointing from a node to another node is called a link
Children of the same parent are called SIBLING
The root node is the only node without a parent.
if there is a direct link to some other node then there is a parent-child relationship between the nodes
Any node in the tree that doesn’t have a child is called a leaf node
When we’re walking the tree we can only go in one direction
Definition
A tree can be called a recursive data structure that consists of a distinguished node called ROOT and its subtrees
in a tree with N nodes, there will be exactly n-1 edges(or links)
all nodes will have exactly ONE INCOMING EDGE
Depth
Length of the path from the root to x, or number of edges in the path from the root to x
Height
Number of edges in longest path from x to a leaf
the height of a leaf node is ZERO
Height of tree
The height of a tree is the height of the ROOT NODE
Binary Tree
A tree in which each node can have at MOST 2 children
Node
You will have two references used for a pointer to the left node and to the right node
The node will also have a field for storing the data of the node
APPLICATIONS OF TREE
Natural hierarchal data
- file system
Organizing data
- for quick search, insertion, deletion
(eg. Binary search tree)
Complete Binary Tree
All levels except possibly the last are completely filled and all nodes are left as possible
Assuming that the root depth is 0 (or level 0) then the maximum number of nodes at any given level can be represented by 2^l
- where l is the level
If all levels are completely filled, the binary tree can be called a PERFECT BINARY TREE
Maximum of number of nodes in a perfect binary tree can be represented by 2^(no of levels) - 1
The height of a complete binary tree can be represented as floor(log2(n))
Balanced Binary tree
We want to keep a binary tree balanced
DEF: we call a binary tree balanced if the difference between the height of left and right subtree for every node is not more than 1
How can we create a binary tree
1) dynamically created nodes
2) in some special cases, arrays are used for complete binary trees