You are on page 1of 13

Abstract

Trees start with a singular root node. This root will point to multiple other nodes, and no node will

ever point at the root. This makes a kind of parent and child hierarchy, where a parent can have

multiple children, and children always have only one parent. Those child nodes could then have

children of their own, and those grand-children could have children, and so on. Each node that has

a child is called a branch node, and nodes that have no children are leaf nodes.

What’s important and unique about trees is that there is only one way to reach each node. To reach

11, you have to go through 1, 2, and 5. To reach 7, you have to go through 1 and 3. Having only one

parent per node and only one path per node means trees are good at detailing things like hierarchies

or a series of dependent events and decisions. A every day example might be something like a family

tree or a simple flowchart, and a technical example would be something like a Document Object

Model (DOM).
Introduction

 Introduction to Trees

Before knowing about the tree , we should know the linear and non-linear data structures. Linear
data structure is a structure in which all the elements are stored sequentially and have only single
level. In contrast, a non-linear data structure is a structure that follows a hierarchy, i.e., elements
are arranged in multiple levels.

Let's understand the structure that forms the hierarchy.

In the above figure, we can assume the company hierarchy where A represents the CEO of the

company, B, C and D represent the managers of the company, E and F represent the team

leaders, and G and H represent the team members. This type of structure has more than one level,

so it is known as a non-linear data structure


Theoretical background
 What is Tree?

A tree is a non-linear data structure that represents the hierarchy. A tree is a collection of nodes
that are linked together to form a hierarchy

et's look at some terminologies used in a tree data structure.

 What is Tree in Data Structure?

A tree data structure is a collection of nodes connected by edges. Each node contains a value
or data which may or may not have a child node. The first node of the tree is called the root. If
this root node is connected with another node, then this root is called the parent node, and the
node connected to it is the child node.

 Terminology

o Root node: The topmost node in a tree data structure is known as a root node. A root
node is a node that does not have any parent.
o Parent of a node: The immediate predecessor of a node is known as a parent of a node.
Here predecessor means the previous node of that particular node.
o Child of a node: The immediate successor of a node is known as a child of a node.
o Leaf node: The leaf node is a node that does not have any child node. It is also known as
an external node.
o Non-leaf node: The non-leaf node is a node that has atleast one child node. It is also
known as an internal node.
o Path: It is a sequence of the consecutive edges from a source node to the destination
node. Here edge is a link between two nodes.
o Ancestor: The predecessor nodes that occur in the path from the root to that node is
known as an ancestor.
o Descendant: The successor nodes that exist in the path from that node to the leaf node.
o Sibling: All the children that have the same parent node are known as siblings.
o Degree: The number of children of a particular node is known as a degree.
o Depth of node: The length of the path from the root to that node is known as a depth of a
node.
o Height of a node: The number of edges that occur in the longest path from that node to
the leaf node is known as the height of a node.
o Level of node: The number of edges that exist from the root node to the given node is
known as a level of a node.

Example from
Terminology Description
Diagram

‘1’, ‘2’, ‘3’ are the


Node Each vertex of a tree is a node.
node in tree.

Node ‘1’ is the


Root Topmost node of a tree
topmost root node.

Node ‘2’ is the parent


The node having an edge sharing to a child
Parent Node of ‘5’ and ‘6’, Node
node
‘3’ is the parent of ‘7’.

The sub-node of a parent node is the child ‘5’ and ‘6’ is the
Child Node
node children of ‘2’.
The last node which does have any sub ‘5’, ‘6’, ‘9’ and ‘8’
Leaf
node is the leaf node are leaf node.

Link between ‘1’ and


Edge Connecting link between two nodes ‘2’, ‘2’ and ‘5’, ‘2’
and ‘6’ are edges

‘5’ and ‘6’ are


Siblings Nodes with same parent are siblings siblings with ‘2’ as
their parent.

Height of a tree is the length of longest Height of ‘1’ is 3.


Height path from root to leaf node. It is calculated (longest path is 1-3-7-
with total number of edges 9)

Number of edges from the root node to


Depth of root node ‘1’
Depth that node is called the Depth of that node.
is height of ‘1’ – 1 = 2
Depth of a tree = Height of tree – 1

Each step from top to bottom is called a ‘1’ or root node is at


Level. If the root node is at level 0, then its level 0, ‘2’, ‘3’, and
Level
next child node is at level 1, its grandchild ‘4’ is at level 1, and
is at level 2, and so on so on.

