Binary Tree
Binary Tree
23 April 2012
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?
23 April 2012
Illustration
50
Level 1
75
30
Level 2 Level 3
12
45
53
80
20
35
48
78
85
Level 4
23 April 2012
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 subtree
23 April 2012
Right subtree
Programming and Data Structure 4
23 April 2012
23 April 2012
Examples
10 50
15
30
75
12
20
12
45
53
80
20
35
48
78
85
23 April 2012
23 April 2012
An Example
Create the tree b 5
15 10
a
20
c 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;
23 April 2012
10
23 April 2012
11
Inorder Traversal
Recursively, perform the following three steps:
Visit the left subtree. Visit the root. Visit the right subtree. LEFT-ROOT-RIGHT
23 April 2012
12
10
20
30
20
30
40
25
50
60
20 10 30
40 20 25 10 50 30 60
23 April 2012
13
. d g b . a h e j i k c f
23 April 2012 Programming and Data Structure 14
Preorder Traversal
Recursively, perform the following three steps:
Visit the root. Visit the left subtree. Visit the right subtree. ROOT-LEFT-RIGHT
23 April 2012
15
10
20
30
20
30
40
25
50
60
10 20 30
10 20 40 25 30 50 60
23 April 2012
16
a b d . g . c e h i j k f
23 April 2012 Programming and Data Structure 17
Postorder Traversal
Recursively, perform the following three steps:
Visit the left subtree. Visit the right subtree. Visit the root. LEFT-RIGHT-ROOT
23 April 2012
18
10
20
30
20
30
40
25
50
60
20 30 10
40 25 20 50 60 30 10
23 April 2012
19
. g d . b h j k i e f c a
23 April 2012 Programming and Data Structure 20
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); } }
23 April 2012 Programming and Data Structure 21
23 April 2012
24
23 April 2012
25
23 April 2012
26
Adjacency Matrix
e1 e5 e4 e3 3 4 e8 5 e6
1 e2
6 e7
. 1 2 3 4 5 6
23 April 2012
1 0 1 1 0 0 0
2 1 0 0 1 1 1
3 1 0 0 1 0 0
4 0 1 1 0 1 0
5 0 1 0 1 0 1
6 0 1 0 0 1 0
27
Incidence Matrix
e1 e5 e4 e3 3 4 e8 5 e6
1 e2
6 e7
. 1 2 3 4 5 6
23 April 2012
e1 1 1 0 0 0 0
e2 1 0 1 0 0 0
e3 0 0 1 1 0 0
e4 0 1 0 1 0 0
e5 e6 0 0 1 1 0 0 0 0 0 1 1 0
e7 0 0 0 0 1 1
e8 0 0 0 1 1 0
28
23 April 2012
29