Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Trees. Tree: Section 4.1 (Weiss) Formal Definition Tree is a sequence of nodes. There is a starting node known as root node. Every node other.

Similar presentations


Presentation on theme: "Lecture 5 Trees. Tree: Section 4.1 (Weiss) Formal Definition Tree is a sequence of nodes. There is a starting node known as root node. Every node other."— Presentation transcript:

1 Lecture 5 Trees

2 Tree: Section 4.1 (Weiss)

3 Formal Definition Tree is a sequence of nodes. There is a starting node known as root node. Every node other than the root has a parent node. Nodes may have any number of children. A has 3 children, B, C, D A is parent of B A BCD E

4 Path to a node p is a sequence of nodes root,n 1, n 2, …..p such that n 1 is a child of root, n 2 is a child of n 1 …… Path to E is ? How many paths are there to one node? Depth of a node is the length of the unique path from the root to the node (not counting the node). Root is at depth 0 Depth of E is ? A,B, E Just one! 2

5 Leaves are nodes without any children. D and E are leaf nodes. Height of a nonleaf node is the length of the LONGEST path from the node to a leaf(not counting the leaf). Height of a leaf is 0 Height of A is ? 2

6 Application Organization of a file system. Root directory EE220, CSE260, TCOM 500 EE220: Lecture Notes, HW Every node has one or more elements: Directory example: element of a node is the name of the corresponding directory Root EE220CSE260TCOM500 Lec HW

7 Implementation Using pointers A node has a pointer to all its children Since a node may have many children, the child pointers have a linked list. A has a pointer to B, C, D each. B has pointers to E and its other child. E does not have any pointers. A BCD E

8 Addition of a child: Create the new child node Add a pointer to this child in the link list of its parent. A BCD E F Want to add new child F to B

9 Deletion of a child B: Children of B are first made children of the parent of B Node B is deleted. A BCD E A BCD E

10 A CD E

11 Deletion of the root: One of the children becomes the new root: Other children of old root become the children of the new root A BCD E C becomes new root B and D are children of C in addition to its original child

12 C BD E

13 Tree Traversal Preorder Postorder Preorder: First do the required operation with a node, then with its children Postorder: First do the required operation with the children, then return to the node.

14 Preorder Example Need to find the depth of each node in the tree representing the unix file system. Listdepth(node,depth) { print(node->name, depth) for each child C of node Listdepth(C,depth+1) }

15 Root EE220CSE260TCOM500 Lec HW Print sequence: Root, 0 EE220 1 Lec 2, HW 2, CSE260 1, Lec 2, TCOM500 1 Lec

16 Postorder Example Need to find the size of each directory in the unix file system Dirsize(node) { if (node = leaf), return (file num); size = 0 for each child C of node size  size + Dirsize( C ); print(node->name, size); }

17 Root EE220CSE260TCOM500 Lec HW 5 files 2 files Lec 3 files Print sequence: Lec: 5 HW: 2 EE220: 7 Lec: 3 CSE 260: 3 TCOM 500: 0 Root: 10

18 Binary Trees Section 4.2, Weiss A node can have at most 2 children, leftchild and rightchild A complete binary tree is one where a node can have 0 or 2 children and all leaves are at the same depth. A complete binary tree of N nodes has depth O(log N)

19 Proof Prove by induction that number of nodes at depth d is 2 d Total number of nodes of a complete binary tree of depth d is 1 + 2 + 4 +…… 2 d = 2 d+1 - 1 Thus 2 d+1 - 1 = N d = log(N + 1) - 1 What is the largest depth of a binary tree of N nodes? N

20 Inorder Travel First operate with the left subtree Then operate with the node Then operate with the right subtree Printtree(node) { if (leftchild exists) printtree(node->leftchild); print(node->element); if (rightchild exists) printtree(node->rightchild); }

21 Root EE220CSE260 Lec HW 5 files 2 files Lec 3 files Print sequence: Lec EE220 HW Root CSE 260 Lec

22 Search Tree A binary search tree is useful for searching, Section 4.3 All elements in the left subtree of a node are smaller than the element of the node, and all elements in the right subtree of a node are larger. We assume that all elements are distinct

23 5 31 1 410 5 38 1 4 Not binary Search Tree Binary Search Tree

24 Searching an element in the Tree Start from the root. Each time we encounter a node, see if the key in the node equals the element. If yes stop. If the element is less, go to the left subtree. If it is more, go to the right subtree. Conclude that the element is not in the list if we reach a leaf node and the key in the node does not equal the element.

25 Search(node, element) { If (node = NULL) conclude NOT FOUND; Else If (node.key = element) conclude FOUND; Else If (element < node.key) Search(node.leftchild, element); Else If (element > node.key) Search(node.rightchild, element); } Complexity: O(d), d is the depth For complete binary search trees: O(log N) where N is the number of nodes

26 5 38 1 410 Search for 10 Sequence Traveled: 5, 8, 10 Found! Search for 3.5 Sequence Traveled: 5, 3, 4 Not found!

27 Find Min Node = root; For (node = root; node=node.leftchild; node!=NULL) prevnode = node; Return(prevnode); Complexity: O(d)

28 5 38 1 410 Travel 5, 3, 1 Return 1;

29 Insert an element Try to find the element; If the element exists, do nothing. If it does not, insert it at the position of the returned null pointer;

30 Insert(node, element) { If (node.key = element) conclude FOUND and RETURN; Else If (element < node.key) { if (node.leftchild =NULL) insert the new node at node.leftchild; else Insert(node.leftchild, element); } Else If (element > node.key) { ?????????? } } Complexity: O(d)

