You are on page 1of 88

Non-Linear Data Structures - I

C E – S E – D ATA S T R U C T U R E S
DR. DEEPTI REDDY
A S S O C I AT E P R O F E S S O R
D E P T. O F C O M P U T E R E N G I N E E R I N G ,

1
Unit 4- Non-Linear Data
Structures - I
Introduction,
Binary tree terminologies,
Representation of Binary trees in memory,
Binary Tree traversal algorithms,
Construction of Binary Tree from traversals,
Binary Search Tree: Insertion, Deletion,
Applications of tree data structure: Expression trees, Huffman trees.

2
Tree Data Structure
The trees is a non-linear data structure.
Tree stores elements hierarchically.
The top node of a tree is the root node and each node, except the root,
has a parent.
A node in a general tree (except the leaf nodes) may have zero or more
sub-trees
Root node

Tree
3
Trees- Terminology
Root node- Top node
Sub-trees- the tree below root node

4
Trees- Terminology
Level number-root node is at level 0, children of the root node are at level
number 1.

5
Trees- Terminology
Parent If N is any node in T that has left successor S1 and right successor S2, then N is
called the parent of S1 and S2. Correspondingly, S1 and S2 are called the left child and
the right child of N. Every node other than the root node has a parent.
In figure shown, node 2 is parent node of 4 and 5.

6
Trees- Terminology
Degree of a node It is equal to the number of children that a node has.
The degree of a leaf node is zero.
For example, in the tree, degree of node 4 is 2, degree of node 5 is zero and degree of
node 7 is 1.

7
Trees- Terminology
Sibling All nodes that are at the same level and share the same parent are called
siblings (brothers).
For example, nodes 2 and 3; nodes 4 and 5; nodes 6 and 7; nodes 8 and 9; and nodes
10 and 11 are siblings.

8
Trees- Terminology
Leaf node A node that has no children is called a leaf node or a terminal node.
The leaf nodes in the tree are: 8, 9, 5, 10, 11, and 12.

9
Trees- Terminology
Edge It is the line connecting a node N to any of its successors.
A binary tree of n nodes has exactly n – 1 edges because every node except the root
node is connected to its parent via an edge

10
Trees- Terminology
Path: A sequence of consecutive edges.
For example, in Fig. the path from the root node to the node 8 is given as: 1, 2, 4, and
8.

11
Trees- Terminology
Depth The depth of a node N is given as the length of the path from the root R to the
node N. The depth of the root node is zero.

12
Trees- Terminology
Height of a tree It is the total number of nodes on the path from the root node to the
deepest node in the tree. A tree with only a root node has a height of 1.
Height of the given tree is: 4

13
Trees- Terminology
Ancestor node- an ancestor of a node is any predecessor
node on the path from root to that node.
Descendant node A descendant node is any successor
node on any path from the node to a leaf node.

14
Reflection
Specify

10 1. Root node
2. Leaf node
5 30 3. Ancestor node od 25
4. Descendant node of 30
2 25 45 5. Level number of 10
6. Level number of 25

15
Reflection
Consider the tree given below.
Now, do the following:
(a) Name the leaf nodes
(b) Name the non-leaf nodes
(c) Name the ancestors of E
(d) Name the descendants of A
(e) Name the siblings of C
(f) Find the height of the tree
(g) Find the height of sub-tree rooted at E
(h) Find the level of node E

16
Binary Tree Data Structure
Binary tree
◦ Tree with 0, 1 or atmost 2 children per node.
◦ A node that has zero children is called a leaf node or a terminal node.
◦ Every node contains a data element, a left pointer which points to the left
child, and a right pointer which points to the right child.
◦ The root element is pointed by a 'root' pointer. If root = NULL, then it means
the tree is empty

Binary Tree
17
Binary tree- Node
representation
struct node {
struct node *left;
int data;
struct node *right;
};

18
Representation of Binary
Trees in the Memory

19
Representation of Binary
Trees in the Memory
Given the memory representation of a tree that stores the names of
family members, construct the corresponding tree from the given data.

20
Representation of Binary
Trees in the Memory
Draw the binary tree having the following memory representation:

21
TRAVERSING A BINARY
TREE
Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
Unlike linear data structures in which the elements are traversed
sequentially, tree is a nonlinear data structure in which the elements
can be traversed in many different ways.
There are different algorithms for tree traversals:
◦ Inorder traversal
◦ Postorder traversal
◦ Preorder traversal

