0% found this document useful (0 votes)
5 views

Unit 3-DS

A tree is a non-linear data structure consisting of nodes connected by links, with a hierarchy based on a root node. There are various types of trees, including general trees, binary trees, and expression trees, each with specific properties and traversal methods. Trees are widely used for organizing hierarchical data, efficient searching, and implementing data structures like heaps and tries.

Uploaded by

rogitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 3-DS

A tree is a non-linear data structure consisting of nodes connected by links, with a hierarchy based on a root node. There are various types of trees, including general trees, binary trees, and expression trees, each with specific properties and traversal methods. Trees are widely used for organizing hierarchical data, efficient searching, and implementing data structures like heaps and tries.

Uploaded by

rogitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

TREE DATA STRUCTURE

A tree is a non-linear abstract data type with a hierarchy-based structure. It consists


of nodes (where the data is stored) that are connected via links. The tree data
structure stems from a single node called a root node and has subtrees connected to
the root.

Important Terms

Following are the important terms with respect to tree.

 Path − Path refers to the sequence of nodes along the edges of a tree.
 Root − The node at the top of the tree is called root. There is only one root
per tree and one path from the root node to any node.
 Parent − Any node except the root node has one edge upward to a node
called parent.
 Child − The node below a given node connected by its edge downward is
called its child node.
 Leaf − The node which does not have any child node is called the leaf node.
 Subtree − Subtree represents the descendants of a node.
 Visiting − Visiting refers to checking the value of a node when control is on
the node.
 Traversing − Traversing means passing through nodes in a specific order.
 Levels − Level of a node represents the generation of a node. If the root
node is at level 0, then its next child node is at level 1, its grandchild is at
level 2, and so on.
 Keys − Key represents a value of a node based on which a search operation
is to be carried out for a node.
TYPES OF TREES

There are three types of trees −

 General Trees
 Binary Trees
 Binary Search Trees

General Trees

General trees are unordered tree data structures where the root node has minimum
0 or maximum ‘n’ subtrees.

The General trees have no constraint placed on their hierarchy. The root node thus
acts like the superset of all the other subtrees.

Binary Trees

Binary Trees are general trees in which the root node can only hold up to
maximum 2 subtrees: left subtree and right subtree. Based on the number of
children, binary trees are divided into three types.

Full Binary Tree

 A full binary tree is a binary tree type where every node has either 0 or 2
child nodes.
Complete Binary Tree

 A complete binary tree is a binary tree type where all the leaf nodes must be
on the same level. However, root and internal nodes in a complete binary
tree can either have 0, 1 or 2 child nodes.

Perfect Binary Tree

 A perfect binary tree is a binary tree type where all the leaf nodes are on the
same level and every node except leaf nodes have 2 children.

TREE TRAVERSAL

Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −

 In-order Traversal

 Pre-order Traversal

 Post-order Traversal

Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.
In-order Traversal

In this traversal method, the left subtree is visited first, then the root and later the
right sub-tree. We should always remember that every node may represent a sub
tree itself.

If a binary tree is traversed in-order, the output will produce sorted key values in
an ascending order.

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

Algorithm

Until all nodes are traversed −

Step 1 − Recursively traverse left subtree.

Step 2 − Visit root node.

Step 3 − Recursively traverse right subtree.


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

Algorithm

Until all nodes are traversed −

Step 1 − Visit root node.

Step 2 − Recursively traverse left subtree.

Step 3 − Recursively traverse right subtree.

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

Algorithm

Until all nodes are traversed −

Step 1 − Recursively traverse left subtree.

Step 2 − Recursively traverse right subtree.

Step 3 − Visit root node.

Binary Tree

A binary tree is a tree data structure in which each parent node can have at most
two children. Each node of a binary tree consists of three items:

 data item

 address of left child

 address of right child


Binary Tree

Types of Binary Tree

1. Full Binary Tree

A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.

Full Binary Tree

2. Perfect Binary Tree


A perfect binary tree is a type of binary tree in which every internal node has
exactly two child nodes and all the leaf nodes are at the same level.

Perfect Binary Tree

3. Complete Binary Tree

A complete binary tree is just like a full binary tree, but with two major differences

1. Every level must be completely filled

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

3. 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.

Complete Binary Tree

4. Degenerate or Pathological Tree

A degenerate or pathological tree is the tree having a single child either left or
right.
Degenerate Binary Tree

5. 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

6. Balanced Binary Tree

It is a type of binary tree in which the difference between the height of the left and
the right subtree for each node is either 0 or 1.
Balanced Binary Tree

Binary Tree Representation

A node of a binary tree is represented by a structure containing a data part and two
pointers to other structures of the same type.

struct node

int data;

struct node *left;

struct node *right;

};

Binary Tree Representation


EXPRESSION TREE IN DATA STRUCTURE

The expression tree is a tree used to represent the various expressions. The tree
data structure is used to represent the expressional statements. In this tree, the
internal node always denotes the operators. The leaf nodes always denote the
operands.

o The operations are always performed on these operands.


o The operator present in the depth of the tree is always at the highest priority.
o The operator, which is not much at the depth in the tree, is always at the
lowest priority compared to the operators lying at the depth.
o The operand will always present at a depth of the tree; hence it is considered
the highest priority among all the operators.
o In short, we can summarize it as the value present at a depth of the tree is at
the highest priority compared with the other operators present at the top of
the tree.
o The main use of these expression trees is that it is used to evaluate,
analyze and modify the various expressions.
o It is also used to find out the associativity of each operator in the expression.
o For example, the + operator is the left-associative and / is the right-
associative.
o The dilemma of this associativity has been cleared by using the expression
trees.
o These expression trees are formed by using a context-free grammar.
o We have associated a rule in context-free grammars in front of each
grammar production.
o These rules are also known as semantic rules, and by using these semantic
rules, we can be easily able to construct the expression trees.
o It is one of the major parts of compiler design and belongs to the semantic
analysis phase.
o In this semantic analysis, we will use the syntax-directed translations, and in
the form of output, we will produce the annotated parse tree.
o An annotated parse tree is nothing but the simple parse associated with the
type attribute and each production rule.
o The main objective of using the expression trees is to make complex
expressions and can be easily be evaluated using these expression trees.
o It is immutable, and once we have created an expression tree, we can not
change it or modify it further.
o To make more modifications, it is required to construct the new expression
tree wholly.
o It is also used to solve the postfix, prefix, and infix expression evaluation.

Expression trees play a very important role in representing the language-level code
in the form of the data, which is mainly stored in the tree-like structure. It is also
used in the memory representation of the lambda expression. Using the tree data
structure, we can express the lambda expression more transparently and explicitly.
It is first created to convert the code segment onto the data segment so that the
expression can easily be evaluated.

The expression tree is a binary tree in which each external or leaf node corresponds
to the operand and each internal or parent node corresponds to the operators so for
example expression tree for 7 + ((1+8)*3) would be:

Let S be the expression tree

If S is not null, then


If S.value is an operand, then

Return S.value

x = solve(S.left)

y = solve(S.right)

Return calculate(x, y, S.value)

The following are the applications of trees:

o Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored
on the disc drive, the file and folder are in the form of the naturally
hierarchical data and stored in the form of trees.

o Organize data: It is used to organize data for efficient insertion, deletion


and searching. For example, a binary tree has a logN time for searching an
element.

o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast
and efficient way for dynamic spell checking.

o Heap: It is also a tree data structure implemented using arrays. It is used to


implement priority queues.

o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.

o Routing table: The tree data structure is also used to store the data in
routing tables in the routers.

You might also like