AfterAcademy Tech
•
08 Jan 2020

Tree is a widely-used powerful data structure that is a viable addition to a programmer’s toolkit. A lot of complex problems can be solved relatively easily if the data is stored in a tree. It is a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear data structures.
Tree is a non-linear data structure which stores the information naturally in the form of hierarchy style.

Root
Edge
Child
Parent
Leaf

Height
Level

Trees can have any number of children but the simplest and most common type of tree is a binary tree.
Ancestor: Any node which precedes a node i.e itself, its parent, or an ancestor of its parents.
Descendent: Any node which follows a node i.e. itself, its child, or a descendent of its child.
Ordered Tree: A rooted tree in which an ordering is specified for the children of each vertex. For example: Binary Search Tree, Heap etc.
Size of a tree: Number of nodes in the tree.
A node in a binary tree can have a maximum of 2 child nodes.
There are primarily 5 types of binary trees:-
1. Rooted Binary Tree
Rooted Binary tree is the simplest type of binary tree where each node has either 0, 1 or 2 child nodes.

2. Full Tree
Every node has either 0 or 2 children. No node should have only 1 child node. It is also known as strictly binary tree.

3. Complete Tree
In a complete tree, all internal nodes have 2 children and all leaf nodes are on the same level. In other words, all levels in a complete tree are entirely filled to its maximum capacity. It is also known as perfect binary tree.

4. Almost Complete Tree
In an almost complete tree, all levels are entirely filled except the last level. The last level is filled from left to right.

5. Skewed Tree
A skewed tree is one in which all nodes have only one child except the leaf node. In a left-skewed tree, all child nodes are left child nodes and in a right-skewed tree, all child nodes are right child nodes.

As mentioned above, tree is a very powerful data structure and has a lot of advantages and applications. Some of these are:
We’ve all learnt what a binary tree is, what it’s properties are and what are its advantages, but do you know how to represent a binary tree?
Confused? No problem.
You are given a simple binary tree, how do you suppose you’re gonna store it in your program? This is known as the representation of a binary tree.
There are basically two primary ways we use to represent binary trees:
In linked representation of a binary tree, every node is an object of a class. The class is supposed to represent a node of a tree and each class has mainly three data members: the value of the node, the reference to the left child and the reference to the right child
class Node
{
int val
Node left
Node right
}
left holds the reference to the left child of a node and right holds the reference to the right child of the node. Similarly, the entire tree is constructed.
But what if there is no left child or no right child? Well, if there is no left child, the reference to the left child will hold the value NULL.

Advantages
In sequential representation, an array is used which stores the value of the nodes of each level from left to right.
The size of this array will be 2^h + 1, where H is the height of the tree. If a node is not present(known as a hole), it can be represented by a special character such as ‘-’.

As you can see, whether a node is present or not, memory is allocated for a complete binary tree of height h. So if there are a lot of holes present in the tree, the sequential representation might be a bad idea as it takes up a lot of space.
But how do we identify the child or parent node of a specific node in sequential representation?
Let us consider a node at index i.
Parent of a node: At index i/2
Left child of a node: At index i*2
Right child of a node: At index i*2+1
Advantages
These two are the most commonly used representations of a node. You can create some other representation too that suits your tailored needs.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Data structures are widely used in every aspect of computer science. Data structures are the way of organizing and storing data in a computer so that it can be used efficiently. In this blog, we will look into data structures, its types, operations and applications.

AfterAcademy Tech
Heap is a very useful data structure that every programmer should know well. The heap data structure is used in Heap Sort, Priority Queues. The understanding of heaps helps us to know about memory management. In this blog, we will discuss the structure, properties, and array implementation of heaps.

AfterAcademy Tech
Given a binary tree, write a program to find the lowest common ancestor (LCA) of two given nodes in the tree. The question is asked previously in Amazon, Facebook, Adobe and requires an understanding of tree data structure.

AfterAcademy Tech
Find all nodes at a distance k from a target node. This problem requires the knowledge of tree data structure. Recursion and tree traversals would be the base of the solution for this problem.
