Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical Session No. 4 Trees: Trees As Basic Data Structures Tree

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

Practical session No.

4 Trees
Trees as Basic Data Structures Tree
ADT that stores elements hierarchically. With the exception of the root, each node in the tree has a parent and zero or more children nodes. Access methods in the tree ADT: root() returns the root of the tree parent(v) returns the parent of node v children(v) returns an iterator of the children of node v. Depth(node v) The depth of node v = the number of ancestors of v depth(root) = 0 depth(v root) = depth(v.parent) + 1 Height(node v) The maximum height of a node v in T height(v) = 0 , if v is a leaf height(v) = 1 + maximum-height of a child of v, if v is not a leaf. Height(tree T) The height of the root of T

Binary Tree

Each node has at most two children, left-son and right-son. Inorder inorder on the left sub tree, visit the current node and finally, inorder on the right sub tree. Preorder visit the current node, preorder on its left sub tree and finally, preorder on its right sub tree. Postorder - postorder on the left sub tree, postorder on the right sub tree and finally, visit the current node. Full Binary Tree A binary tree in which each node has exactly zero or two children A full binary tree with n leaves has n-1 inner nodes.

k-Tree

Complete Binary Tree A binary tree in which all the leaf nodes are at some depth d or d-1, and all leaves at depth d are toward the left. A complete binary tree of height h has between 2h and 2h+1-1 nodes. The height of a complete binary tree with n nodes is log n Each node has k children at most. Representation option: 'left child - right sibling', each node has the following

pointers:

parent left child - a pointer to the left most child. right-sibling - a pointer to the next sibling on the right.

Question 1
For a binary tree T let's define: L(T) = Number of leaves in T. D2(T) = Number of nodes in T that their degree is 2. Prove that for a given binary tree T with n nodes, D2(T) = L(T) - 1. Solution: Complete induction on the number of nodes in the tree: Induction base: n = 1 a single node. D2(T) = 0 = L(T)-1 Assumption: The theorem is true for a binary tree with k < n nodes. Induction step: 2 optional cases: a. The roots degree is 1 b. The roots degree is 2

Case a: The root's degree is 1. D2(T) = D2(T1) = L(T1) 1 = L(T) 1

Case b: The root's degree is 2: L(T) = L(T1) + L(T2) D2(T) = D2(T1) + D2(T2) + 1 (the root) = L(T1) - 1 + L(T2) - 1 + 1= L(T) 1

Therefore, the theorem is true. Note: In a complete tree T, L(T) = the number of inner nodes + 1. So, for example, the size of a complete tree with n leaves is O(n).

Question 2
T is a binary tree with n nodes. Any node x has the following fields:
x.key - natural number x.left - pointer to the left son x.right - pointer to the right son x.val - natural number for general

use.

maxPath(T) = the maximal sum of keys in a path from the root to a leaf. a. Find an algorithm for finding maxPath(T) in O(n). b. Can you improve the algorithm so that it will print the maximal path as well? Example:

Solution: a. Finding maxPath(T). Compute recursively for the left sub-tree and the right sub-tree and add the root's key to the maximal sum.

maxPath( T ){ if (T == null) return 0; lmax = maxPath(left-sub-tree(T)); rmax = maxPath(right-sub-tree(T)); m = Max(lmax, rmax); return m + T.key; } b. 1. Finding maxPath(T). Same as before, only use x.val to remember actual path. x.val = 0 indicates that the maximal sum was accepted from the left sub-tree. x.val = 1 indicates the maximal sum was accepted from the right sub-tree. 2. Printing the path. For any node x, use x.val to decide which way to go next.

maxPath( T ){ if (T == null) return 0; lmax = maxPath(left-sub-tree(T)); rmax = maxPath(right-sub-tree(T)); m = Max(lmax, rmax); if (m == lmax) T.val = 0 else T.val = 1 return m + T.key; }

PrintPath(T){ While (T.left null or T.right null ) if (T.val == 0) print("left ") T = T.left else print("right ") T = T.right }

Time complexity: maxPath(T) O(n) since every node was visited once. PrintPath(T) O(n) since every node in the path was visited once, and this number is

bounded by n. Totally O(n)

Question 3
Construct the tree from the following preorder and inorder travels: Preorder: a,b,c,d,e,g,h,j,f. Inorder: c,d,b,a,h,g,j,e,f.

Solution: From the preorder we can say that a is the root, that is why we can divide the inorder travel into two sub-trees [c,d,b] a [h,g,j,e,f] (in the preorder form it will be a [b,c,d] [e,g,h,j,f] ) a c,d,b h,g,j,e,f

In the left sub-tree from the preorder form ([b,c,d]) we can say that b is the root and again we could divide the left side in to additional two sub-trees [c,d] b [].

