0% found this document useful (0 votes)
100 views31 pages

CH # 6 (Trees)

The document discusses different types of tree data structures including their terminology and properties. It covers binary trees and defines various tree concepts like root, child, parent and leaf nodes. It also explains different tree traversal techniques.

Uploaded by

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

CH # 6 (Trees)

The document discusses different types of tree data structures including their terminology and properties. It covers binary trees and defines various tree concepts like root, child, parent and leaf nodes. It also explains different tree traversal techniques.

Uploaded by

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

Data Structures & Algorithms

Chapter # 6
The Trees

By:
Muhammad Imran
Assistant Prof. of Computer Science
Govt. Millat Degree College Mumtazabad, Multan

Lecturer (Visiting Faculty) of I.T


University of Education (Multan Campus) Multan
Ch # 6 (The Trees)
2

Trees
A data structure is said to be linear if its elements form a sequence or a linear list. Previous linear
data structures that we have studied like an array, stacks, queues and linked lists organize data in
linear order. A data structure is said to be non linear if its elements form a hierarchical
classification where, data items appear at various levels.

Trees and Graphs are widely used non-linear data structures. Tree and graph structures represents
hierarchial relationship between individual data elements. Graphs are nothing but trees with
certain restrictions removed.

In linear data structure, data is organized in sequential order and in non-linear data structure, data
is organized in random order. Tree is a very popular data structure used in wide range of
applications. A tree data structure can be defined as follows...

Tree is a non-linear data structure which organizes data in hierarchical structure and this
is a recursive definition.

A tree data structure can also be defined as follows...

Tree data structure is a collection of data (Node) which is organized in hierarchical


structure and this is a recursive definition

In tree data structure, 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.

A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the
top of the hierarchy. Each node can have at most one link coming into it.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
3

In a tree data structure, if we have N number of nodes then we can have a maximum
of N-1 number of links.

Example

Terminologies in Trees
In a tree data structure, we use the following terminology...

1. Root

In a tree data structure, the first node is called as Root Node. Every tree must have root node.
We can say that root node is the origin of tree data structure. In any tree, there must be only one
root node. We never have multiple root nodes in a tree. Here ‘A’ is the root node.

2. Edge

In a tree data structure, 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.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
4

3. Parent Node

In a tree data structure, the node which is predecessor of any node is called as PARENT NODE.
In simple words, the node which has branch from it to any other node is called as parent node.
Parent node can also be defined as "The node which has child / children".

In above figure A,B,C,E and G are parent nodes.

4. Child

In a tree data structure, 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. In the above figure B and C are children of A. similarly G and H are children of C.

5. Siblings Nodes

In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes. In the above example B & C are
siblings. D,E,F are siblings. G and H are siblings. I and J are siblings.

6. Leaf Nodes

In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.

In a tree data structure, 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. In the above tree
D ,I ,J ,F ,K and H are Leaf Nodes.

7. Internal Nodes

In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In
simple words, an internal node is a node with atleast one child.
In a tree data structure, 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. In the above tree A, B, C, E and G are Internal Nodes.

8. External Nodes

The leaf / terminal nodes are called as External nodes. In the above tree D,I,J,F,K and H are
External Nodes.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
5

9. Ancestor and Descendent

If there is a path from node A to node B, then A is called an ancestor of B and


B is called a descendent of A. In above tree E is the ancestor of I and J and vice versa. Similarly
G is the descendent of K.

10. Degree

In a tree data structure, 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'. In the above tree
degree of B is 3, Degree of A is 2, and Degree of F is 0.

11. Level

In a tree data structure, 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).

12. Height of a tree

In a tree data structure, the total number of egdes 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 '0'.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
6

13. Depth of a Tree

In a tree data structure, the total number of egdes 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 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'.

14. Path

In a tree data structure, 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.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
7

15. Sub Tree

In a tree data structure, each child from a node forms a subtree recursively. Every child node will
form a subtree on its parent node.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
8

Binary Tree
In a normal tree, every node can have any number of children. 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 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 as 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.

Example

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

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

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
9

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

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
10

2. Complete 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 and in complete 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 2 2 = 4 nodes and at level 3 there must
be 23 = 8 nodes.

A binary tree in which every internal node has exactly two children and all leaf nodes are
at same level is called Complete Binary Tree. Complete binary tree is also called as Perfect
Binary Tree.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
11

3. Extended Binary Tree / Full Binary Tree


A binary tree can be converted into Full Binary tree by adding dummy nodes to existing nodes
wherever required.

The full binary tree obtained by adding dummy nodes to a binary tree is called as
Extended Binary Tree.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
12

In above figure, a normal binary tree is converted into full binary tree by adding dummy nodes in
shape of rectangle.

Internal and external nodes:

