Survey

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Transcript

Chapter 10 – Binary Trees Tree Structures (3 slides) Tree Node Level and Path Len. (5 slides) Binary Tree Definition Selected Samples / Binary Trees Binary Tree Nodes Binary Search Trees Locating Data in a Tree Removing a Binary Tree Node stree ADT (4 slides) Using Binary Search Trees - Removing Duplicates Update Operations (3 slides) Removing an Item From a Binary Tree (7 slides) 1 Main Index Summary Slides (5 slides) Contents Tree Structures President-CEO Production Manager Personnel Manager Purchasing Supervisor Warehouse Supervisor Sales Manager Shipping Supervisor HIERARCHICAL TREE STRUCTURE 2 Main Index Contents Tree Structures + * a / c d BINARY EXPRES S ION TREE FOR "a*b 3 Main Index e - b Contents + (c-d) / e" Tree Structures A B C E F D G (b) I J (a) A GENERAL TREE 4 Main Index Contents H Tree Node Level and Path Length Level: 0 A B C E G 5 D Level: 1 Level: 2 F H Level: 3 Main Index Contents Tree Node Level and Path Length – Depth Discussion A B C D H E I F J Complete Tree (Depth 3) 6 Main Index Contents G Tree Node Level and Path Length – Depth Discussion A B D C E F Complete Tree (Depth 2) Full with all possible nodes 7 Main Index Contents G Tree Node Level and Path Length – Depth Discussion A B D H C E I Non-Complete Tree (Depth 3) Level 2 is missing nodes 8 Main Index Contents Tree Node Level and Path Length – Depth Discussion A B D H C E F G K I Non-CompleteTree (Depth 3) Nodes at level 3 do not occurpy leftmost positions 9 Main Index Contents Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: – – 10 (a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree TL and the right subtreeTR. The nodes in T consist of node R and all the nodes in TL and TR. Main Index Contents Selected Samples of Binary Trees A A B B C C D E F G D H E I Tree B Size 5 Depth 4 Tree A Size 9 Depth 3 11 Main Index Contents A Binary Tree Nodes B C D E F H G left A right Abstract Tree Model left B right left left G left D right left E right left Main Index right right Tree Node Model 12 C Contents left H right F right Binary Search Trees 25 30 10 37 50 15 65 30 59 Binary Search Tree 1 53 62 Binary Search Tree 3 Binary Search Tree 2 13 Main Index Contents Current Node Root = 50 Action -LOCATING DATA IN A TREECompare item = 37 and 50 37 < 50, move to the left subtree Compare item = 37 and 30 37 > 30, move to the right subtree Compare item = 37 and 35 37 > 35, move to the right subtree Compare item = 37 and 37. Item Node = 30 Node = 35 Node = 37 found. 50 30 55 25 10 35 32 53 62 37 15 14 Main Index 60 Contents Removing a Binary Search Tree Node 25 // \\ 10 37 15 65 30 Delete node 25 30 37 10 10 65 15 65 15 30 Good Solution Bad Solution: 30 is out of place (b) (a) 15 37 Main Index Contents CLASS stree Constructors “d_stree.h” stree(); Create an empty search tree. stree(T *first, T *last); Create a search tree with the elements from the pointer range [first, last). CLASS stree Opertions “d_stree.h” void displayTree(int maxCharacters); Display the search tree. The maximum number of characters needed to output a node value is maxCharacters. bool empty(); Return true if the tree is empty and false otherwise. 16 Main Index Contents CLASS stree Opertions “d_stree.h” int erase(const T& item); Search the tree and remove item, if it is present; otherwise, take no action. Return the number of elements removed. Postcondition: If item is located in the tree, the size of the tree decreases by 1. void erase(iterator pos); Erase the item pointed to the iterator pos. Precondition: The tree is not empty and pos points to an item in the tree. If the iterator is invalid, the function throws the referenceError exception. Postcondition: The tree size decreases by 1. 17 Main Index Contents CLASS stree Opertions “d_stree.h” void erase(iterator first, iterator last); Remove all items in the iterator range [first, last). Precondition: The tree is not empty. If empty, the function throws the underflowError exception. Postcondition: The size of the tree decreases by the number of items in the range. iterator find(const T& item); Search the tree by comparing item with the data values in a path of nodes from the root of the tree. If a match occurs, return an iterator pointing to the matching value in the tree. If item is not in the tree, return the iterator value end(). 18 Main Index Contents CLASS stree Opertions “d_stree.h” Piar<iterator, bool> insert(const T& item); If item is not in the tree, insert it and return an iteratorbool pair where the iterator is the location of the newelement and the Boolean value is true. If item is already in the tree, return the pair where the iterator locates the existing item and the Boolean value is false. Postcondition: The size of the tree is increased by 1 if item is not present in the tree. int size(); Return the number of elements in the tree. 19 Main Index Contents Using Binary Search Trees Application: Removing Duplicates 7 v 2 20 9 3 7 3 2 5 3 2 9 3 Main Index 5 Contents v 2 3 5 7 9 Update Operations: 1st of 3 steps 1)- The function begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35. parent 25 20 t 35 12 40 (a) Step 1: Compare 32 and 25. Traverse the right subtree. 21 Main Index Contents Update Operations: 2nd of 3 steps 2)- Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35. 25 20 12 40 t (b) Step 2: Compare 32 and 35. Traverse the left subtree. 22 parent 35 Main Index Contents Update Operations: 3rd of 3 steps 1)- Create a leaf node with data value 32. Insert the new node as the left child of node 35. newNode = getSTNode(item,NULL,NULL,parent); parent->left = newNode; 25 20 12 32 (c) Step 3: Insert 32 as left child of parent 35 23 Main Index parent 35 Contents 40 Removing an Item From a Binary Tree Before After 40 40 30 P D 25 10 35 26 30 65 50 P 35 26 50 33 29 34 34 28 28 No replacement is necessary. pNodePtr->left is NULL Delete leaf node 10. pNodePtr->left is dNode 24 25 33 29 65 Main Index Contents Removing an Item From a Binary Tree Before After 40 P 30 25 10 40 65 D 26 33 29 35 R 50 P 30 25 10 65 R 33 50 34 26 34 29 28 28 Delete node 35 with only a left child: Node R is the left child. 25 Main Index Attach node R to the parent. Contents Removing an Item From a Binary Tree Before After 40 40 30 P 10 65 25 D 35 26 R P 50 33 29 30 10 25 R 28 34 65 35 29 50 33 34 28 Delete node 26 with only a right child: Node R is the right child. 26 Main Index Attach node R to the parent. Contents Removing an Item From a Binary Tree 40 40 30 65 25 10 35 26 50 25 33 29 65 35 26 10 34 34 28 Delete node 30 with two children. 27 33 29 28 50 Main Index Orphaned subtrees. Contents Removing an Item From a Binary Tree Before replacing D by R After replacing D by R 40 P pNodePtr 30 40 65 P 30 pOfRNodePtr = dNodePtr 25 D 35 26 10 R 26 29 35 R 29 33 34 28 28 R 33 10 rNodePtr 65 50 28 Main Index Contents 34 50 Removing an Item From a Binary Tree Before unlinking R P pNodePtr 40 dNodePtr 35 R 26 33 rNodePtr 26 10 R 34 28 29 D 30 65 50 pOfRNodePtr rNodePtr 33 29 40 65 R 25 10 P pNodePtr D 30 After unlinking R 28 Main Index Contents 35 29 34 50 pOfRNodePtr Removing an Item From a Binary Tree Before replacing D by R After replacing D by R P pNodePtr P pNodePtr 40 40 R R D 30 33 rNodePtr 26 10 65 35 29 rNodePtr 10 34 Contents 35 29 28 Main Index 65 26 50 28 30 33 34 50 Summary Slide 1 §- trees - hierarchical structures that place elements in nodes along branches that originate from a root. - Nodes in a tree are subdivided into levels in which the topmost level holds the root node. §- Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure. - Tree terminology with which you should be familiar: parent | child | descendents | leaf node | interior node | subtree. 31 Main Index Contents Summary Slide 2 §- Binary Trees - Most effective as a storage structure if it has high density §- ie: data are located on relatively short paths from the root. §- A complete binary tree has the highest possible density - an n-node complete binary tree has depth int(log2n). - At the other extreme, a degenerate binary tree is equivalent to a linked list and exhibits O(n) access times. 32 Main Index Contents Summary Slide 3 §- Traversing Through a Tree - There are six simple recursive algorithms for tree traversal. - The most commonly used ones are: 1)inorder (LNR) 2)postorder (LRN) 3)preorder (NLR). - Another technique is to move left to right from level to level. §- This algorithm is iterative, and its implementation involves using a queue. 33 Main Index Contents Summary Slide 4 §- A binary search tree stores data by value instead of position - It is an example of an associative container. §- The simple rules “== return” “< go left” “> go right” until finding a NULL subtree make it easy to build a binary search tree that does not allow duplicate values. 34 Main Index Contents Summary Slide 5 §- The insertion algorithm can be used to define the path to locate a data value in the tree. §- The removal of an item from a binary search tree is more difficult and involves finding a replacement node among the remaining values. 35 Main Index Contents