Tree
Tree Data Structure is a non-linear data structure in which a collection of
elements known as nodes are connected to each other via edges such that
there exists exactly one path between any two nodes.
Or
A tree data structure is a hierarchical structure that is used to represent
and organize data in a way that is easy to navigate and search. It is a
collection of nodes that are connected by edges and has a hierarchical
relationship between the nodes.
Tree terminologies
• Node: A node is a structure that contains a key or value
and pointers in its child node in the tree data structure.
• Edge: In a tree in data structures, the connecting link of
any two nodes is called the edge of the tree data
structure. In the tree data structure, N number of nodes
connecting with N -1 number of edges.
• Root Node: The topmost node of a tree or the node
which does not have any parent node is called the root
node. {A} is the root node of the tree. A non-empty tree
must contain exactly one root node and exactly one
path from the root to all other nodes of the tree.
• Parent Node: The node which is a predecessor of a
node is called the parent node of that node
• Child Node: The node which is the immediate
successor of a node is called the child node of that
node.
• Leaf Node or External Node or Terminal Node: The
nodes which do not have any child nodes are called leaf
nodes.
• Internal node or Non- Terminal Node: A node with at
least one child is called Internal Node.
• Ancestor of a Node: Any predecessor nodes on the
path of the root to that node are called Ancestors of
that node
• Descendant: A node x is a descendant of another
node y if and only if y is an ancestor of y.
• Sibling: Children of the same parent node are called
siblings.
• Level of a node: The count of edges on the path from
the root node to that node. The root node has level 0
• Subtree: Any node of the tree along with its
descendant.
• Degree of a node: In the tree data structure, the total
number of children of a node is called the degree of the
node.The highest degree of the node among all the
nodes in a tree is called the Degree of Tree.
• Path: In the tree in data structures, the sequence of
nodes and edges from one node to another node is
called the path between those two nodes. The length of
a path is the total number of nodes in a path.
• Depth: In a tree, many edges from the root node to the
particular node are called the depth of the tree.In the
tree, the total number of edges from the root node to the
leaf node in the longest path is known as "Depth of
Tree".In the tree data structures, the depth of the root
node is 0.
• Height: In a tree data structure, the number of edges
from the leaf node to the particular node in the longest
path is known as the height of that node. In the tree, the
height of the root node is called "Height of Tree".The
tree height of all leaf nodes is 0.
Binary Tree:
The Binary tree means that the node can have maximum two
children. Here, binary name itself suggests that 'two'; therefore,
each node can have either 0, 1 or 2 children.
Types of Binary Tree
Strict binary tree
Only if each node has either 0 or 2 offspring can the tree be
regarded as strict binary tree. The term "strict binary tree" can
also refer to a tree where all nodes, save the leaf nodes, must
have two children.
Complete Binary Tree
Nodes are not added to a new level until the preceding level is
fully filled because a Binary tree is known as a Complete Binary
tree if all the nodes are added from the left. All of the nodes must
be as far to the left as feasible in the final level.
Binary Search Tree:
A Binary Search Tree is a data structure used in computer
science for organizing and storing data in a sorted manner.
Each node in a Binary Search Tree has at most two children,
a left child and a right child, with the left child containing
values less than the parent node and the right child containing
values greater than the parent node.
Heap Tree
Heap data structure is a complete binary tree that satisfies the
heap property, where any given node is always greater than its
child node/s and the key of the root node is the largest among all
other nodes. This property is also called max heap property and
always smaller than the child node/s and the key of the root node
is the smallest among all other nodes. This property is also called
min heap property.
MAX HEAP TREE
MIN HEAP TREE
Array Representation of Binary Tree
For the array representation of binary tree, we will form an
array of size 2*n+1 size where n is the number of nodes the
binary tree. Now we will move step by step for the array
representation of binary tree.
Example 1:
1. First, suppose we have a binary tree with seven nodes
2. Now, for the array representation of binary tree, we have
to give numbering to the corresponding nodes. The
standard form of numbering the nodes is to start from the
root node and move from left to right at every level .After
numbering the tree and nodes will look like this:
3. Now as we have numbered from zero you can simply
place the corresponding number in the matching index of
an array then the array will look like this:
That is the array representation of binary tree
Example 2:
While giving a number you are stuck with the cases where
you encounter a leaf node so just make the child node of the
leaf nodes as NULL then the tree will look like this:
Now just number the nodes as done above for the array
representation of binary tree after that the tree will look like this:
9. Now we have the number on each node we can easily use
the tree for array representation of binary tree and the
array will look like this:
Tree Traversal
Tree Traversal refers to the process of visiting or accessing
each node of the tree exactly once in a certain order.
A Tree Data Structure can be traversed in following ways:
• Inorder Traversal
• Preorder Traversal
• Postorder Traversal
Inorder Traversal
Inorder traversal is defined as a type of tree traversal
technique which follows the Left-Root-Right pattern, such that:
• The left subtree is traversed first
• Then the root node for that subtree is traversed
• Finally, the right subtree is traversed
We start from A, and following in-order traversal, we move to its
left subtree B.B is also traversed in-order. The process goes on
until all the nodes are visited. The output of in-order traversal of
this tree will be –
D→B→E→A→F→C→G
Pre-order Traversal
In this traversal method, the root node is visited first, then the
left subtree and finally the right subtree.
We start from A, and following pre-order traversal, we first
visit A itself and then move to its left subtree B. B is also
traversed pre-order. The process goes on until all the nodes are
visited. The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Post-order Traversal
In this traversal method, the root node is visited last, hence the
name. First we traverse the left subtree, then the right subtree
and finally the root node.
We start from A, and following pre-order traversal, we first visit
the left subtree B. B is also traversed post-order. The process
goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Garbage Collection:
In computer languages, garbage collection is a crucial
component of memory management. It is the procedure of a
program's memory being automatically identified and released.
C programming lacks built-in garbage collection capabilities
because it is a low-level programming language. However, there
are a number of libraries that offer garbage collection features
for C programs. In this article, we will examine garbage collection
in C and how it is implemented using the, Boehm-Demers-
Weiser garbage collector library.
C offers low-level memory management mechanisms through its
malloc() and free() functions. The free() method is used to
release memory when it is no longer required, while the malloc()
function is used to allocate memory dynamically during runtime.
These functions' fundamental syntax is as follows:
void* malloc(size_t size);
void free(void* ptr);