We define two terms: Internal nodes and external nodes. An internal node is a tree
node having at least one–key and possibly some children. It is some times convenient to have
another types of nodes, called an external node, and pretend that all null child links point to such
a node. An external node doesn’t exist, but serves as a conceptual place holder for nodes to be
inserted. We draw internal nodes using circles, with letters as labels. External nodes are denoted
by squares. The square node version is sometimes called an extended binary tree. A binary tree
with n internal nodes has n+1 external nodes. Following Figure shows a sample
tree illustrating both internal and external nodes.

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

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
13

1. Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree.

Consider the above example of 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 - 1.

2. Linked List Representation


We use double linked list to represent a binary tree. In a double linked list, 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.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
14

In this linked list representation, a node has the following structure...

The above example of binary tree represented using Linked list representation is shown as
follows...

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
15

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.

There are three types of binary tree traversals.

1. In - Order Traversal

2. Pre - Order Traversal

3. Post - Order Traversal

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

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 subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order. The steps for traversing a binary tree in inorder traversal are:

1. Visit the left subtree, using inorder.


2. Visit the root.
3. Visit the right subtree, using inorder.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
16

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 inorder
traversal of this tree will be −
D→B→E→A→F→C→G

Example 2: What will be the traversing order if we use in-order traversal algorithm

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
17

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 this traversal method, the root node is visited first, then the left subtree and finally the
right subtree. The steps for traversing a binary tree in preorder traversal are:

1. Visit the root.


2. Visit the left subtree, using preorder.
3. Visit the right subtree, using preorder.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
18

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

Example 2: What will be the traversing order if we use pre-order traversal algorithm

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
19

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 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. The steps for traversing a binary tree in postorder
traversal are:

1. Visit the left subtree, using postorder.


2. Visit the right subtree, using postorder
3. Visit the root.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
20

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

Example 2: What will be the traversing order if we use post-order traversal algorithm

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
21

Post-Order Traversal for above example binary tree is

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

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
22

Expression Trees:
Expression tree is a binary tree, because all of the operations are binary. It is also possible for a
node to have only one child, as is the case with the unary minus operator. The leaves of an
expression tree are operands, such as constants or variable names, and the other (non leaf) nodes
contain operators. Once an expression tree is constructed we can traverse it in three ways:

• Inorder Traversal
• Preorder Traversal
• Postorder Traversal

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
23

Following Figure shows some more expression trees that represent arithmetic expressions given
in infix form.

An expression tree can be generated for the infix and postfix expressions.
An algorithm to convert a postfix expression into an expression tree is as follows:

1. Read the expression one symbol at a time.


2. If the symbol is an operand, we create a one-node tree and push a pointer to it onto a stack.
3. If the symbol is an operator, we pop pointers to two trees T1 and T2 from the stack (T1 is
popped first) and form a new tree whose root is the operator and whose left and right children
point to T2 and T1 respectively. A pointer to this new tree is then pushed onto the stack.

Example 1:
Construct an expression tree for the postfix expression: a b + c d e + * *
Solution:
The first two symbols are operands, so we create one-node trees and push pointers to
them onto a stack.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
24

Next, a ‘+’ is read, so two pointers to trees are popped, a new tree is formed, and a pointer to it is
pushed onto the stack.

Next, c, d, and e are read, and for each one–node tree is created and a pointer to the
corresponding tree is pushed onto the stack.

Now a ‘+’ is read, so two trees are merged.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
25

Continuing, a ‘*’ is read, so we pop two tree pointers and form a new tree with a ‘*’ as root.

Finally, the last symbol is read, two trees are merged, and a pointer to the final tree is left on the
stack.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
26

For the above tree:


Inorder form of the expression: a + b * c * d + e
Preorder form of the expression: * + a b * c + d e
Postorder form of the expression: a b + c d e + * *

Do yourself.

Binary Search Tree:


A binary search tree is a binary tree. It may be empty. If it is not empty then it satisfies the
following properties:
1. Every element has a key and no two elements have the same key.
2. The keys in the left subtree are smaller than the key in the root.
3. The keys in the right subtree are larger than the key in the root.
4. The left and right subtrees are also binary search trees.
Figure (a) is a binary search tree, whereas figure (b) is not a binary search
tree.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
27

Basic Operations

Following are the basic operations of a tree –

Search − Searches an element in a tree.


Insert − Inserts an element in a tree.
Delete:
Pre-order Traversal − Traverses a tree in a pre-order manner.
In-order Traversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.

Example

Construct a Binary Search Tree by inserting the following sequence of numbers...

10,12,5,4,20,8,7,15 and 13

Above elements are inserted into a Binary Search Tree as follows...

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
28

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
29

Example 2:
Construct a Binary Search Tree by inserting the following sequence of numbers...

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

Insertion in a BST
Value in a BST can be inserted by first locating the proper place of the particular value.
For example if we want to insert 15 in the above BST. It will become the left child of 18.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
30

Deletion in a BST

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 6 (The Trees)
31

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.

You might also like