31 5 38 1 410 Insert 3.5 Sequence Traveled: 5, 3, 4 Insert 3.5 as left child of 4 5 38 1 410 3.5

32 DELETION When we delete a node, we need to consider how we take care of the children of the deleted nodes. This has to be done such that the property of the search tree is maintained. Any problem if the node has no child? Any problem if the node has only one child? No, simply delete the node! No, simply replace the node with its child

33 Suppose, the node has two children. Look at the right subtree of the node (subtree rooted at the right child of the node). Find the Minimum there. Replace the key of the node to be deleted by the minimum element. Delete the minimum element. Any problem deleting it? For deletion convenience, always have a pointer from a node to its parent. Need to take care of the children of this min. element, but the min element can at most one child;

34 Pseudo Code If a node is childless, then { node->parent->ptr_to_node = NULL free node; } If a node has one child { node->parent->child = node->child; free node; } Delete(node) {

35 If a node has 2 children, { minnode = findmin(rightsubtree)->key; node->key = minnode->key; delete(minnode); } Complexity? O(d)

36 5 38 1 410 3.5 Delete 3; 3 has 2 children; Findmin right subtree for 3 returns 3.5 So 3 is replaced by 3.5, and 3.5 is deleted. 5 3.58 1 410

37 Lazy Deletion Don’t physically delete the node, but mark it as ``junk.’’ Any problem? Increases the depth of the tree. However, if the number of deleted nodes is of the same order as the number of existing nodes, then lazy deletion does not alter the order of the depth significantly.

38 AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can be attained if both subtrees of each node have roughly the same height. AVL tree is a binary search tree where the height of the two subtrees of a node differs by at most one Height of a null tree is -1

39 5 38 1 410 5 3 1 4 AVL Tree Not AVL Tree

40 Section 4.4, Weiss Suppose an AVL tree of height h contains contains at most S(h) nodes: S(h) = L(h) + R(h) + 1 L(h) is the number of nodes in left subtree R(h) is the number of nodes in right subtree You have larger number of nodes if there is larger imbalance between the subtrees This happens if one subtree has height h-1, another h-2 Thus, S(h) = S(h-1) + S(h-2) + 1

41 Using this you can show that h = O(log N) Rings a bell! Fibonacci numbers F N  N S(h)  h h is O(log N)

42 Operations in AVL Tree Searching, Complexity? FindMin, Complexity? Deletion? Insertion? O(log N)

43 Insertion Search for the element If it is not there, insert it in its place. Any problem? Insertion may imbalance the tree. Heights of two children of a node may differ by 2 after an insertion. Tree Rotations used to restore the balance.

44 If an insertion cause an imbalance, which nodes can be affected? Nodes on the path of the inserted node. Let U be the node nearest to the inserted one which has an imbalance. insertion in the left subtree of the left child of U insertion in the right subtree of the left child of U insertion in the left subtree of the right child of U insertion in the right subtree of the right child of U

45 Insertion in left child of left subtree Single Rotation U V X Y Z V U X Y Z Before Rotation After Rotation

46 5 3 1 4 Insert 0.8 AVL Tree 8 0.8 5 3 1 4 8 U V X Y Z 3 5 1 4 8 After Rotation

47 Double Rotation Suppose, imbalance is due to an insertion in the left subtree of right child Single Rotation does not work! U V A D W B C W V U A D B C Before RotationAfter Rotation

48 5 3 1 4 Insert 3.5 AVL Tree 8 3.5 5 3 1 4 8 4 5 0.8 3 3.5 After Rotation U V AW B D 8

49 Pseudocode Insert(X, T) { If (T = NULL) insert X at T; T->height = 0; If (X  T.element) { Insert(X, T ->left) If Height(T ->left) - Height(T ->right) = 2

50 Pseudocode Singlerotate routine in Fig 4.41 (Weiss) Separate for left and right nodes Doublerotate routine in Fig 4.43 (Weiss) Separate for left and right nodes

51 { If (X < T.leftchild.element) T =singleroratewithleft(T); else T =doubleroratewithleft(T); } } Else If (X>T.element) { Insert(X, T ->right) If Height(T ->right) - Height(T ->leftt) = 2 { If (X > T.righchild.element) T =singleroratewithright(T); else T =doubleroratewithright(T); } }

52 T->height = max(height(T->left), height(T->right)) + 1; Return(T); EXTENDED EXAMPLE Example in book

53 Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 3 Fig 1 3 2 Fig 2 3 2 1 Fig 3 2 1 3 Fig 4 2 1 3 4 Fig 5 2 1 3 4 5 Fig 6

54 2 1 4 5 3 Fig 7 6 2 1 4 5 3 Fig 8 4 2 5 6 1 3 Fig 9 4 2 5 6 1 3 7 Fig 10 4 2 6 7 1 3 5Fig 11

55 4 2 6 7 1 3 5 16 Fig 12 4 2 6 7 1 3 5 16 15 Fig 13 4 2 6 15 1 3 5 16 7 Fig 14

56 5 4 2 7 15 1 3 6 16 14 Fig 16 4 2 6 15 1 3 5 16 7 14 Fig 15 Continued in Book Deletions can be done with similar rotations


Download ppt "Lecture 5 Trees. Tree: Section 4.1 (Weiss) Formal Definition Tree is a sequence of nodes. There is a starting node known as root node. Every node other."

Similar presentations


Ads by Google