1 / 29

Main Index

CSE 331 – Lecture 15. 1. Main Index. Contents. Chapter 10 Binary Tree Algorithms Traversals Inorder Preorder Postorder Level-order Using binary trees Find Tree height Find # of nodes Deleting a tree. Binary Search Tree stree ADT Using Binary Search Trees

dafydd
Download Presentation

Main Index

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 331 – Lecture 15 1 Main Index Contents • Chapter 10 • Binary Tree Algorithms • Traversals • Inorder • Preorder • Postorder • Level-order • Using binary trees • Find Tree height • Find # of nodes • Deleting a tree • Binary Search Tree • stree ADT • Using Binary Search Trees • - Removing Duplicates • Iterators (next time) • Summary Slides

  2. Tree Node Class template <typename T> class tnode { public: // public data simplifies building class functions T nodeValue; tnode<T> *left, *right; // default constructor. data not initialized tnode() {} // initialize the data members tnode (const T& item, tnode<T> *lptr = NULL, tnode<T> *rptr = NULL) : nodeValue(item), left(lptr), right(rptr) {} };

  3. 3 Main Index Contents Binary Tree Nodes

  4. 4 Main Index Contents Selected Samples of Binary Trees

  5. A B C D E F G H I Tree A Size 9 Depth 3 5 Main Index Contents Traversals Inorder: D B H E I A F C G Postorder: D H I E B F G C A Preoder: A B D E H I C F G Level-Order: A B C D E F G H I

  6. Inorder Traversal Recursive Algorithm (1) travers left subtree (2) process root (3) traverse right subtree // version to output values in tree “inorder” template <typename T> void inorderOutput(tnode<T> *t, const string& separator = " ") { if (t != NULL) { inorderOutput(t->left, separator); // descend left cout << t->nodeValue << separator; // output the node inorderOutput(t->right, separator); // descend right } }

  7. Postorder Traversal Recursive Algorithm (1) travers left subtree (2) traverse right subtree (3) process root // version to output values in tree “postorder” template <typename T> void postorderOutput(tnode<T> *t, const string& separator = “ ") { if (t != NULL) { postorderOutput(t->left, separator); // descend left postorderOutput(t->right, separator); // descend right cout << t->nodeValue << separator; // output the node } }

  8. Preorder Traversal Recursive Algorithm (1) process root (2) travers left subtree (3) traverse right subtree // version to output values in tree “preorder” template <typename T> void preorderOutput(tnode<T> *t, const string& separator = “ ") { if (t != NULL) { cout << t->nodeValue << separator; // output the node postorderOutput(t->left, separator); // descend left postorderOutput(t->right, separator); // descend right } }

  9. Level-order Traversal template <typename T> void levelorderOutput(tnode<T> *t, const string& separator = " ") { // store siblings of each node in a queue so that they are // after parent, and in order for next level of tree queue<tnode<T> *> q; tnode<T> *p; q.push(t); // start by pushing root (t) onto queue while(!q.empty()) { p = q.front(); // process front queue node q.pop(); cout << p->nodeValue << separator; if(p->left != NULL) q.push(p->left); // push left child in queue if(p->right != NULL) q.push(p->right); // push right child on queue } }

  10. Level-order traversal Similar to breadth-first search Traverses each level of the tree, in turn, from left to right Algorithm Push root on queue While queue not empty pop node from queue process node push left and right child of node onto queue

  11. Node Density Full Tree (depth k) 2k+1 -1 nodes 2k - 1 interior nodes 2k leaves Complete tree with n nodes depth is log2n Node count inequality 2k <= n < 2k+1 leads to k <= log2n < k+1

  12. Counting Leaves Algorithm (different from text) if tree is empty return 0 else if root has no children return 1 else return leaves(TL) + leaves(TR) Full Tree (depth n) has 2n+1 -1 nodes and 2n leaves

  13. Counting Leaves template <typename T> int countLeaf (tnode<T> *t) { if (t == NULL) return 0; else if (t != NULL) { // if t is a leaf node (no children), it counts. if (t->left == NULL && t->right == NULL) return 1; return (countLeaf(t->left + countLeaf(t->right); } }

  14. Tree Depth (Height) Tree height is 1 + MAX(depth(TL),depth(TR)) Algorithm If tree is empty return -1 else return 1 + MAX(depth(TL),depth(TR))

  15. Depth (Height) int depth (tnode<T> *t) { int depthLeft, depthRight, depthval; if (t == NULL) depthval = -1; // depth of an empty tree is -1 else { depthLeft= depth(t->left); depthRight= depth(t->right); // depth is 1 + maximum subtree depth depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); } return depthval; }

  16. Deleting a tree Algorithm if tree not empty delete both subtrees delete root template <typename T> void deleteTree(tnode<T> *t) { // postorder delete left and right subtrees of t // and then delete node t if (t != NULL) { deleteTree(t->left); deleteTree(t->right); delete t; } }

  17. Binary Search Tree Definition • A binary search tree T is • (a) a binary tree, and • (b) each and every internal node R and its children (CL and CR), if they exist, have values such that valueCL < valueR <= valueCR

  18. 18 Main Index Contents Binary Search Trees

  19. 19 Main Index Contents Current NodeAction-LOCATING DATA IN A TREE- Root = 50 Compare item = 37 and 50 37 < 50, move to the left subtree Node = 30 Compare item = 37 and 30 37 > 30, move to the right subtree Node = 35 Compare item = 37 and 35 37 > 35, move to the right subtree Node = 37 Compare item = 37 and 37. Item found.

  20. stree(); Create an empty search tree. CLASS stree CLASS stree “d_stree.h” “d_stree.h” Constructors Opertions stree(T *first, T *last); Create a search tree with the elements from the pointer range [first, last). 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. 20 Main Index Contents

  21. 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. CLASS stree “d_stree.h” Opertions 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. 21 Main Index Contents

  22. 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. CLASS stree “d_stree.h” Opertions 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(). 22 Main Index Contents

  23. Piar<iterator, bool> insert(const T& item); If item is not in the tree, insert it and return an iterator- bool 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. CLASS stree “d_stree.h” Opertions int size(); Return the number of elements in the tree. 23 Main Index Contents

  24. Removing duplicates • Since binary search tree does not insert duplicates, a vector can be purged of duplicates by … • For each element in vector • Insert element in binary search tree • Clear vector • For each element in binary search tree • Append element to vector • Clear binary search tree • If tree traversed “inorder” this results in a sort of the vector as well

  25. 25 Main Index Contents Using Binary Search Trees Application: Removing Duplicates

  26. 26 Main Index Contents 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.

  27. 27 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.

  28. 28 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.

  29. 29 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.

More Related