














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data Structures and Algorithms - Trees
Typology: Study notes
1 / 54
This page cannot be seen from the preview
Don't miss anything!















































b c b c d e f d Not a Tree a
A data structure is said to be linear if its elements form a sequence or a linear list. Previous linear data structures that we have studied like an array, stacks, queues and linked lists organize data in linear order. A data structure is said to be non linear if its elements form a hierarchical classification where, data items appear at various levels. Trees and Graphs are widely used non-linear data structures. Tree and graph structures represents hierarchial relationship between individual data elements. Graphs are nothing but trees with certain restrictions removed. In this chapter in particular, we will explain special type of trees known as binary trees, which are easy to maintain in the computer. 5.1. TREES: A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the top of the hierarchy. Each node can have at most one link coming into it. The node where the link originates is called the parent node. The root node has no parent. The links leaving a node (any number of links are allowed) point to child nodes. Trees are recursive structures. Each child node is itself the root of a subtree. At the bottom of the tree are leaf nodes, which have no children. Trees represent a special case of more general structures known as graphs. In a graph, there is no restrictions on the number of links that can enter or leave a node, and cycles may be present in the graph. The figure 5.1.1 shows a tree and a non-tree. In a tree data structure, there is no distinction between the various children of a node i.e., none is the "first child" or "last child". A tree in which such distinctions are made is called an ordered tree , and data structures built on them are called ordered tree data structures. Ordered trees are by far the commonest form of tree data structure.
A B C right child left subtree D E F G H I
In general, tree nodes can have any number of children. In a binary tree, each node can have at most two children. A binary tree is either empty or consists of a node called the root together with two binary trees called the left subtree and the right subtree. A tree with no nodes is called as a null tree. A binary tree is shown in figure 5.2.1. Figure 5.2.1. Binary Tree Binary trees are easy to implement because they have a small, fixed number of child links. Because of this characteristic, binary trees are the most common types of trees and form the basis of many important data structures. Tree Terminology: Leaf node A node with no children is called a leaf (or external node ). A node which is not a leaf is called an internal node. Path A sequence of nodes n 1 , n 2 ,.. ., n k, such that n i is the parent of n i + 1 for i = 1, 2,.. ., k - 1. The length of a path is 1 less than the number of nodes on the path. Thus there is a path of length zero from a node to itself. For the tree shown in figure 5.2.1, the path between A and I is A, B, D, I. Siblings The children of the same parent are called siblings. For the tree shown in figure 5.2.1, F and G are the siblings of the parent node C and H and I are the siblings of the parent node D. Ancestor and Descendent If there is a path from node A to node B, then A is called an ancestor of B and B is called a descendent of A_._ Subtree Any node of a tree, with all of its descendants is a subtree.
1 2 Strict Binary Tree (a) 3 1 2 3 6 7 4 5 6 13 8 9 Strictly^ Complete binary tree 1 1 2 3 2 3 4 5 6 7 4 5 6 9 10 8 9 10 11 12 13 14 15 (c) Full binary tree Strictly Binary tree: If every non-leaf node in a binary tree has nonempty left and right subtrees, the tree is termed as strictly binary tree. Thus the tree of figure 5.2.3(a) is strictly binary. A strictly binary tree with n leaves always contains 2n - 1 nodes. Full Binary tree: A full binary tree of height h has all its leaves at level h. Alternatively; All non leaf nodes of a full binary tree have two children, and the leaf nodes have no children. A full binary tree with height h has 2h^ + 1^ - 1 nodes. A full binary tree of height h is a strictly binary tree all of whose leaves are at level h. Figure 5.2.3(d) illustrates the full binary tree containing 15 nodes and of height 3. A full binary tree of height h contains 2 h^ leaves and, 2 h^ - 1 non-leaf nodes. h 2 l^ 2 h^^1 1. l 0 For example, a full binary tree of height 3 contains 2 3+1^1 = 15 nodes. Figure 5.2.3. Examples of binary trees Complete Binary tree: A binary tree with n nodes is said to be complete if it contains all the first n nodes of the above numbering scheme. Figure 5.2.4 shows examples of complete and incomplete binary trees. A complete binary tree of height h looks like a full binary tree down to level h-1, and the level h is filled from left to right.
1 1 2 3 2 3 2 4 5 6 4 5 7 4 (a) (c) c a d Internal Nodes: a, b, c, d b 4 5 2 3 A complete binary tree with n leaves that is not strictly binary has 2n nodes. For example, the tree of Figure 5.2.3(c) is a complete binary tree having 5 leaves and 10 nodes. Figure 5.2.4. Examples of complete and incomplete binary trees Internal and external nodes: We define two terms: Internal nodes and external nodes. An internal node is a tree node having at least one key and possibly some children. It is some times convenient to have another types of nodes, called an external node, and pretend that all null child place holder for nodes to be inserted. We draw internal nodes using circles, with letters as labels. External nodes are denoted by squares. The square node version is sometimes called an extended binary tree. A binary tree with n internal nodes has n+1 external nodes. Figure 5.2.6 shows a sample tree illustrating both internal and external nodes. Figure 5.2.6. Internal and external nodes Data Structures for Binary Trees:
Figure 5.2.7. Linked representation for the binary tree 5.3. Binary Tree Traversal Techniques: A tree traversal is a method of visiting every node in the tree. By visit, we mean that some type of operation is performed. For example, you may wish to print the contents of the nodes. There are four common ways to traverse a binary tree:
_1. Preorder
print root - > data; inorder(root->rchild); } } Preorder Traversal: In a preorder traversal, each root node is visited before its left and right subtrees are traversed. Preorder search is also called backtracking. The steps for traversing a binary tree in preorder traversal are:
2 7 2 6 9 5 11 Preo rder traversal yields: 2, 7 , 2 , 6 , 5 , 11 , 5 , 9 , 4 Posto rder travarsal yields: 2, 5 , 11 , 6 , 7 , 4 , 9 , 5 , 2 Ino rder travarsal yields: 2, 7 , 5 , 6 , 11 , 2 , 5 , 4 , 9 Level o rder traversal yields: 2, 7 , 5 , 2 , 6 , 9 , 5 , 11 , 4 A B C D E G H K L M Preo rder traversal yields: A, B, D, G , K, H, L, M , C , E Posto rder travarsal yields: K, G , L, M , H, D, B, E, C , A Ino rder travarsal yields: K, G , D, L, H, M , B, A, E, C Level o rder traversal yields: A, B, C , D, E, G , H, K, L, M Example 3: Traverse the following binary tree in pre, post, inorder and level order. Pre, P o st, Inorder a nd lev e l order T rav ers ing Example 4: Traverse the following binary tree in pre, post, inorder and level order. Bina ry T re e Pre, P o st, Inorder a nd lev e l order T rav ers ing 5.3.2. Building Binary Tree from Traversal Pairs: Sometimes it is required to construct a binary tree if its traversals are known. From a single traversal it is not possible to construct unique binary tree. However any of the two traversals are given then the corresponding tree can be drawn uniquely: Inorder and preorder Inorder and postorder Inorder and level order The basic principle for formulation is as follows: If the preorder traversal is given, then the first node is the root node. If the postorder traversal is given then the last node is the root node. Once the root node is identified, all the nodes in the left sub-trees and right sub-trees of the root node can be identified using inorder. Same technique can be applied repeatedly to form sub-trees.
A D G B H E I C F It can be noted that, for the purpose mentioned, two traversal are essential out of which one should be inorder traversal and another preorder or postorder; alternatively, given preorder and postorder traversals, binary tree cannot be obtained uniquely. Example 1: Construct a binary tree from a given preorder and inorder sequence: Preorder: A B D G C E H I F Inorder: D G B A H E I C F Solution: From Preorder sequence A B D G C E H I F , the root is: A From Inorder sequence D G B A H E I C F , we get the left and right sub trees: Left sub tree is: D G B Right sub tree is: H E I C F The Binary tree upto this point looks like: To find the root, left and right sub trees for D G B: From the preorder sequence B D G, the root of tree is: B From the inorder sequence D G B, we can find that D and G are to the left of B. The Binary tree upto this point looks like: To find the root, left and right sub trees for D G: From the preorder sequence D G, the root of the tree is: D From the inorder sequence D G , we can find that there is no left node to D and G is at the right of D. A B H E I C F D G
A D G B H E I C F A B H E I C F D G A B H E I C F D G Solution: From Postorder sequence G D B H I E F C A , the root is: A From Inorder sequence D G B A H E I C F , we get the left and right sub trees: Left sub tree is: D G B Right sub tree is: H E I C F The Binary tree upto this point looks like: To find the root, left and right sub trees for D G B: From the postorder sequence G D B, the root of tree is: B From the inorder sequence D G B, we can find that D G are to the left of B and there is no right subtree for B. The Binary tree upto this point looks like: To find the root, left and right sub trees for D G: From the postorder sequence G D , the root of the tree is: D From the inorder sequence D G , we can find that is no left subtree for D and G is to the right of D. The Binary tree upto this point looks like: To find the root, left and right sub trees for H E I C F: From the postorder sequence H I E F C , the root of the left sub tree is: C From the inorder sequence H E I C F, we can find that H E I are to the left of C and F is the right subtree for C.
A B C D H E I F G A B C D E F G H I n n1 n2 n3 n4 n5 n7 n8 n The Binary tree upto this point looks like: To find the root, left and right sub trees for H E I: From the postorder sequence H I E , the root of the tree is: E From the inorder sequence H E I , we can find that H is left subtree for E and I is to the right of E. The Binary tree upto this point looks like: Example 3: Construct a binary tree from a given preorder and inorder sequence: Inorder: n1 n2 n3 n4 n5 n6 n7 n8 n Preorder: n6 n2 n1 n4 n3 n5 n9 n7 n Solution: From Preorder sequence n6 n2 n1 n4 n3 n5 n9 n7 n8, the root is: n From Inorder sequence n1 n2 n3 n4 n5 n6 n7 n8 n9 , we get the left and right sub trees: Left sub tree is: n1 n2 n3 n4 n Right sub tree is: n7 n8 n The Binary tree upto this point looks like:
n n2 n n1 n4 n n3 n5 n n n1 n2 n3 n4 n n n n From the inorder sequence n7 n8 , we can find that is no left subtree for n7 and n8 is at the right of n7. The Binary tree upto this point looks like: Example 4: Construct a binary tree from a given postorder and inorder sequence: Inorder: n1 n2 n3 n4 n5 n6 n7 n8 n Postorder: n1 n3 n5 n4 n2 n8 n7 n9 n Solution: From Postorder sequence n1 n3 n5 n4 n2 n8 n7 n9 n6 , the root is: n From Inorder sequence n1 n2 n3 n4 n5 n6 n7 n8 n9 , we get the left and right sub trees: Left sub tree is: n1 n2 n3 n4 n Right sub tree is: n7 n8 n The Binary tree upto this point looks like: To find the root, left and right sub trees for n1 n2 n3 n4 n5 : From the postorder sequence n1 n3 n5 n4 n2 , the root of tree is: n From the inorder sequence n1 n2 n3 n4 n5 , we can find that n1 is to the left of n2 and n3 n4 n5 are to the right of n2. The Binary tree upto this point looks like:
n n2 n7 n8 n n1 n n3 n n n2 n n1 n4 n7 n n3 n n n2 n n1 n4 n n3 n5 n To find the root, left and right sub trees for n3 n4 n5: From the postorder sequence n3 n5 n4 , the root of the tree is: n From the inorder sequence n3 n4 n5 , we can find that n3 is to the left of n4 and n5 is to the right of n4. The Binary tree upto this point looks like: To find the root, left and right sub trees for n7 n8 and n9: From the postorder sequence n8 n7 n9 , the root of the left sub tree is: n From the inorder sequence n7 n8 n9 , we can find that n7 and n8 are to the left of n and no right subtree for n9. The Binary tree upto this point looks like: To find the root, left and right sub trees for n7 and n8: From the postorder sequence n8 n7 , the root of the tree is: n From the inorder sequence n7 n8 , we can find that there is no left subtree for n7 and n8 is to the right of n7. The Binary tree upto this point looks like:
void inorder(node *root) { if( root != NULL ) { inorder(root->lchild); printf("%3s",root->data); inorder(root->rchild); } } void preorder(node *root) { if( root != NULL ) { printf("%3s",root->data); preorder(root->lchild); preorder(root->rchild); } } void postorder(node *root) { if( root != NULL ) { postorder(root->lchild); postorder(root->rchild); printf("%3s",root->data); } } void levelorder() { int j; for(j = 0; j < ctr; j++) { if(tree[j] != NULL) printf("%3s",tree[j]->data); } } void print_leaf(node *root) { if(root != NULL) { if(root->lchild == NULL && root->rchild == NULL) printf("%3s ",root->data); print_leaf(root->lchild); print_leaf(root->rchild); } } int height(node *root) { if(root == NULL) { return 0; }
if(root->lchild == NULL && root->rchild == NULL) return 0; else } return (1 + max(height(root->lchild), height(root->rchild))); void main() { int i; create_fbinarytree(); printf("\n Inorder Traversal: "); inorder(tree[0]); printf("\n Preorder Traversal: "); preorder(tree[0]); printf("\n Postorder Traversal: "); postorder(tree[0]); printf("\n Level Order Traversal: "); levelorder(); printf("\n Leaf Nodes: "); print_leaf(tree[0]); printf("\n Height of Tree: %d ", height(tree[0])); } 5.3.4. Binary Tree Creation and Traversal Using Pointers: This program performs the following operations:
struct tree { struct tree* lchild; char data[10]; struct tree* rchild; }; typedef struct tree node; node Q[50]; int node_ctr; node getnode() { node temp ; temp = (node) malloc(sizeof(node)); printf("\n Enter Data: "); fflush(stdin); scanf("%s",temp->data); temp->lchild = NULL; temp->rchild = NULL; return temp; }