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?
January 5, 2013 Programming and 2
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
Left Right
subtree subtree
January 5, 2013 Programming and 4
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.
January 5, 2013 Programming and 7
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 */
January 5, 2013 Programming and 9
10. An Example
a
• Create the tree 10
b 5 20 c
15 d
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;
January 5, 2013 Programming and 10
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
January 5, 2013 Programming and 11
12. Inorder Traversal
• Recursively, perform the following three steps:
– Visit the left subtree.
– Visit the root.
– Visit the right subtree.
LEFT-ROOT-RIGHT
January 5, 2013 Programming and 12
14. a
b c
d e f
g h i
j k
. d g b . a h e j i k c f
January 5, 2013 Programming and 14
15. Preorder Traversal
• Recursively, perform the following three steps:
– Visit the root.
– Visit the left subtree.
– Visit the right subtree.
ROOT-LEFT-RIGHT
January 5, 2013 Programming and 15
17. a
b c
d e f
g h i
j k
a b d . g . c e h i j k f
January 5, 2013 Programming and 17
18. Postorder Traversal
• Recursively, perform the following three steps:
– Visit the left subtree.
– Visit the right subtree.
– Visit the root.
LEFT-RIGHT-ROOT
January 5, 2013 Programming and 18
22. A case study :: Expression Tree
*
+ - Represents the expression:
(a + b) * (c – (d + e))
a b c +
d e
Preorder traversal :: * + a b - c + d e
Postorder traversal :: a b + c d e + - *
January 5, 2013 Programming and 22
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);
}
}
January 5, 2013 Programming and 23
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.
January 5, 2013 Programming and 24
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.
January 5, 2013 Programming and 25
26. How to represent a graph in a program?
• Many ways
– Adjacency matrix
– Incidence matrix, etc.
January 5, 2013 Programming and 26