Tree: A Non-linear Data StructureJanuary 5, 2013   Programming and   1
Background• Linked lists use dynamic memory allocation,  and hence enjoys some advantages over static  representations usi...
Illustration                                    50                            Level 1                    30               ...
Binary Tree• A new data structure   – Binary means two.• Definition   – A binary tree is either empty, or it consists of a...
Examples of Binary TreesJanuary 5, 2013   Programming and   5
Not Binary TreesJanuary 5, 2013   Programming and    6
Binary Search Trees• A particular form of binary tree suitable for  searching.• Definition   – A binary search tree is a b...
Examples     10                                             505          15                       30                      ...
How to Implement a Binary Tree?• Two pointers in every node (left and right).       struct nd {                     int el...
An Example                                      a• Create the tree                10                          b 5         ...
Traversal of Binary Trees• In many applications, it is required to move  through all the nodes of a binary tree, visiting ...
Inorder Traversal• Recursively, perform the following three steps:   – Visit the left subtree.   – Visit the root.   – Vis...
Example:: inorder traversal                                          10        10                      20                 ...
a                  b                           c          d                       e                   f              g    ...
Preorder Traversal• Recursively, perform the following three steps:   – Visit the root.   – Visit the left subtree.   – Vi...
Example:: preorder traversal                                         10        10                     20                  ...
a                  b                           c          d                       e                   f              g    ...
Postorder Traversal• Recursively, perform the following three steps:   – Visit the left subtree.   – Visit the right subtr...
Example:: postorder traversal                                         10        10                     20                 ...
a                  b                           c          d                       e                   f              g    ...
Implementations void inorder (node *root)                 void preorder (node *root){                                     ...
A case study :: Expression Tree                  *        +                     -               Represents the expression:...
Binary Search Tree (Revisited)• Given a binary search tree, how to write a program to  search for a given element?• Easy t...
Some Points to Note• The following operations are a little complex  and are not discussed here.   – Inserting a node into ...
Graph :: another important data structure• Definition   – A graph G={V,E} consists of a set of vertices V, and     a set o...
How to represent a graph in a program?• Many ways   – Adjacency matrix   – Incidence matrix, etc.January 5, 2013   Program...
Adjacency Matrix                     e1                          e5         1                         2                   ...
Incidence Matrix                  e1                           e5         1                      2                        ...
January 5, 2013   Programming and   29
Upcoming SlideShare
Loading in...5
×

L11 tree

126

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
126
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

L11 tree

  1. 1. Tree: A Non-linear Data StructureJanuary 5, 2013 Programming and 1
  2. 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
  3. 3. Illustration 50 Level 1 30 75 Level 2 12 45 53 80 Level 3 20 35 48 78 85 Level 4January 5, 2013 Programming and 3
  4. 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 subtreeJanuary 5, 2013 Programming and 4
  5. 5. Examples of Binary TreesJanuary 5, 2013 Programming and 5
  6. 6. Not Binary TreesJanuary 5, 2013 Programming and 6
  7. 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
  8. 8. Examples 10 505 15 30 75 12 20 12 45 53 80 20 35 48 78 85January 5, 2013 Programming and 8
  9. 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. 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. 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 traversalJanuary 5, 2013 Programming and 11
  12. 12. Inorder Traversal• Recursively, perform the following three steps: – Visit the left subtree. – Visit the root. – Visit the right subtree. LEFT-ROOT-RIGHTJanuary 5, 2013 Programming and 12
  13. 13. Example:: inorder traversal 10 10 20 30 20 30 40 25 50 60 20 10 30 40 20 25 10 50 30 60January 5, 2013 Programming and 13
  14. 14. a b c d e f g h i j k . d g b . a h e j i k c fJanuary 5, 2013 Programming and 14
  15. 15. Preorder Traversal• Recursively, perform the following three steps: – Visit the root. – Visit the left subtree. – Visit the right subtree. ROOT-LEFT-RIGHTJanuary 5, 2013 Programming and 15
  16. 16. Example:: preorder traversal 10 10 20 30 20 30 40 25 50 60 10 20 30 10 20 40 25 30 50 60January 5, 2013 Programming and 16
  17. 17. a b c d e f g h i j k a b d . g . c e h i j k fJanuary 5, 2013 Programming and 17
  18. 18. Postorder Traversal• Recursively, perform the following three steps: – Visit the left subtree. – Visit the right subtree. – Visit the root. LEFT-RIGHT-ROOTJanuary 5, 2013 Programming and 18
  19. 19. Example:: postorder traversal 10 10 20 30 20 30 40 25 50 60 20 30 10 40 25 20 50 60 30 10January 5, 2013 Programming and 19
  20. 20. a b c d e f g h i j k . g d . b h j k i e f c aJanuary 5, 2013 Programming and 20
  21. 21. Implementations void inorder (node *root) void preorder (node *root){ { if (root != NULL) if (root != NULL) { { inorder (root->left); printf (“%d “, root->element); printf (“%d “, root->element); inorder (root->left); inorder (root->right); inorder (root->right); } }} } void postorder (node *root) { if (root != NULL) { inorder (root->left); inorder (root->right); printf (“%d “, root->element); } } January 5, 2013 Programming and 21
  22. 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. 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. 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. 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. 26. How to represent a graph in a program?• Many ways – Adjacency matrix – Incidence matrix, etc.January 5, 2013 Programming and 26
  27. 27. Adjacency Matrix e1 e5 1 2 6 e2 e4 e6 e7 e3 e8 3 4 5 . 1 2 3 4 5 6 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 0January 5, 2013 Programming and 27
  28. 28. Incidence Matrix e1 e5 1 2 6 e2 e4 e6 e7 e3 e8 3 4 5 . e1 e2 e3 e4 e5 e6 e7 e8 1 1 1 0 0 0 0 0 0 2 1 0 0 1 1 1 0 0 3 0 1 1 0 0 0 0 0 4 0 0 1 1 0 0 0 1 5 0 0 0 0 0 1 1 1 6 0 0 0 0 1 0 1 0January 5, 2013 Programming and 28
  29. 29. January 5, 2013 Programming and 29
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×