L11 tree
-
Upload
mondalakash2012 -
Category
Documents
-
view
123 -
download
1
Transcript of L11 tree
April 12, 2023 Programming and Data Structure 1
Tree: A Non-linear Data Structure
April 12, 2023 Programming and Data Structure 2
Background
• Linked lists use dynamic memory allocation, and hence enjoys some advantages over static representations using arrays.
• A problem– Searching an ordinary linked list for a given
element.• Time complexity O(n).
– Can the nodes in a linked list be rearranged so that we can search in O(nlog2n) time?
April 12, 2023 Programming and Data Structure 3
Illustration
50
20
534512
7530
35
80
48 8578
Level 1
Level 2
Level 3
Level 4
April 12, 2023 Programming and Data Structure 4
Binary Tree
• A new data structure– Binary means two.
• Definition– A binary tree is either empty, or it consists of a node
called the root, together with two binary trees called the left subtree and the right subtree.
Root
Leftsubtree
Rightsubtree
April 12, 2023 Programming and Data Structure 5
Examples of Binary Trees
April 12, 2023 Programming and Data Structure 6
Not Binary Trees
April 12, 2023 Programming and Data Structure 7
Binary Search Trees
• A particular form of binary tree suitable for searching.
• Definition– A binary search tree is a binary tree that is either
empty or in which each node contains a key that satisfies the following conditions:
• All keys (if any) in the left subtree of the root precede the key in the root.
• The key in the root precedes all keys (if any) in its right subtree.
• The left and right subtrees of the root are again binary search trees.
April 12, 2023 Programming and Data Structure 8
Examples
20 35 48 8578
50
534512
7530
802012
155
10
April 12, 2023 Programming and Data Structure 9
How to Implement a Binary Tree?
• Two pointers in every node (left and right).struct nd { int element; struct nd *lptr; struct nd *rptr; };typedef nd node;node *root; /* Will point to the root of the tree */
April 12, 2023 Programming and Data Structure 10
An Example
• Create the tree
a = (node *) malloc (sizeof (node)); b = (node *) malloc (sizeof (node)); c = (node *) malloc (sizeof (node)); d = (node *) malloc (sizeof (node)); a->element = 10; a->lptr = b; a->rptr = c; b->element = 5; b->lptr = NULL; b->rptr = NULL; c->element = 20; c->lptr = d; c->rptr = NULL; d->element = 15; d->lptr = NULL; d->rptr = NULL; root = a;
10
5 20
15 d
cb
a
April 12, 2023 Programming and Data Structure 11
Traversal of Binary Trees
• In many applications, it is required to move through all the nodes of a binary tree, visiting each node in turn.– For n nodes, there exists n! different orders.
– Three traversal orders are most common:• Inorder traversal
• Preorder traversal
• Postorder traversal
April 12, 2023 Programming and Data Structure 12
Inorder Traversal
• Recursively, perform the following three steps:– Visit the left subtree.
– Visit the root.
– Visit the right subtree.
LEFT-ROOT-RIGHT
April 12, 2023 Programming and Data Structure 13
Example:: inorder traversal
3020
10
10
502540
3020
60
20 10 30 40 20 25 10 50 30 60
April 12, 2023 Programming and Data Structure 14
a
e
i
d
cb
f
hg
kj
. d g b . a h e j i k c f
April 12, 2023 Programming and Data Structure 15
Preorder Traversal
• Recursively, perform the following three steps:– Visit the root.
– Visit the left subtree.
– Visit the right subtree.
ROOT-LEFT-RIGHT
April 12, 2023 Programming and Data Structure 16
Example:: preorder traversal
3020
10
10
502540
3020
60
10 20 30 10 20 40 25 30 50 60
April 12, 2023 Programming and Data Structure 17
a
e
i
d
cb
f
hg
kj
a b d . g . c e h i j k f
April 12, 2023 Programming and Data Structure 18
Postorder Traversal
• Recursively, perform the following three steps:– Visit the left subtree.
– Visit the right subtree.
– Visit the root.
LEFT-RIGHT-ROOT
April 12, 2023 Programming and Data Structure 19
3020
10
10
502540
3020
60
Example:: postorder traversal
20 30 10 40 25 20 50 60 30 10
April 12, 2023 Programming and Data Structure 20
a
e
i
d
cb
f
hg
kj
. g d . b h j k i e f c a
April 12, 2023 Programming and Data Structure 21
Implementations void inorder (node *root){ if (root != NULL) { inorder (root->left); printf (“%d “, root->element); inorder (root->right); }}
void preorder (node *root){ if (root != NULL) { printf (“%d “, root->element); inorder (root->left); inorder (root->right); }}
void postorder (node *root){ if (root != NULL) { inorder (root->left); inorder (root->right); printf (“%d “, root->element); }}
April 12, 2023 Programming and Data Structure 22
A case study :: Expression Tree
*
cba
-+
+
d e
Represents the expression: (a + b) * (c – (d + e))
Preorder traversal :: * + a b - c + d e
Postorder traversal :: a b + c d e + - *
April 12, 2023 Programming and Data Structure 23
Binary Search Tree (Revisited)
• Given a binary search tree, how to write a program to search for a given element?
• Easy to write a recursive program. int search (node *root, int key) { if (root != NULL) { if (root->element == key) return (1); else if (root->element > key) search (root->lptr, key); else search (root->rptr, key); } }
April 12, 2023 Programming and Data Structure 24
Some Points to Note
• The following operations are a little complex and are not discussed here.– Inserting a node into a binary search tree.
– Deleting a node from a binary search tree.
April 12, 2023 Programming and Data Structure 25
Graph :: another important data structure
• Definition– A graph G={V,E} consists of a set of vertices V, and
a set of edges E which connect pairs of vertices.
– If the edges are undirected, G is called an undirected graph.
– If at least one edge in G is directed, it is called a directed graph.
April 12, 2023 Programming and Data Structure 26
How to represent a graph in a program?
• Many ways– Adjacency matrix
– Incidence matrix, etc.
April 12, 2023 Programming and Data Structure 27
Adjacency Matrix
1
53 4
2 6
e8
e5
e4e3
e2
e1
e7e6
. 1 2 3 4 5 61 0 1 1 0 0 02 1 0 0 1 1 13 1 0 0 1 0 04 0 1 1 0 1 05 0 1 0 1 0 16 0 1 0 0 1 0
April 12, 2023 Programming and Data Structure 28
Incidence Matrix
1
53 4
2 6
e8
e5
e4e3
e2
e1
e7e6
. e1 e2 e3 e4 e5 e6 e7 e81 1 1 0 0 0 0 0 02 1 0 0 1 1 1 0 03 0 1 1 0 0 0 0 04 0 0 1 1 0 0 0 15 0 0 0 0 0 1 1 16 0 0 0 0 1 0 1 0
April 12, 2023 Programming and Data Structure 29