a b c,d h,g,j,e,f

In the same way we can also unfold c,d.

a b c d h,g,j,e,f

The next iterations will be:

a b c d h,g,j e f

And finally: a b c d h g j e f

Claim: from two travels on some tree where one of them is inorder we can reconstruct the tree. One way of proving this claim is done using the process that was described in this question. Question 4 Is it always possible to recreate a binary tree according to its Preorder and Postorder visits? Solution:

Not always. Example: Preorder: AB Postorder: BA The tree can be one of the two:

Binary Search Trees


BST property In a BST T, for any node x in T: 1. If y is a node in the left sub-tree of x then key(y) < key(x). 2. if y is a node in the right sub-tree of x then key(y) key(x). Minimal key Maximal key The key of the leftmost node. The key of the rightmost node.

Predecessor x is a node in a BST T. The predecessor of x is the node preceding x in an in-order traversal on T. If x has left son the maximum in the left sub-tree of x. Otherwise, predecessor is the lowest ancestor of x whose right child is also ancestor of x. Successor x is a node in a BST T. The successor of x is the node which succeeds x in an in-order traversal on T. If x has right son the minimum in the right sub-tree of x. Otherwise, successor is the lowest ancestor of x whose left child is also ancestor of x. Delete Delete node x from a BST T:

If x is a leaf and the left child of his parent x.parent.left = null

If x is a leaf and the right child of his parent x.parent.right = null If x has only one child, update x.parent to point to x's child instead of x. update the parent of the child of x to be the former parent of x If x has 2 children, replace x in node y, it's successor (y has no left son) Note that y might have right son. If so, then apply the same process of deleting a node with only one child to y.(see previous section)

Example of BST:

Question 5
Given a BST T of size n, in which each node x has an additional field x.size, the number of the keys in the sub-tree rooted at x (including the root node x). Suggest an O(h) time algorithm Greater(T, k) to find the number of keys that are strictly greater than k (h is the height of the binary search tree).

Solution : Greater(T, k) keys = 0 while (T != Null ) if (k < T.key) keys keys + T.right.size + 1 T = T.left else if (k > T.key) T = T.right {*} else // k = T.key keys keys + T.right.size break; return keys The suggested algorithm works well if the key is a unique value for each node. Otherwise when reaching {*}, i.e., k=T.key, we should start a process of moving to the right until reaching a node y with a key that is bigger then k, and then we should return keys + y.size. Time Complexity: O(h) Note: h=O(n) in the worst case and O(logn) in the average case (randomized tree).

Question 6
T is a BST of height h. Find an algorithm that returns all the keys in the range [a,b] (a<b) in O(h+k) time, where k is the number or keys in the range.

Solution: Range(a, b)

1. Find nodes containing a and b in the tree, let Pa and Pb be the search path from the root to a and b respectively. 2. Let x be the node where Pa and Pb split. 3. for every node v in the search path from a to x do: if (a < v){ print v; inorder(v.right); } 4. for every node v in the search path from x to b do: if (b > v){ inorder(v.left); print v; }

b a

Time complexity: Pa and Pb are at most O(h) long each. In-order travel is O(n) for a tree with n nodes. We are printing k numbers. So the inorder traversals cost O(k). Total time O(h+k)

Question 7
There are two binary search trees T1 and T2 and their heights h1 and h2 respectively. All the keys in T1 are smaller than all the keys in T2. Assume all key values are distinct. 1. How can you merge T1 and T2 into one binary search tree in time O(min(h1,h2)) so that the height of the merged tree will be minimal? 2. What would be the height of the merged tree?

Solution:

1. case a: h1 h2 Extract from T1 the element v with the maximum key in T1 in O(h1) time. v.left T1 v.right T2 v is the root of the new merged tree. case b: h1 > h2 Extract from T2 the element v with the minimum key in T2 in O(h2) time. v.left T1 v.right T2 v is the root of the new merged tree. 2. The height of the merged tree = max(h1,h2)+1

Question 8
Suppose we have numbers between 1 and 1000 in a binary search tree. We want to search for the number 363. Which of the following sequences could not be the sequence of nodes examined? a. b. c. d. e. 2, 252, 401, 398, 330, 344, 397, 363 924, 220, 911, 244, 898, 258, 362, 363 925, 202, 911, 240, 912, 245, 363 2, 399, 387, 219, 266, 382, 381, 278, 363 935, 278, 347, 621, 299, 392, 358, 363 c,e

Solution:

Sequence c isn't a possible sequence because 912 can't be in the left sub-tree of 911. Sequence e isn't a possible sequence because 299 can't be in the right sub-tree of 347.

You might also like