0% found this document useful (0 votes)
12 views13 pages

Tree

Tree

Uploaded by

salmamoahmmed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Tree

Tree

Uploaded by

salmamoahmmed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

Binary Tree Data structure


In a normal tree, every node can have any number of children. A binary tree is a special type of tree data
structure in which every node can have a maximum of 2 children. One is known as a left child and the other is
known as right child.
A tree in which every node can have a maximum of two children is called Binary Tree. In a binary tree,
every node can have either 0 children or 1 child or 2 children but not more than 2 children.

4
There are different types of binary trees and they are...

Strictly Binary Tree


In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every node should
have exactly two children or none. That means every internal node must have exactly two children. A strictly
Binary Tree can be defined as follows...
A binary tree in which every node has either two or zero number of children is called Strictly Binary
Tree
Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree

Strictly binary tree data structure is used to represent mathematical expressions.

Example

Complete Binary Tree


A complete binary tree is a special type of binary tree where all the levels of the tree are filled completely
except the lowest level nodes which are filled from as left as possible. A complete binary tree is just like a full
binary tree, but with two major differences

1. All the leaf elements must lean towards the left.

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

Perfect Binary Tree


A perfect binary tree is a special type of binary tree in which all the leaf nodes are at the same depth, and all
non-leaf nodes have two children. In simple terms, this means that all leaf nodes are at the maximum depth of
the tree, and the tree is completely filled with no gaps. The maximum number of nodes in a perfect binary tree
is given by the formula 2^(d+1) – 1, where d is the depth of the tree. This means that a perfect binary tree with a
depth of n has 2^n leaf nodes and a total of 2^(n+1) –1 nodes.
In Perfect binary tree all the nodes must have exactly two children and at every level of complete binary tree
there must be 2level number of nodes. For example at level 2 there must be 22 = 4 nodes and at level 3 there must
be 23 = 8 nodes.
Example

Degenerate (or pathological) tree


A Tree where every internal node has one child. Such trees are performance-wise same as linked list. A
degenerate or pathological tree is a tree having a single child either left or right.

Degenerate (or pathological) tree

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.

Skewed Binary Tree

Balanced Binary Tree


A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the
height of the left and right subtree of any node differ by not more than 1. The conditions for a height-balanced
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

Consider the following binary tree...

1. Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a binary tree.
Consider the above example of a binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional array with a
maximum size of 2n + 1.

2. Linked List Representation of Binary Tree


Every node consists of three fields. First field for storing left child address, second for storing actual data and
third for storing right child address. In this linked list representation, a node has the following structure...

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)

Consider the following binary tree...

1. In - Order Traversal ( leftChild - root - rightChild )


In In-Order traversal, the root node is visited between the left child and right child. In this traversal, the
left child node is visited first, then the root node is visited and later we go for visiting the right child node.
This in-order traversal is applicable for every root node of all subtrees in the tree. This is performed
recursively for all nodes in the tree.
In the above example of a binary tree, first we try to visit left child of root node 'A', but A's left child 'B' is a root node for left
subtree. so we try to visit its (B's) left child 'D' and again D is a root for subtree with nodes D, I and J. So we try to visit its left
child 'I' and it is the leftmost child. So first we visit 'I' then go for its root node 'D' and later we visit D's right child 'J'. With
this we have completed the left part of node B. Then visit 'B' and next B's right child 'F' is visited. With this we have completed
left part of node A. Then visit root node 'A'. With this we have completed left and root parts of node A. Then we go for the
right part of the node A. In right of A again there is a subtree with root C. So go for left child of C and again it is a subtree with
root G. But G does not have left part so we visit 'G' and then visit G's right child K. With this we have completed the left part of
node C. Then visit root node 'C' and next visit C's right child 'H' which is the rightmost child in the tree. So we stop the
process.

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

2. Pre - Order Traversal ( root - leftChild - rightChild )


In Pre-Order traversal, the root node is visited before the left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order traversal
is applicable for every root node of all subtrees in the tree.

That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal.

Pre-Order Traversal for above example binary tree is

A-B-D-I-J-F-C-G-K-H

3. Post - Order Traversal ( leftChild - rightChild - root )


In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left child
node is visited first, then its right child and then its root node. This is recursively performed until the
right most node is visited.

Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-Order Traversal.

Post-Order Traversal for above example binary tree is

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.

Storing Naturally Hierarchical Data


One of the main applications of tree data structure is to store hierarchical data. A lot of real-world data falls into
this category. For instance, think about the file system on your computer. The files and folders are stored in a
hierarchical fashion with a root folder (usually denoted by /). Each subfolder can further have more subfolders
and so on. So when you want to store such data, a tree data structure is the most intuitive way to do it.

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.

Most of the content in this file is taken from http://www.btechsmartclass.com/data_structures/tree-


terminology.html

13

You might also like