Tree
Tree
In linear data structure data is organized in sequential order and in non-linear data structure data is organized in
random order. A tree is a very popular non-linear data structure used in a wide range of applications.
Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive
definition. Also, it is a collection of data (Node) which is organized in hierarchical structure recursively
In tree, every individual element is called as Node. Node in a tree data structure stores the actual data of that
particular element and link to next element in hierarchical structure. In a tree data structure, if we
have N number of nodes then we can have a maximum of N-1 number of links.
Terminology
Root: The first node is called as Root Node. Every tree must have a root node. We can say that the root node
is the origin of the tree data structure. In any tree, there must be only one root node.
2. Edge: The connecting link between any two nodes is called as EDGE. In a tree with 'N' number of nodes
there will be a maximum of 'N-1' number of edges.
3. Parent: The node which is a predecessor of any node is called as PARENT NODE. In simple words, the
node which has a branch from it to any other node is called a parent node. Parent node can also be defined as
"The node which has child / children".
1
4. Child: The node which is descendant of any node is called as CHILD Node. In simple words, the node
which has a link from its parent node is called as child node. In a tree, any parent node can have any number of
child nodes. In a tree, all the nodes except root are child nodes.
5. Siblings: the nodes with the same Parent are called as SIBLINGS.
6. Leaf: the node which does not have a child is called as LEAF Node. In simple words, a leaf is a node with
no child. The leaf nodes are also called as External Nodes. External node is also a node with no child. In a
tree, leaf node is also called as 'Terminal' node.
2
7. Internal Nodes: the node which has at least one child is called as INTERNAL Node. In simple words,
an internal node is a node with at least one child. Nodes other than leaf nodes are called as Internal Nodes. The
root node is also said to be Internal Node if the tree has more than one node. Internal nodes are also called as
'Non-Terminal' nodes.
8. Degree: The total number of children of a node is called as DEGREE of that Node. In simple words, the
Degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree is
called as 'Degree of Tree'.
9. Level: the root node is said to be at Level 0 and the children of root node are at Level 1 and the children
of the nodes which are at Level 1 will be at Level 2 and so on. In simple words, in a tree each step from top to
bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step).
10. Height: The total number of edges plus one from leaf node to a particular node in the longest path is
called as HEIGHT of that Node. In a tree, height of the root node is said to be height of the tree. In a
tree, height of all leaf nodes is '1'.
11. Depth: the total number of edges from root node to a particular node is called as DEPTH of that
Node. In a tree, the total number of edges from root node to a leaf node in the longest path is said to be Depth
3
of the tree. In simple words, the highest depth of any leaf node in a tree is said to be depth of that tree. In a
tree, depth of the root node is '0'.
12. Path: The sequence of Nodes and Edges from one node to another node is called as PATH between that
two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J
has length 4.
13. Sub Tree: Each child from a node forms a subtree recursively. Every child node will form a subtree on
its parent node.
4
There are different types of binary trees and they are...
Example
2. The last leaf element might not have a right sibling i.e. a complete binary tree doesn't have to be a full
binary tree.
5
Properties of Complete Binary Tree:
• A complete binary tree is said to be a proper binary tree where all leaves have the same depth.
• In a complete binary tree number of nodes at depth d is 2^d.
• In a complete binary tree with n nodes height of the tree is log(n+1).
• All the levels except the last level are completely full
6
Skewed Binary Tree
A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated by the left nodes or
the right nodes. Thus, there are two types of skewed binary tree: left-skewed binary tree and right-skewed
binary tree.
1. difference between the left and the right subtree for any node is not more than one
2. the left subtree is balanced
3. the right subtree is balanced
In a height-balanced tree, the absolute difference between the height of the left and right subtree for every node
is 0 or 1.
Example: The first and third trees are height balanced binary trees. Whereas the second and last trees is not a
height balanced tree. Because the difference between the left and right is 2.
7
Binary Tree Representations
A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
2. Linked List Representation
To represent a binary tree of depth 'n' using array representation, we need one dimensional array with a
maximum size of 2n + 1.
The above example of the binary tree represented using Linked list representation is shown as follows...
8
Binary Tree Traversals
When we wanted to display a binary tree, we need to follow some order in which all the nodes of that binary
tree must be displayed. In any binary tree, displaying order of nodes depends on the traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
The traversal of binary tree follows two techniques: Depth-First Search (DFS) and Breadth-First Search
(BFS). Depth-first search is a technique used for traversing trees or graphs. Here backtracking is used for
traversal. In this traversal first, the deepest node is visited and then backtracks to its parent node if no sibling
of that node exists. While, the breadth-first search algorithm is used to search a tree or graph data structure
for a node that meets a set of criteria. It starts at the tree’s root or graph and searches/visits all nodes at the
current depth level before moving on to the nodes at the next depth level.
Binary tree traversal:
• Breadth First Traversal (Or Level Order Traversal)
• Depth First Traversals
• Inorder Traversal (Left-Root-Right)
• Preorder Traversal (Root-Left-Right)
• Postorder Traversal (Left-Right-Root)
That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-Order Traversal.
9
In-Order Traversal for above example of binary tree is
I-D-J-B-F-A-G-K-C-H
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal.
A-B-D-I-J-F-C-G-K-H
I-J-D-F-B-K-G-H-C-A
Level-Order traversal
In Level-Order traversal, print its nodes level by level, i.e., print all nodes of level 1 first, followed by
nodes of level 2 and so on… Print nodes for any level from left to right.
For example, the level order traversal for the following tree is 1, 2, 3, 4, 5, 6, 7.
10
Applications
In computer science, the tree data structure can be used to store hierarchical data. A tree traversal is a process of
visiting each node in a tree. This can be done in different ways like pre-order, post-order, or in-order. Trees are also
used to store data that naturally have hierarchical relationships, like the file system on our computers. Besides that,
trees are also used in several applications like heaps, tries, and suffix trees.
Also, different applications use binary tree and different variants of binary trees such as tries, binary search trees, and
B-trees. In computing, binary trees are mainly used for searching and sorting as they provide a means to store data
hierarchically. Some common operations that can be conducted on binary trees include insertion, deletion, and
traversal.
Organize Data
Trees can also be used as an organizational tool. For instance, a family tree is one such example where family
relationships are represented using a tree-like structure. Similarly, trees can also be used to represent
geographical features like states and cities in the USA or Countries and Continents in the world etc.
Routing Tables
A routing table is used to link routers in a network. It is usually implemented with a trie data structure, which is
a variation of a binary tree. The tree data structure will store the location of routers based on their IP addresses.
Routers with similar addresses are grouped under a single subtree. To find a router to which a packet must be
forwarded, we need to traverse the tree using the prefix of the network address to which a packet must be sent.
Afterward, the packet is forwarded to the router with the longest matching prefix of the destination address.
Decision Trees
Binary trees can also be used for classification purposes. A decision tree is a supervised machine learning
algorithm. The binary tree data structure is used here to emulate the decision-making process. A decision tree
usually begins with a root node. The internal nodes are conditions or dataset features. Branches are decision
rules while the leaves nodes are the outcomes of the decision. For example, suppose we want to classify apples.
The decision tree for this problem will be as follows:
11
Expression Evaluation
Binary trees can be used in expression evaluation. An expression tree, also known as a syntax tree, is a tree data
structure used to represent mathematical expressions or programming language statements. It is commonly used
in compilers and interpreters for programming languages, as they provide a way to parse and evaluate code. In
an expression tree, each node represents either an operator or an operand. The operators are stored at the interior
node and the operands are stored at the leaf node. and the leaves of the tree represent the actual values or
variables in the expression.
Sorting
Binary search trees, a variant of binary trees are used in the implementation of sorting algorithms to order items.
A binary search tree is simply an ordered or sorted binary tree such that the value in the left child is less than the
value in the parent node. At the same time, the values in the right node are greater than the value in the parent
node. To complete a sorting procedure, the items to be sorted are first inserted into a binary search tree. To
retrieve the sorted items, the tree is traversed using in-order traversal.
12
Indices for Databases
In database indexing, B-trees are used to sort data for simplified searching, insertion, and deletion. It is
important to note that a B-tree is not a binary tree, but can become one when it takes on the properties of a
binary tree. The database creates indices for each given record in the database. The B-tree then stores in its
internal nodes, references to data records with the actual data records in its leaf nodes. This provides sequential
access to data in the databases.
Data Compression
In data compression, Huffman coding is used to create a binary tree capable of compressing data. Data
compression is the processing of encoding data to use fewer bits. Given a text to compress, Huffman coding
builds a binary tree and inserts the encodings of characters in the nodes based on their frequency in the text. The
encoding for a character is obtained by traversing the tree from its root to the node. Frequently occurring
characters will have a shorter path as compared to less occurring characters. This is done to reduce the number
of bits for frequent characters and ensure maximum data compression.
13