22
Pre-order traversal
To traverse a non-empty binary tree in pre-order, the following operations are
performed recursively at each node. The algorithm works by:
1. Visiting the root node,
2. Traversing the left sub-tree, and finally
3. Traversing the right sub-tree.

The pre-order traversal of the tree is given as A, B, C.

NLR traversal algorithm (Node-Left-Right)

23
Algorithm for pre-order
traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE-> DATA
Step 3: PREORDER(TREE-> LEFT)
Step 4: PREORDER(TREE-> RIGHT)
[END OF LOOP]
Step 5: END

24
Pre-order Traversal
10

5 30

2 25 45

25
Pre-order Traversal
Perform Pre-order traversal

26
Example: Pre-order traversal

TRAVERSAL ORDER: A, B, D, G, H, L, E, C, F, I, J,and K


TRAVERSAL ORDER: A, B, D, C, E, F, G, H, and I

27
Pre-order Function in C++
struct node
{ int data;
struct node *left;
struct node *right;
};
struct Node *root = NULL;

void preorder(struct node *root)


{ if (root != NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}

28
Pre-order Function in C++
1. void preorder(struct node *root)
2. { if (root != NULL)
3. {
4. cout<<root->data<<" ";
5. preorder(root->left);
6. preorder(root->right);
7. }
8. }

29
In-order traversal
To traverse a non-empty binary tree in pre-order, the following operations are
performed recursively at each node. The algorithm works by:
1. Traversing the left sub-tree,
2. Visiting the root node,
3. Traversing the right sub-tree.

The in-order traversal of the tree is given as B, A, C.

LNR traversal algorithm (Left-Node-Right)

30
In-order Traversal
10

5 30

2 25 45

31
In-order Traversal
Perform In-order traversal

32
Algorithm for In-order
traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: INORDER(TREE-> LEFT)
Step 3: Write TREE-> DATA
Step 4: INORDER(TREE-> RIGHT)
[END OF LOOP]
Step 5: END

33
In-order Function in C++
void inorder(struct node *root)
{ if (root != NULL)
{
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}

34
Example: In-order traversal

TRAVERSAL ORDER: G, D, H, L, B, E, A, C, I, F, K, and J


TRAVERSAL ORDER: B, D, A, E, H, G, I, F, and C

35
Post-order traversal
To traverse a non-empty binary tree in pre-order, the following operations are
performed recursively at each node. The algorithm works by:
1. Traversing the left sub-tree,
2. Traversing the right sub-tree.
3. Visiting the root node,

The in-order traversal of the tree is given as B, C,A.

LRN traversal algorithm (Left-Right-Node)

36
Algorithm for post-order
traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: POSTORDER(TREE-> LEFT)
Step 3: POSTORDER(TREE-> RIGHT)
Step 4: Write TREE-> DATA
[END OF LOOP]
Step 5: END

37
Post-order Traversal
10

5 30

2 25 45

38
Post-order Traversal
Perform Post-order traversal

39
Post-order Function in C++
void postorder(struct node *root)
{ if (root != NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}

40
Example: Post-order traversal

TRAVERSAL ORDER: G, L, H, D, E, B, I, K, J, F, C, and A


TRAVERSAL ORDER: D, B, H, I, G, F, E, C, and A

41
Perform in-order, pre-order
and post-order traversal

42
Construct Binary tree from
traversal
Construct Binary Tree from given Inorder and Preorder traversals
Let us consider the below traversals:
•Inorder sequence: D B E A F C
•Preorder sequence: A B D E C F

43
Construct Binary tree from
traversal
Construct Binary Tree from given Inorder and Preorder traversals
Let us consider the below traversals:
•Inorder sequence: 40 20 50 10 60 30
•Preorder sequence: 10 20 40 50 30 60

44
Construct Binary tree from
traversal
Construct Binary Tree from given Inorder and Postorder traversals
Let us consider the below traversals:
•Inorder sequence: 4, 8, 2, 5, 1, 6, 3, 7
•Postorder sequence: 8, 4, 5, 2, 6, 7, 3, 1

45
Construct Binary tree from
traversal
Construct Binary Tree from given Inorder and Postorder traversals
Let us consider the below traversals:
•Inorder sequence: 4, 2, 1, 7, 5, 8, 3, 6
•Postorder sequence: 4, 2, 7, 8, 5, 6, 3, 1

46
Binary Search Tree
each node has 0, 1, or at the most 2 children
Key property
◦ Value at node
◦ Smaller values in left subtree
◦ Larger values in right subtree

2 45

49
Binary Search tree- Example
Identify valid binary search tree.
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10

25
a. c.
b.
50
Insert operation
Insert 8, 5, 1, 19, 10, 11, 2

51
Insert operation
Insert 8, 3, 10, 1, 6, 14, 4, 7, 13

52
Example- Insert

53
Example- Insert

54
Example- Insert

55
Example- Insert

56
Example- Insert

57
f

Insert operation (Recursive)


struct node *insert(struct node *root, int val)
{
if(root == NULL)
return getNewNode(val);
if(root->key < val)
root->right = insert(root->right,val);
else if(root->key > val)
root->left = insert(root->left,val);

return root;

}
struct node *getNewNode(int val)
{
struct node *newNode = new node;
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

58
Activity
Insert new node 13

59
Insert operation (iterative)
struct node *insertElement(struct node *tree, int val) while(nodeptr!=NULL)
{ {
struct node *ptr, *nodeptr, *parentptr; parentptr=nodeptr;
ptr = new node; if((val< nodeptr -> data)
ptr–>data = val; nodeptr=nodeptr–>left;
ptr–>left = NULL; else
ptr–>right = NULL; nodeptr = nodeptr–>right;
if(tree==NULL) }
{ if(val< parentptr -> data)
tree=ptr; parentptr–>left = ptr;
tree–>left=NULL; else
tree–>right=NULL; parentptr–>right = ptr;
} }
else return tree;
{ }
parentptr=NULL;
nodeptr=tree;

60
Reflection
1. The number of edges from the root to the node is called __________
of the tree.
a) Height
b) Depth
c) Length
d) Width
2. The number of edges from the node to the deepest leaf is called
_________ of the tree.
a) Height
b) Depth
c) Length
d) Width

