CS2201 DATA STRUCTURES
UNIT II
UNIT-II – TREE STRUCTURES SYLLABUS
Tree ADT – tree traversals – left child right sibling data structures for general trees – Binary Tree ADT – expression trees – applications of trees – binary search tree ADT – Threaded Binary Trees.
CHAPTER 1 TREE ADT
1.1 PRELIMINARIES: TREE: A tree is a finite set of one or more nodes such that there is a specially designated
node called the Root, and zero or more non empty sub trees T1, T2, Tk, each of whose roots are connected by a directed edge from Root R.
ROOT: A node which doesn’t have a parent. In the above tree the Root is A. NODE: Item of Information LEAF: A node which doesn’t have children is called leaf or Terminal node. Here B, K,
L, H, M, J are leafs. SIBLINGS : Children of the same parents are said to be siblings, Here B, C, D, E are
siblings, F, G are siblings. Similarly!, J & K, L are siblings. PATH: A path from node n1 to nk is defined as a sequence of nodes n1, n2, n3…..nk
such that ni is the parent of n
. There is exactly only one path from each node to root.
i+1
1
CS2201 DATA STRUCTURES
UNIT II
LENGTH: In fig 3.1.1 path from A to L is A, C, F, L. where A is the parent for C, C is
the parent of F and F is the parent of L. DEGREE: The number of subtrees of a node is called its degree.
Degree of A is 4 Degree of C is 2 Degree of D is I Degree of H is 0. In fig 3.1.1 the degree of the tree is 4. LEVEL: The level of a node is defined by initially letting the root be at level one, if a
node is at level L then its children are at level L + 1. Level of A is I. Level of B, C, D, is 2. Level of F, H, I, J is 3 Level of K, L, and M is 4. DEPTH: For any node n, the depth of n is the length of the unique path from root to n. The depth of the root is zero.
Infig3.l.1 Depth ofnodeFis2. Depth of node L is3. HEIGHT : For any node n, the height of the node n is the length of the longest path from
n to the leaf The height of the leaf is zero
In fig 3.1.1 Height of node F is 1. Height of L is 0.
2
CS2201 DATA STRUCTURES
UNIT II
LENGTH: In fig 3.1.1 path from A to L is A, C, F, L. where A is the parent for C, C is
the parent of F and F is the parent of L. DEGREE: The number of subtrees of a node is called its degree.
Degree of A is 4 Degree of C is 2 Degree of D is I Degree of H is 0. In fig 3.1.1 the degree of the tree is 4. LEVEL: The level of a node is defined by initially letting the root be at level one, if a
node is at level L then its children are at level L + 1. Level of A is I. Level of B, C, D, is 2. Level of F, H, I, J is 3 Level of K, L, and M is 4. DEPTH: For any node n, the depth of n is the length of the unique path from root to n. The depth of the root is zero.
Infig3.l.1 Depth ofnodeFis2. Depth of node L is3. HEIGHT : For any node n, the height of the node n is the length of the longest path from
n to the leaf The height of the leaf is zero
In fig 3.1.1 Height of node F is 1. Height of L is 0.
2
CS2201 DATA STRUCTURES
UNIT II
1.2 Implementation of Trees
One way to implement a tree would be to have in each node, besides its data, a pointer to each child of the node. Keep the children of each node in a linked list of tree nodes. typedef struct tree_node *tree_ptr; struct tree_node { element_type element; tree_ptr first_child; tree_ptr next_sibling; };
CHAPTER 2 TREE TRAVERSAL
TREE TRAVERSALS WITH APPLICATION: The 3 important orderings are pre order, post order, in order. Three traversal methods are a) Preorder Traversal b) Inorder Traversal c) Post order Traversal
Tree for Traversal A C
B F D
G
E
H
I
2.1 Pre order traversal
Pre order traversal of a binary tree yields the prefix expression of a binary tree.
3
CS2201 DATA STRUCTURES
UNIT II
Preorder traversal of binary tree.
1) Visits the root (R) 2) Traverses the left subtree preorder (l) 3) Traverses the right subtree preorder (r)
Procedure Preorder (n : node); begin list n; for each child C of n, if any in order from lift do PREORDER (C) end; {PREORDER}
Preorder traversal of tree yield the following list ABDECFGHI 2.2 Inorder Traversal
Inorder traversal of a binary tree yields the infix expression of a binary tree. The reorder traversal of binary tree 1) Traverses the left subtree (l) 2) Visits the root (R) 3) traverses the right subtree (r) Procedure INORDER (n : node); begin if n is a leaf then list n else begin INORDER (leftmost child of n); list n for each child C of n, except for the leftmost, in order from the left do INORDER (C) end 4
CS2201 DATA STRUCTURES
UNIT II
end; {INORDER} The inorder traversal of the tree yields the following listing of the nodes. DBEAFCHGI 2.3 Post order traversal
Post order traversal of a binary tree yields the post fix expression of the binary tree. The post order traversal of binary tree. 1) traverses the left subtree postorder (l) 2) traverses the right sub tree post order (r) 3) Visits the root (R) Preorder procedure
Procedure POSTORDER (n : node); begin for each child C of n, if any in order from left do POSTORDER (C) list n; end; {POST ORDER} The postorder traversal of the tree yields the following listing of the nodes. DEBFHIGCA APPLICATION:
There are many applications for trees. One of the popular uses is the directory structure in many common operating systems, including UNIX, VAX/VMS , and DOS. The root of this directory is /usr . (The asterisk next to the name indicates that /usr is itself a directory.) /usr has three children, mark , alex, and bill , which are themselves directories. Thus, /usr contains three directories and no regular files. The filename /usr/mark/book/ch1.r is obtained by following the leftmost child three times. Each / after the first indicates an edge; the result is the full pathname. This hierarchical file system is very popular, because it allows users to organize their data logically. Furthermore, two files in different directories can share the same name, because they must have different paths from the root and thus have different pathnames. A directory in the UNIX file system is just a file with a list of all its children, so the directories are structured almost exactly in accordance with the type declaration above.* Indeed, if the normal command to print a file is applied to a directory, then the names of the files in the directory can be seen in the output (along with other non-ASCII information).
5
CS2201 DATA STRUCTURES
UNIT II
UNIX DIRECTORY void list_directory ( Directory_or_file D ) { list_dir ( D, 0 ); } void list_dir ( Directory_or_file D, unsigned int depth ) { /*1*/ if ( D is a legitimate entry) { /*2*/ print_name ( depth, D ); /*3*/ if( D is a directory ) /*4*/ for each child, c, of D /*5*/ list_dir( c, depth+1 ); } }
Routine to list a directory in a hierarchical file system The (preorder) directory listing /usr mark book chr1.c chr2.c chr3.c course cop3530 fall88 syl.r spr89 syl.r sum89 syl.r junk.c alex junk.c bill work
6
CS2201 DATA STRUCTURES
UNIT II
course cop3212 fall88 grades prog1.r prog2.r fall89 prog1.r prog2.r grades
CHAPTER 3 LEFT CHILD RIGHT SIBLING FOR DATA STRUCTURES
Generally in the linked list representation of trees the node consists of a left pointer field and right pointer field. The left pointer field points to the left child and the right pointer field points to the right child. But here the right pointer field is marked to be null. In left child right sibling for data structures the right pointer field for the root node is null. The other children are connected to the left most child’s right field as sibling. The right pointer field is set to null for the parent node. But left most child’s right pointer is linked to the siblings. The right pointer is used only to link the sibling.
7
CS2201 DATA STRUCTURES
UNIT II
CHAPPTER 4 BINARY TREE
4.1 Definition
Binary Tree is a tree in which no node can have more than two children. Maximum number of nodes at level i of a binary tree is 2
i+1
4.2 IMPLEMENTATION: BINARY TREE NODE DECLARATIONS Struct TreeNode { int Element; Struct TreeNode *left; Struct TreeNode *Right; };
4.3 COMPARISON BETWEEN GENERAL TREE & BINARY TREE General Tree
Binary Tree
8
CS2201 DATA STRUCTURES
UNIT II
FULL BINARY TREE:
A full binary tree of height h has 2 h-1 - 1 nodes.
COMPLETE-BINARY-TREE:
A complete binary tree of height h has between 2 h and 2 h+1 -1 nodes. In the bottom level the elements should be filled from left to right.
4.4 REPRESENTATION OF A BINARY TREE
There are two ways for representing binary tree, they are •
Linear Representation
9
CS2201 DATA STRUCTURES
•
UNIT II
Linked Representation
4.4.1 Linear Representation
The elements are represented using arrays. For any element in position i, the left child is in position 2i, the right child is in position (2i + 1), and the parent is in position (i/2).
4.4.2 Linked Representation
The elements are represented using pointers. Each node in linked representation has three fields, namely, * Pointer to the left subtree * Datafield * Pointer to the right subtree In leaf nodes, both the pointer fields are assigned as NULL.
10
CS2201 DATA STRUCTURES
UNIT II
Each node in a tree is represented as a node consisting of three fields. The left pointer field is used to point the left child. The right pointer field is used to point the right child. In case if any one of the child is not available either the left or right the concerning pointer fields are set to NULL. CHAPTER 5 EXPRESSION TREE
Expression Tree is a binary tree in ‘which the leaf nodes are operands and the interior nodes are operators. Like binary tree, expression tree can also be travesed by inorder, preorder and postorder traversal. 5.1 Constructing an Expression Tree
Let us consider postfix expression given as an input for constructing an expression t performing the following steps: 1. Read one symbol at a time from the postfix expression. 2. Check whether the symbol is an operand or operator. (a) If the symbol is an operand, create a one - node tree and push a pointer onto the stack. (b) If the symbol is an operator pop two pointers from the stack namely T1 and T2 and form a new tree with root as the operator and T2 as a left child and T1 as a right child.A pointer to this new tree is then pushed onto the stack. Example -
ab + c *
The first two symbols are operand, so create a one node tree and push the pointer on to the stack.
Next ‘+‘symbol is read, so two pointers are popped, a new tree is formed and a pointer to this is pushed onto the stack 11
CS2201 DATA STRUCTURES
UNIT II
Next the operand C is read, so a one node tree is created and the pointer to it is pushed onto the stack
Now ‘*’ is read, so two trees are merged and the pointer to the final tree is pushed onto the stack.
CHAPTER 6 APPLICATION OF TREES
Application of Trees
Trees and Board Games – Trees are often used in implementing games, particularly “board” games 12
CS2201 DATA STRUCTURES
UNIT II
•
node represents a position in the board
•
branches from a node represent all the possible moves from that position
•
the children represent the new positions
Planning a Move Ahead – equivalent to choosing a path through the game tree Games with Cycles – it is possible to return to a previous position When cannot be implemented with trees – graphs are required
Binary Trees for – ordered binary trees can be used to represent Expression Evaluation and evaluate arithmetic expressions if a node is a leaf, the element in it specifies the value • if it is not a leaf, evaluate the children and combine them according to the • operation specified by the element
CHAPTER 7 THE SEARCH TREE ADT - BINARY SEARCH TREE
7.1 Definition: -
13
CS2201 DATA STRUCTURES
UNIT II
Binary search tree is a binary tree in which for every node X in the tree, the values of all the keys in its left subtree are smaller than the key value in x, and the values of all the keys in its right subtree are larger than the key value in X.
Comparision Between Binary Tree & Binary Search Tree Binary Tree
•
Binary Search Tree
A tree is said to be a binary tree if it
•
A binary search tree is a binary
has atmost two children. It doesn’t
tree in the key values in the left
have any order.
node is less the root and the key values in the right node is greater than the root.
14
CS2201 DATA STRUCTURES
UNIT II
7.2 IMPLEMENTATION OF BINARY SEARCH TREE DECLARATION ROUTINE FOR BINARY SEARCH TREE
7.2.1 Make Empty
This operation is mainly for initialization when the programmer prefer to initialize the first element as a one - node tree. ROUTINE TO MAKE AN EMPTY TREE
15
CS2201 DATA STRUCTURES
UNIT II
7.2.2 Find :
* Check whether the root is NULL if so then return NULL. * Otherwise, Check the value X with the root node value (i.e. T —> data) (1) If X is equal to T —>data, return T. (2) If X is less than T—> data, Traverse the left of T recursively. (3) If X is greater than T—> data, traverse the right of T recursively. ROUTINE FOR FIND OPERATION
Example - To Find an element 10 (consider, X = 10)
16
CS2201 DATA STRUCTURES
UNIT II
7.2.3 Find Min and Find Max:
This operation returns the position of the smallest element in the tree. To perform FindMin, start at the root and go left as long as there is a left child. The stopping point is the smallest element. RECURISVE ROUTINE FOR FINDMIN
17
CS2201 DATA STRUCTURES
UNIT II
} The minimum element is found in the left side of the tree as the smaller elements are
placed in the left side. First the tree is checked whether it is NULL or not. If it is not null the pointer is moved to the left side of the tree to find the minimum element. The traversing process is done on the left side until the left child pointer field becomes NULL. Example -
18
CS2201 DATA STRUCTURES
UNIT II
NON - RECURSIVE ROUTINE FOR FINDMIN
FindMax
FindMax routine return the position of largest elements in the tree. To perform a FindMax start at the root and go right as long as there is a right child. The stopping point is the largest element.
RECURSIVE ROUTINE FOR FINDMAX
NON RECURSIVE ROUTINE FOR FINDMAX
19
CS2201 DATA STRUCTURES
UNIT II
7.2.4 Insert
To insert the element X into the tree,
Check with the root node T
If it is less than the root, Traverse the left subtree recursively until it reaches the T —> left equals to NULL. Then X is placed in T —> left
If X is greater than the root. Traverse the right subtree recursively until it reaches the T —> right equals to NULL. Then X is placed in T —> right .
ROUTINE TO INSERT INTO A BINARY SEARCH TREE SearchTree Insert ( int x, searchTree T) { if(T = = NULL) { T = malloc (size of (Struct TreeNode)); if (T! =NULL) { T —> Element=X; T —> left = NULL; T —> right=NULL; }} else if(X Element)
20
CS2201 DATA STRUCTURES
UNIT II
T —> left = Insert (X, T —> left); else if(X > T —> Element) T —>Right = Insert (X, T —> Right); return T; } Example -
To insert 8,5,10, 15,20,18,3 First element 8 is considered as root
7.2.5 Delete
Deletion operation is the complex operation in the Binary search tree. To delete n consider the following three possibilities. CASE 1:3 Node to be deleted is a leaf node (ie) No children. CASE 2: Node with one child. CASE 3: Node with two children. CASE 1 : Node with no children (Leaf node)
If the node is a leaf node, it can be deleted immediately.
21
CS2201 DATA STRUCTURES
UNIT II
CASE 2 - Node with one child
If the node has one child, it can be deleted by adjusting its parent pointer that points to its child node To Delete 5
Case 3 : Node with two children It is difficult to delete a node which has two children.
The general strategy is to replace the data of the node to be deleted with its smallest data of the right subtree and recursively delete that node.
22
CS2201 DATA STRUCTURES
UNIT II
23
CS2201 DATA STRUCTURES
UNIT II
DELETION ROUTINE FOR BINERY SEARCH TREES
} 24
CS2201 DATA STRUCTURES
UNIT II
7.2.6 Average Case Analysis:
All of the operations except make_null , should take O(log n) time, because in constant time we descend a level in the tree, thus operating on a tree that is now roughly half as large. Indeed, the running time of all the operations, except make_null , is O(d ), where d is the depth of the node containing the accessed key. The average depth over all nodes in a tree is O(log n) on the assumption that all trees are equally likely. The sum of the depths of all nodes in a tree is known as the internal path length. The average internal path length of a binary search tree is calculated, where the average is taken over all possible binary search trees. Let D(n) be the internal path length for some tree T of n nodes. D(1) = 0. An n-node tree consists of an i-node left subtree and an (n - i - 1)-node right subtree, plus a root at depth zero for 0 i < n. D(i) is the internal path length of the left subtree with respect to its root. In the main tree, all these nodes are one level deeper. The same holds for the right subtree. Thus, we get the recurrence D (n)
=
D (i)
+
D (n
-
i
-1) +
n
-1
If all subtree sizes are equally likely, which is true for binary search trees (since the subtree size depends only on the relative rank of the first element inserted into the tree), but not binary trees, then the average value of both D(i) and D(n - i -1) is. This recurrence will be encountered and solved in Chapter 7, obtaining an average value of D(n) = O(n log n). Thus, the expected depth of any node is O(log n). As an example, the randomly generated 500-node tree shown in Figure 4.26 has nodes at expected depth 9.98. It is tempting to say immediately that this result implies that the average running time of all the operations discussed in the previous section is O(log n), but this is not entirely 25
CS2201 DATA STRUCTURES
UNIT II
true. The reason for this is that because of deletions, it is not clear that all binary search trees are equally likely. In particular, the deletion algorithm described above favors making the left subtrees deeper than the right, because we are always replacing a deleted node with a node from the right subtree. The exact effect of this strategy is still unknown, but it seems only to be a theoretical novelty. It has been shown that if we alternate insertions and deletions (n2) times, then the trees will have an expected depth of . After a quarter-million random insert/delete pairs, the tree that was somewhat right-heavy looks decidedly unbalanced (average depth = 12.51).
CHAPTER 8 THREADED BINARY TREE
8.1 DEFINITION
A Threaded Binary Tree is a binary tree in which every node that does not have a right child has a THREAD (in actual sense, a link) to its INORDER successor. By doing this threading we avoid the recursive method of traversing a Tree, which makes use of stacks and consumes a lot of memory and time. Threaded Trees: Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers
We can use these pointers to help us in inorder traversals.
We have the pointers reference the next node in an inorder traversal; called threads.
We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer.
Threaded Tree Code Example code: class Node { Node left, right; boolean leftThread, rightThread; }
26
CS2201 DATA STRUCTURES
UNIT II
Threaded Tree Example:
Threaded Tree Traversal: We start at the leftmost node in the tree, print it, and follow its right thread
If we follow a thread to the right, we output the node and continue to its right
If we follow a link to the right, we go to the leftmost node, print it, and continue
27
CS2201 DATA STRUCTURES
UNIT II
28
CS2201 DATA STRUCTURES
UNIT II
Threaded Tree Traversal Code:
Node leftMost(Node n) {
29
CS2201 DATA STRUCTURES
UNIT II
Node ans = n; if (ans == null) { return null; } while (ans.left != null) { ans = ans.left; } return ans; } void inOrder(Node n) { Node cur = leftmost(n); while (cur != null) { print(cur); if (cur.rightThread) { cur = cur.right; } else { cur = leftmost(cur.right); } } } Threaded Tree Modification: We’re still wasting pointers, since half of our leafs’ pointers are still null.
We can add threads to the previous node in an inorder traversal as well, which we can use to traverse the tree backwards or even to do postorder traversals.
30
CS2201 DATA STRUCTURES
UNIT II
31
CS2201 DATA STRUCTURES
UNIT II
UNIT-II – TREE STRUCTURES PART –A
1. Define non-linear data structure? Data structure which is capable of expressing more complex relationship than that of physical adjacency is called non-linear data structure. 2. Define tree? A tree is a data structure, which represents hierarchical relationship between individual data items. 3. Define leaf? In a directed tree any node which has out degree o is called a terminal node or a leaf. 4. What is meant by directed tree? Directed tree is an acyclic diagraph which has one node called its root with indegree o while all other nodes have indegree I. 5. What is a ordered tree? In a directed tree if the ordering of the nodes at each level is prescribed then such a tree is called ordered tree. 6.What is a Binary tree? A Binary tree is a finite set of elements that is 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 7. What are the applications of binary tree? Binary tree is used in data processing. a. File index schemes b. Hierarchical database management system 8. What is meant by traversing? Traversing a tree means processing it in such a way, that each node is visited only once.
32
CS2201 DATA STRUCTURES
UNIT II
9. What are the different types of traversing? The different types of traversing are a. Pre-order traversal-yields prefix from of expression. b. In-order traversal-yields infix form of expression. c. Post-order traversal-yields postfix from of ex pression.
10. What are the two methods of binary tree implementation? Two methods to implement a binary tree are, a. Linear representation. b. Linked representation 11. Define pre-order traversal? Pre-order traversal entails the following steps; a. Visit the root node b. Traverse the left subtree c. Traverse the right subtree 12.Define post-order traversal? Post order traversal entails the following steps; a. Traverse the left subtree b. Traverse the right subtree c. Visit the root node 13. Define in -order traversal? In-order traversal entails the following steps; a. Traverse the left subtree b. Visit the root node c. Traverse the right subtree 14. What is the length of the path in a tree? The length of the path is the number of edges on the path. In a tree there is exactly one path form the root to each node. 15. Define expression trees? The leaves of an expression tree are operands such as constants or variable names and the other nodes contain operators. 16.Define Strictly binary tree? If every nonleaf node in a binary tree has nonempty left and right subtrees ,the tree is termed as a strictly binary tree. 17.Define complete binary tree? A complete binary tree of depth d is the strictly binary tree all of whose are at level d. 18.What is an almost complete binary tree? 33