Node ‘2’, ‘5’, and ‘6’


Sub-Tree Descendants of a node represent subtree.
represent a sub-tree.

Degree of ‘2’ is 2 (‘5’


Degree of a node represents the total
Degree of Node and ‘6’). Degree of
number of children in it.
‘4’ is 1.

How is a tree represented in the memory?

Each node will contain three parts, data part, address of the left subtree, and address of the right
subtree. If any node does not have the child, then both link parts will have NULL values.
A General Tree
A general tree is a tree where each node may have zero or more children (a binary tree is a
specialized case of a general tree). General trees are used to model applications such as file
systems. Any type of tree can be a general tree
 Binary Tree

Binary tree is a tree data structure in which each node can have 0, 1, or 2 children – left and right
child

Properties of a Binary tree

 Maximum number of nodes at any level ‘L’ in a binary tree is 2


 Minimum number of nodes in a binary tree of height H is H + 1
 Maximum number of nodes in a binary tree of height H is 2H+1 – 1
 Total Number of leaf nodes in a Binary Tree = Total Number of nodes with 2 children + 1
 The maximum number of nodes at each level of i is 2i.
 Searching operation takes O(log2N)

Binary trees can be divided into the following types:

 Perfect binary tree: Every internal node has two child nodes. All the leaf nodes are at the
same level.
 Full binary tree: Every parent node or an internal node has either exactly two children or no
child nodes.
 Complete binary tree: All levels except the last one are full with nodes.
 Degenerate binary tree: All the internal nodes have only one child.
 Balanced binary tree: The left and right trees differ by either 0 or 1.
Applications of Binary trees

 Decision Tree – Machine learning Algorithm

A supervised learning algorithm which is used both in case of a classification or regression based
problem. It starts with a root node and ends with a decision or prediction made by leaves. All the
nodes in the tree represent a condition on the basis of which the split occurs.

 Working with Morse Code

The organization of morse code is done in the form of a binary tree


 Binary Expression Trees

A binary tree used to evaluate an expression. The operators are stored at the interior node and the
operands are stored at the leaf node.
 Binary Search Tree (BST)

A binary search tree (BST) is also called an ordered or sorted binary tree in which the value at the
left sub tree is lesser than that of root and right subtree has value greater than that of root.

Every binary search tree is a binary tree. However, not every binary tree is a binary search tree.
What’s the difference between a binary tree and binary search tree? The most important difference
between the two is that in a BST, the left child node’s value must be less than the parent while the
right child node’s value must be higher.

 Properties of a Binary Search tree

 Each node has a maximum of up to two children


 The value of all the nodes in the left sub-tree is less than the value of the root
 Value of all the nodes in the right subtree is greater than or equal to the value of the root
 This rule is recursively valid for all the left and right subtrees of the root

 Applications of a Binary Search tree

 Used to efficiently store data in sorted form in order to access and search stored elements quickly.
 Given ‘A’ a sorted array, find out how many times x occurs in ‘A’.
 Player ‘A’ chooses a secret number ‘n’. Player ‘B’ can guess a number x and A replies how x
 compares to n (equal, larger, smaller). What’s an efficient strategy for B to guess ‘n’?
Conclusion
 A tree data structure is defined as a collection of objects or entities known as nodes that
are linked together to represent a hierarchy.

 A tree is recursive in nature as it is a recursive data structure. It is so because each tree


contains multiple subtrees, as each node in a tree is a root node to another tree that makes
it a subtree.

A tree has multiple applications like:

 Traversing: that allows us or a program to traverse through an object model


like DocumentObjectModel.

 Searching: in a structure like a binary search tree, it's easier to search for an element as
smaller elements are located in the left subtree and greater elements are located in
the right subtree.

 Decision Making: A flow of an application can be represented in a hierarchical structure


that allows us to understand all the decisions that a user can make.

 Machine Learning: Machine learning allows us to make automatic decision making


framework that was made possible with tree data structure.

 There are multiple types of trees but the most common of all is the Binary Search Tree.
Reference
1. https://www.scaler.com/topics/data-structures/tree-data-structure/
2. https://www.naukri.com/learning/articles/tree-data-structures-types-properties-
and-applications/
3. https://www.upgrad.com/blog/types-of-trees-in-data-structure/
4. https://www.javatpoint.com/tree-vs-graph-data-structure

You might also like