61
Reflection
Construct a binary tree by using postorder and inorder
sequences given below.
Inorder: N, M, P, O, Q
Postorder: N, P, Q, O, M

a b c d

62
Reflection
Construct a binary search tree by using postorder sequence
given below.
Postorder: 2, 4, 3, 7, 9, 8, 5.

a b c d

63
Reflection
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order
into an initially empty binary search tree. The binary search tree uses the
usual ordering on natural numbers. What is the in-order, post-order and
pre-order traversal sequence of the resultant tree?

64
Delete operation
The delete function deletes a node from the binary search tree.
However, utmost care should be taken that the properties of the binary search
tree are not violated and nodes are not lost in the process.

65
Delete
Three cases:
(1) the node is a leaf
◦ Delete it immediately

10 10

5 30 Delete 2 5 30

2 25 45 25 45

66
Delete
Three cases:
(2) the node has one child
◦ Replace the node with its child

6 6

2 Delete 4 2 8
8

4 1 3 45
1 45

3
67
Delete
(3) the node has 2 children
◦ i. replace the node with the minimum element at the right subtree
(inorder successor)
◦ Ii. delete the minimum element by invoking case 1 or 2.

6 6

2 Delete 2 3 8
8

4 1 4 45
1 45

3
68
Delete
8
8

3 11 Delete 11
3 12

9 14
5 9 14
1 5
1

6 10 12 15 6 10 13 15

13

69
Delete
1. Delete node 78
2. Delete node 54
3. Delete node 56

70
Delete
1. Delete node 1 8

2. Delete node 5
3 11
3. Delete node 11
9 14
5
1

6 10 12 15

13

71
Delete Algorithm
p=tree; 8
q=null;
/*search for the node with the key val, set p to point
to the node and q to its parent, if any. */ 3 11
while (p!=null && p->data != val)
{
q=p; 9 14
5
p=(val < p->data ) ? p->left : p->right; 1
} //end while
if (p==null) { print (“val not found”); return;}
/* set the variable rp to the node that will replace node p.*/ 6 10 12 15
/* first two cases: the node to be deleted has at most
one child node.*/
if (p->left ==null) 13
rp= p->right;
else if (p->right==null)
rp=p->left;

