TreeDS Lecture Notes 02
TreeDS Lecture Notes 02
Table of Contents
Introduction to Tree in Data Structures
The Necessity for a Tree in Data Structures
Tree Node
Tree Terminologies
Types of Tree in Data Structures
Tree Traversal
Application of Tree in Data Structures
You must have observed the different parts of trees, from your childhood. A tree has
roots, stems, branches, and leaves. And each leaf in a tree linked through roots via a
unique path. Hence, similarly, a tree in data structures possesses hierarchical
relationships, e.g. family tree, Document Object Model (DOM) in HTML, etc.
z
After learning the introduction to a tree in data structures, you will see why you need a
tree in data structures.
struct node
{
int data;
struct node *leftchild;
struct node *rightchild;
}
Continuing with this tutorial, you will see some key terminologies of the tree in data
structures.
Tree Terminologies
Root
In a tree data structure, the root is the first node of the tree. The root node is the initial node
of the tree in data structures.
In the tree data structure, there must be only one root node.
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.
Parent
In the tree in data structures, the node that is the predecessor of any node is known as
a parent node, or a node with a branch from itself to any other successive node is called
the parent node.
Child
The node, a descendant of any node, is known as child nodes in data structures.
In a tree, any number of parent nodes can have any number of child nodes.
In a tree, every node except the root node is a child node.
Siblings
In trees in the data structure, nodes that belong to the same parent are called siblings.
Leaf
Trees in the data structure, the node with no child, is known as a leaf node.
In trees, leaf nodes are also called external nodes or terminal nodes.
Internal nodes
Trees in the data structure have at least one child node known as internal nodes.
In trees, nodes other than leaf nodes are internal nodes.
Sometimes root nodes are also called internal nodes if the tree has more than one node.
Degree
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.
Level
In tree data structures, the root node is said to be at level 0, and the root node's children
are at level 1, and the children of that node at level 1 will be level 2, and so on.
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.
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.
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.zx
Subtree
In the tree in data structures, each child from a node shapes a sub-tree recursively and
every child in the tree will form a sub-tree on its parent node.
Now you will look into the types of trees in data structures.
Types of Tree in Data Structures
Here are the different kinds of tree in data structures:
General Tree
The general tree is the type of tree where there are no constraints on the hierarchical
structure.
Properties
The general tree follows all properties of the tree data structure.
A node can have any number of nodes.
Binary Tree
A binary tree has the following properties:
Properties
Follows all properties of the tree data structure.
Binary trees can have at most two child nodes.
These two children are called the left child and the right child.
Red-Black Tree
A red-black tree is a self-balancing binary search tree, where each node has either the color
of red or black.
The colors of the nodes are used to make sure that the tree remains approximately balanced
during insertion and deletion.
Properties
Follow all properties of binary tree data structure.
Self-balancing.
Each node is either red or black.
The root and leaves nodes are black.
If the node is red, then both children are black.
Every path from a given node to any of its nodes must go through the same number of black
nodes.
Splay Tree
A splay tree is a self-balancing binary search tree.
Properties
Follows properties of binary tree data structure.
Self-balancing.
Recently accessed elements are quicker to access again.
After you perform operations such as insertion and deletion, the splay tree acts, which is
called splaying. Here it rearranges the tree so that the particular elements are placed at
the root of the tree.
Treap Tree
The Treap tree is made up of a tree, and the heap is a binary search tree.
Properties
Each node has two values: a key and a priority.
Follows a binary search tree property.
Priority of the treap tree follows the heap property.
Tree Traversal
Traversal of the tree in data structures is a process of visiting each node and prints their
value. There are three ways to traverse tree data structure.
Pre-order Traversal
In-Order Traversal
Post-order Traversal
In-Order Traversal
In the in-order traversal, the left subtree is visited first, then the root, and later the right
subtree.
Algorithm:
Step 1- Recursively traverse the left subtree
Step 2- Visit root node
Step 3- Recursively traverse right subtree
Pre-Order Traversal
In pre-order traversal, it visits the root node first, then the left subtree, and lastly right
subtree.
Algorithm:
Step 1- Visit root node
Step 2- Recursively traverse the left subtree
Step 3- Recursively traverse right subtree
Post-Order Traversal
It visits the left subtree first in post-order traversal, then the right subtree, and finally the
root node.
Algorithm:
Step 1- Recursively traverse the left subtree
Step 2- Visit root node
Step 3- Recursively traverse right subtree