Trees    EENG212    Algorithms           andData Structures
TreesOutline Introduction to Trees Binary Trees: Basic Definitions Traversing Binary Trees Node Representation of Bina...
Introduction to TreesBINARY TREES: BASIC DEFINITIONS A binary tree is a finite set of elements that are  either empty or ...
BINARY TREES: BASICDEFINITIONS                rootleft subtree                       right subtree
BINARY TREES: BASICDEFINITIONS   If A is the root of a binary tree and B is the root of its    left or right subtrees, th...
BINARY TREES: BASICDEFINITIONSleft son            right son           leaves
BINARY TREES: BASICDEFINITIONS   If every nonleaf node in a binary tree has    nonempty left and right subtrees, the tree...
BINARY TREES: BASICDEFINITIONS   The level of a node in a binary tree is defined    as follows: The root of the tree has ...
BINARY TREES: BASICDEFINITIONS
TRAVERSING BINARY TREES   One of the common operations of a binary tree is to    traverse the tree. Traversing a tree is ...
TRAVERSING BINARY TREES   Traversing a binary tree in preorder    (depth-first order)    1. Visit the root.    2. Travers...
Traversing a binary tree inpreorder          Preorder: ABDGCEHIF
TRAVERSING BINARY TREESTraversing a binary tree in inorder(or symmetric order)  1. Traverse the left subtree in inorder.  ...
Traversing a binary tree ininorder         Inorder: DGBAHEICF
TRAVERSING BINARY TREES   Traversing a binary tree in postorder    1. Traverse the left subtree in postorder.    2. Trave...
Traversing a binary tree inpostorder         Postorder: GDBHIEFCA
NODE REPRESENTATION OFBINARY TREES   Each node in a binary tree contains info, left, right    and father fields. The left...
PRIMITIVE FUNCTIONS INBINARY TREES The maketree function allocates a node and sets it as the root of a single node binary ...
PRIMITIVE FUNCTIONS INBINARY TREES   The setleft and setright functions sets a node with    content x as the left son and...
BINARY TREE TRAVERSALMETHODS   Recursive functions can be used to perform    traversal on a given binary tree. Assume tha...
BINARY TREE TRAVERSALMETHODSvoid pretrav(NODEPTR tree){if(tree != NULL){    printf(“%dn”, tree->info);    pretrav(tree->le...
BINARY TREE TRAVERSALMETHODS   The following recursive inorder traversal    function displays the info part of the nodes ...
BINARY TREE TRAVERSALMETHODSvoid intrav(NODEPTR tree){if(tree != NULL){    intrav(tree->left);    printf(“%dn”, tree->info...
BINARY TREE TRAVERSALMETHODS   The following recursive postorder traversal    function displays the info part of the node...
BINARY TREE TRAVERSALMETHODSvoid posttrav(NODEPTR tree){if(tree != NULL){    posttrav(tree->left);    posttrav(tree->right...
BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES   A binary tree, that has the property that all    elements in the left...
BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES Given the following sequence of numbers, 14, 15, 4, 9, 7, 18, 3, 5, 16,...
BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES
BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES The inorder (left-root-right) traversal of the  above Binary Search Tre...
SEARCHING THROUGH THEBINARY SEARCH TREE   Searching operation of the binary search tree    is always in downward directio...
SEARCHING THROUGH THEBINARY SEARCH TREEstruct node{ int info; /* can be of different type*/ struct node *left; struct node...
SEARCHING THROUGH THEBINARY SEARCH TREE   The following recursive function can be used    to search for a given key eleme...
SEARCHING THROUGH THEBINARY SEARCH TREEint BinSearch(NODEPTR p, int key){if(p == NULL)    return FALSE;else {if (key == p-...
INSERTING NODES INTO ABINARY SEARCH TREE   The following recursive function can be used    to insert a new node into a gi...
NODEPTR insert(NODEPTR p, int x){if(p == NULL){    p = getnode();    p->info = x;    p->left = NULL;    p->right = NULL;  ...
Application of Binary SearchTree   Suppose that we wanted to find all duplicates in a list of    numbers. One way of doin...
#include <stdio.h>#include <stdlib.h>struct node {           struct node *left ;           int info ;           struct nod...
while(scanf("%d",&number) !=EOF) { p=q=root;   /* find insertion point */   while((number !=p->info) && q!=NULL)    {p=q; ...
void intrav(NODEPTR tree){if(tree != NULL){      intrav(tree->left);      printf(“%dn”, tree->info);      intrav(tree->rig...
Upcoming SlideShare
Loading in …5
×

Trees

3,300 views

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,300
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
317
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Trees

  1. 1. Trees EENG212 Algorithms andData Structures
  2. 2. TreesOutline Introduction to Trees Binary Trees: Basic Definitions Traversing Binary Trees Node Representation of Binary Trees Primitive Functions in Binary Trees
  3. 3. Introduction to TreesBINARY TREES: BASIC DEFINITIONS A binary tree is a finite set of elements that are either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees called the left and right subtrees of the original tree. A left or right subtree can be empty. Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.
  4. 4. BINARY TREES: BASICDEFINITIONS rootleft subtree right subtree
  5. 5. BINARY TREES: BASICDEFINITIONS If A is the root of a binary tree and B is the root of its left or right subtrees, then A is said to be the father of B and B is said to be the left son of A. A node that has no sons is called the leaf. Node n1 is the ancestor of node n2 if n1 is either the father of n2 or the father of some ancestor of n2. In such a case n2 is a descendant of n1. Two nodes are brothers if they are left and right sons of the same father.
  6. 6. BINARY TREES: BASICDEFINITIONSleft son right son leaves
  7. 7. BINARY TREES: BASICDEFINITIONS If every nonleaf node in a binary tree has nonempty left and right subtrees, the tree is called a strictly binary tree.
  8. 8. BINARY TREES: BASICDEFINITIONS The level of a node in a binary tree is defined as follows: The root of the tree has level 0, and the level of any other node in the tree is one more than the level of its father. The depth of a binary tree is the maximum level of any leaf in the tree. A complete binary tree of depth d is the strictly binary all of whose leaves are at level d. A complete binary tree with depth d has 2d leaves and 2d-1 nonleaf nodes.
  9. 9. BINARY TREES: BASICDEFINITIONS
  10. 10. TRAVERSING BINARY TREES One of the common operations of a binary tree is to traverse the tree. Traversing a tree is to pass through all of its nodes once. You may want to print the contents of each node or to process the contents of the nodes. In either case each node of the tree is visited. There are three main traversal methods where traversing a binary tree involves visiting the root and traversing its left and right subtrees. The only difference among these three methods is the order in which these three operations are performed.
  11. 11. TRAVERSING BINARY TREES Traversing a binary tree in preorder (depth-first order) 1. Visit the root. 2. Traverse the left subtree in preorder. 3. Traverse the right subtree in preorder.
  12. 12. Traversing a binary tree inpreorder Preorder: ABDGCEHIF
  13. 13. TRAVERSING BINARY TREESTraversing a binary tree in inorder(or symmetric order) 1. Traverse the left subtree in inorder. 2. Visit the root. 3. Traverse the right subtree in inorder.
  14. 14. Traversing a binary tree ininorder Inorder: DGBAHEICF
  15. 15. TRAVERSING BINARY TREES Traversing a binary tree in postorder 1. Traverse the left subtree in postorder. 2. Traverse the right subtree in postorder. 3. Visit the root.
  16. 16. Traversing a binary tree inpostorder Postorder: GDBHIEFCA
  17. 17. NODE REPRESENTATION OFBINARY TREES Each node in a binary tree contains info, left, right and father fields. The left, right and father fields points the node’s left son, right son and the father respectively. struct node{ int info; /* can be of different type*/ struct node *left; struct node *right; struct node *father; }; typedef struct node *NODEPTR;
  18. 18. PRIMITIVE FUNCTIONS INBINARY TREES The maketree function allocates a node and sets it as the root of a single node binary tree. NODEPTR maketree(int x) { NODEPTR p; p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return p; }
  19. 19. PRIMITIVE FUNCTIONS INBINARY TREES The setleft and setright functions sets a node with content x as the left son and right son of the node p respectively.void setleft(NODEPTR p, int x) void setright(NODEPTR p, int x){ {if(p == NULL){ if(p == NULL){printf(“void insertionn”); printf(“void insertionn”);else if (p->left != NULL) else if (p->right != NULL)printf(“invalid insertionn”); printf(“invalid insertionn”);else elsep->left = maketree(x); p->right = maketree(x);} }
  20. 20. BINARY TREE TRAVERSALMETHODS Recursive functions can be used to perform traversal on a given binary tree. Assume that dynamic node representation is used for a given binary tree. In the following traversal methods, the tree is traversed always in downward directions. Therefore the father field is not needed. The following recursive preorder traversal function displays the info part of the nodes in preorder. Note that the info part is integer number and tree is a pointer to the root of the tree.
  21. 21. BINARY TREE TRAVERSALMETHODSvoid pretrav(NODEPTR tree){if(tree != NULL){ printf(“%dn”, tree->info); pretrav(tree->left); pretrav(tree->right);}}
  22. 22. BINARY TREE TRAVERSALMETHODS The following recursive inorder traversal function displays the info part of the nodes in inorder. Note that the info part is integer number and tree is a pointer to the root of the tree.
  23. 23. BINARY TREE TRAVERSALMETHODSvoid intrav(NODEPTR tree){if(tree != NULL){ intrav(tree->left); printf(“%dn”, tree->info); intrav(tree->right);}}
  24. 24. BINARY TREE TRAVERSALMETHODS The following recursive postorder traversal function displays the info part of the nodes in postorder. Note that the info part is integer number and tree is a pointer to the root of the tree.
  25. 25. BINARY TREE TRAVERSALMETHODSvoid posttrav(NODEPTR tree){if(tree != NULL){ posttrav(tree->left); posttrav(tree->right); printf(“%dn”, tree->info);}}
  26. 26. BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES A binary tree, that has the property that all elements in the left subtree of a node n are less than the contents of n, and all elements in the right subtree of n are greater than or equal to the contents of n, is called a Binary Search Tree or Ordered Binary Tree.
  27. 27. BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES Given the following sequence of numbers, 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 The following binary search tree can be constructed.
  28. 28. BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES
  29. 29. BINARY SEARCH TREE: ANAPPLICATION OF BINARY TREES The inorder (left-root-right) traversal of the above Binary Search Tree and printing the info part of the nodes gives the sorted sequence in ascending order. Therefore, the Binary search tree approach can easily be used to sort a given array of numbers. The inorder traversal on the above Binary Search Tree is: 3, 4, 4, 5, 5, 7, 9, 9, 14, 14, 15, 16, 17, 18, 20
  30. 30. SEARCHING THROUGH THEBINARY SEARCH TREE Searching operation of the binary search tree is always in downward direction. Therefore the following node structure can be used to represent the node of a given binary search tree. Note that the father link is not required.
  31. 31. SEARCHING THROUGH THEBINARY SEARCH TREEstruct node{ int info; /* can be of different type*/ struct node *left; struct node *right;};typedef struct node *NODEPTR;
  32. 32. SEARCHING THROUGH THEBINARY SEARCH TREE The following recursive function can be used to search for a given key element in a given array of integers. The array elements are stored in a binary search tree. Note that the function returns TRUE (1) if the searched key is a member of the array and FALSE (0) if the searched key is not a member of the array.
  33. 33. SEARCHING THROUGH THEBINARY SEARCH TREEint BinSearch(NODEPTR p, int key){if(p == NULL) return FALSE;else {if (key == p->info) return TRUE;else{if(key < p->info) return BinSearch(p->left, key);else return BinSearch(p->right, key);}}}
  34. 34. INSERTING NODES INTO ABINARY SEARCH TREE The following recursive function can be used to insert a new node into a given binary search tree.
  35. 35. NODEPTR insert(NODEPTR p, int x){if(p == NULL){ p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return p;}else{ if(x < p->info) p->left = insert(p->left, x); else p->right = insert(p->right, x); return p;}}
  36. 36. Application of Binary SearchTree Suppose that we wanted to find all duplicates in a list of numbers. One way of doing this to compare each number with all those that precede it. However this involves a large number of comparison. The number of comparison can be reduced by using a binary tree. The first number in the list is placed in a node that is the root of the binary tree with empty left and right sub-trees. The other numbers in the list is than compared to the number in the root. If it is matches, we have duplicate. If it is smaller, we examine the left sub-tree; if it is larger we examine the right sub- tree. If the sub-tree is empty, the number is not a duplicate and is placed into a new node at that position in the tree. If the sub-tree is nonempty, we compare the number to the contents of the root of the sub-tree and the entire process is repeated with the sub- tree. A program for doing this follows .
  37. 37. #include <stdio.h>#include <stdlib.h>struct node { struct node *left ; int info ; struct node *right; };typedef struct node *NODEPTR;NODEPTR maketree(int);NODEPTR getnode(void);void intrav(NODEPTR);void main(){int number;NODEPTR root , p , q; printf("%sn","Enter First number"); scanf("%d",&number); root=maketree(number); /* insert first root item */ printf("%sn","Enter the other numbers");
  38. 38. while(scanf("%d",&number) !=EOF) { p=q=root; /* find insertion point */ while((number !=p->info) && q!=NULL) {p=q; if (number <p->info) q = p->left; else q = p->right; } q=maketree(number); /* insertion */ if (number==p->info) printf("%d is a duplicate n",number); else if (number<p->info) p->left=q; else p->right=q; } printf("Tree Created n "); /* inorder Traversing */ intrav(root); }
  39. 39. void intrav(NODEPTR tree){if(tree != NULL){ intrav(tree->left); printf(“%dn”, tree->info); intrav(tree->right);}}NODEPTR maketree(int x){NODEPTR p;p = getnode();p->info = x;p->left = NULL;p->right = NULL;return p;}NODEPTR getnode(void){ NODEPTR p;p=(NODEPTR) malloc(sizeof(struct node));return p;}

×