72
Delete Algorithm (Iterative)
else {
/* third case : node p has two child nodes.
Set rp to the inorder successor of p and f
8
to the father of rp*/
f=p;
rp= p->right; 3 11
s= rp->left;
while (s!=null)
{ f=rp; 9 14
1 5
rp= s;
s= rp->left;
} 6 10 12 15
/* rp is the inorder successor of p */
if(f!=p) {
/* p is not the parent of rp and rp==f->left */ 13
f->left = rp->right;

73
Delete Algorithm (Iterative)cont..
/* remove node rp from its current position and replace it with the right son of
node rp*/ 8
rp-> right= p->right;
rp->left= p->left;
} 3 11

if (q==null)
9 14
tree= rp; 1
5
else
(p == q->left) ? q->left= rp : q->right = rp;
freenode (p); 6 10 12 15
return;

13

74
Delete (Recursive)
Node* deleteNode(Node* root, int k)
{
// Base case
if (root == NULL)
return root;

// Recursive calls for ancestors of


// node to be deleted
if (root->key > k) {
root->left = deleteNode(root->left, k);
return root;
}
else if (root->key < k) {
root->right = deleteNode(root->right, k);
return root;
}

// We reach here when root is the node


// to be deleted.

75
Delete (Recursive) cont..
// If one of the children is empty
if (root->left == NULL) { // Delete successor. Since successor
Node* temp = root->right; // is always left child of its parent
delete root; // we can safely make successor's right
return temp; // right child as left of its parent.
} // If there is no succ, then assign
else if (root->right == NULL) { // succ->right to succParent->right
Node* temp = root->left; if (succParent != root)
delete root; succParent->left = succ->right;
return temp; else
succParent->right = succ->right;
}
// If both children exist
else { // Copy Successor Data to root
root->key = succ->key;
Node* succParent = root;
// Delete Successor and return root
// Find successor delete succ;
Node* succ = root->right; return root;
while (succ->left != NULL) { }
succParent = succ; }
succ = succ->left;
}

76
Delete (Recursive) cont..
8

3 11

9 14
1 5

6 10 12 15

13

77
Activity
Write alg to search a given value in BST

struct node* search(struct node* root, int key)


{
// Base Cases: root is null or key is present at root
if (root == NULL || root->key == key)
return root;

// Key is greater than root's key


if (root->key < key)
return___________________

// Key is smaller than root's key


return______________________
}

78
Search operation (iterative)
search(struct node *tree, int val) while(ptr!=NULL&&________)
{ {
struct node *ptr=tree if(__________)
ptr=ptr–>left;
if(tree==NULL) else
{ ___________
__________ }
} if(ptr==NUL)
else ____________
{ else if(ptr-data==val)
____________
}

79
Expression Trees
Binary trees are widely used to store algebraic expressions. For example, consider the
algebraic expression given as:
Exp = (a – b) + (c * d)
This expression can be represented using a binary tree as shown in Fig. 9.13.

The expression tree is a binary tree in which each internal node corresponds to the
operator and each leaf node corresponds to the operand.

80
Expression tree-example
Given an expression,
Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h)), construct the
corresponding binary tree.

81
Huffman’s Tree
Huffman coding is an entropy encoding algorithm developed by David A.
Huffman that is widely used as a lossless data compression technique.
The Huffman coding algorithm uses a variable length code table to encode a
source character where the variable-length code table is derived on the basis of
the estimated probability of occurrence of the source character.
The key idea behind Huffman algorithm is that it encodes the most common
characters using shorter strings of bits than those used for less common source
characters.

82
Huffman’s Tree- Algorithm
The algorithm works by creating a binary tree of nodes that are stored in an array.
Step 1: Create a leaf node for each character. Add
the character and its weight or frequency of occurrence to
the priority queue.
Step 2: Repeat Steps 3 to 5 while the total number of nodes
in the queue is greater than 1.
Step 3: Remove two nodes that have the lowest weight (or
highest priority).
Step 4: Create a new internal node by merging these two
nodes as children and with weight equal to the sum of the
two nodes' weights.
Step 5: Add the newly created node to the queue.

83
Huffman’s Tree
Create a Huffman tree with the following nodes arranged in a priority queue.

84
Huffman’s Tree

85
Huffman’s Tree

86
Huffman’s Tree

87
Huffman’s Tree
Huffman’s codes
Character Code
A
B
C
D
E
F 101
G 000
H 001
I 011
J 11

88
Example
Construct Huffman tree

89
90

You might also like