1. Trang chủ
  2. » Công Nghệ Thông Tin

Data Structures and Algorithms - Chapter 7 -Tree pptx

88 425 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 88
Dung lượng 0,99 MB

Nội dung

Chapter - Tree Basic tree concepts Binary trees Binary Search Tree (BST) 10 Delete node from BST • Node having both subtrees 74 Delete node from BST Auxiliary functions for Insert: recursive_Delete iterative_Delete 75 Recursive Delete recursive_Delete (ref subroot , val key ) Deletes a node from a BST Pre subroot is NULL or points to the root of a subtree Key contains value needs to be removed from BST Post If key is found, it will be removed from BST Return notFound or success Uses recursive_Delete and RemoveNode functions 76 Recursive Delete (cont.) recursive_Delete (ref subroot , val key ) if (subroot is NULL) return notFound else if (key < subroot->data.key) return recursive_Delete(subroot->left, key) else if (key > subroot->data.key) return recursive_Delete(subroot->right, key) else RemoveNode(subroot) return success End recursive_Delete 77 Delete Node from BST Delete (val key ) Deletes a node from a BST Pre subroot is NULL or points to the root of a subtree Key contains value needs to be removed from BST Post If key is found, it will be removed from BST Return notFound or success Uses recursive_Delete and RemoveNode functions return recursive_Delete (root, key) End Delete 78 Auxiliary Function RemoveNode RemoveNode (ref subroot , val key ) pDel = subroot // remember node to delete at end if (subroot ->left is NULL) // leaf node or node having only right subtree subroot = subroot->right // (a) and (b) else if (subroot->right is NULL) // node having only left subtree subroot = subroot->left subroot pDel subroot pDel (a) key needs to be deleted = 18 (b) 79 Auxiliary Function RemoveNode RemoveNode (ref subroot , val key ) pDel = subroot // remember node to delete at end if (subroot ->left is NULL) // leaf node or node having only right subtree subroot = subroot->right // (a) and (b) else if (subroot->right is NULL) // node having only left subtree subroot = subroot->left subroot pDel subroot pDel (a) key needs to be deleted = 18 key needs to be deleted = 18 (b) 80 Auxiliary Function RemoveNode RemoveNode (ref subroot , val key ) pDel = subroot // remember node to delete at end if (subroot ->left is NULL) // leaf node or node having only right subtree subroot = subroot->right else if (subroot->right is NULL) // node having only left subtree subroot = subroot->left // (c) subroot pDel key needs to be deleted = 44 (c) 81 Auxiliary Function RemoveNode RemoveNode (ref subroot , val key ) pDel = subroot // remember node to delete at end if (subroot ->left is NULL) // leaf node or node having only right subtree subroot = subroot->right else if (subroot->right is NULL) // node having only left subtree subroot = subroot->left // (c) subroot pDel key needs to be deleted = 44 (c) 82 Auxiliary Function RemoveNode (cont.) else // node having both subtrees (d) parent = subroot pDel = parent ->left // move left to find the predecessor loop (pDel->right is not NULL) // pDel is not the predecessor parent = pDel key needs to be deleted = 23 pDel = pDel->right subroot->data = pDel->data subroot parent if (parent = subroot) parent->left = pDel->left pDel else parent->right = pDel->left recycle pDel End RemoveNode (d) 83 Auxiliary Function RemoveNode (cont.) else // node having both subtrees (d) parent = subroot pDel = parent ->left // move left to find the predecessor loop (pDel->right is not NULL) // pDel is not the predecessor parent = pDel key needs to be deleted = 23 pDel = pDel->right subroot->data = pDel->data subroot if (parent = subroot) parent->left = pDel->left parent else parent->right = pDel->left pDel recycle pDel return success End RemoveNode (d) 84 Auxiliary Function RemoveNode (cont.) else // node having both subtrees (d) parent = subroot pDel = parent ->left // move left to find the predecessor loop (pDel->right is not NULL) // pDel is not the predecessor parent = pDel key needs to be deleted = 23 pDel = pDel->right subroot->data = pDel->data subroot if (parent = subroot) parent->left = pDel->left else parent->right = pDel->left parent recycle pDel return success End RemoveNode pDel (d) 85 Auxiliary Function RemoveNode (cont.) else // node having both subtrees (d) parent = subroot pDel = parent ->left // move left to find the predecessor loop (pDel->right is not NULL) // pDel is not the predecessor parent = pDel key needs to be deleted = 23 pDel = pDel->right subroot->data = pDel->data subroot if (parent = subroot) parent->left = pDel->left else parent->right = pDel->left parent recycle pDel return success End RemoveNode pDel 22 (d) 86 Auxiliary Function RemoveNode (cont.) else // node having both subtrees (d) parent = subroot pDel = parent ->left // move left to find the predecessor loop (pDel->right is not NULL) // pDel is not the predecessor parent = pDel key needs to be deleted = 23 pDel = pDel->right subroot->data = pDel->data subroot if (parent = subroot) 22 parent->left = pDel->left else parent->right = pDel->left parent recycle pDel return success End RemoveNode pDel 22 (d) 87 Auxiliary Function RemoveNode (cont.) else // node having both subtrees (d) parent = subroot pDel = parent ->left // move left to find the predecessor loop (pDel->right is not NULL) // pDel is not the predecessor parent = pDel key needs to be deleted = 23 pDel = pDel->right subroot->data = pDel->data subroot if (parent = subroot) 22 parent->left = pDel->left else parent->right = pDel->left parent recycle pDel return success pDel End RemoveNode (d) 88 ... not NULL) AND (subroot- >data. key target) if (target < subroot- >data. key) subroot = subroot->left else subroot = subroot->right return subroot End iterative_Search Target = 22 subroot 47 Search... (refOperation(ref Data )) inOrderTraverse (refOperation(ref Data )) postOrderTraverse (refOperation(ref Data )) • Breadth-First Traverse:... recursive_postOrder(subroot->left) recursive_postOrder(subroot->right) Operation(subroot- >data) End recursive_postOrder 30 Depth-First Traversal preOrderTraverse (refOperation(ref Data ))

Ngày đăng: 15/03/2014, 17:20

TỪ KHÓA LIÊN QUAN