1 / 18

Lab 4

Lab 4. Due date: March 29. Linked Representation. Each binary tree node is represented as an object whose data type is binaryTreeNode . The space required by an n node binary tree is n * (space required by one node). The Struct binaryTreeNode. template <class T> struct binaryTreeNode

Download Presentation

Lab 4

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. Lab 4 • Due date: March 29

  2. Linked Representation • Each binary tree node is represented as an object whose data type is binaryTreeNode. • The space required by an n node binary tree is n * (space required by one node).

  3. The StructbinaryTreeNode template <class T> structbinaryTreeNode { T element; binaryTreeNode<T> *leftChild, *rightChild; binaryTreeNode() {leftChild = rightChild = NULL;} // other constructors come here };

  4. root a c b d f e g h leftChild element rightChild Linked Representation Example

  5. // create a binary tree with root x binaryTreeNode<int> *x, *y, *z; y = new binaryTreeNode<int> (2); z = new binaryTreeNode<int> (3); x = new binaryTreeNode<int> (1, y, z);

  6. visit void visit(binaryTreeNode<T> *x) {// visit node *x, just output element field. cout << x->element << ' '; treeSize++; }

  7. Binary Tree Traversal Methods • Preorder • Inorder • Postorder • Level order

  8. a b c Preorder Example (visit = print) a b c

  9. Preorder Traversal template <class T> void preOrder(binaryTreeNode<T> *t) { if (t != NULL) { visit(t); preOrder(t->leftChild); preOrder(t->rightChild); } }

  10. a b c Inorder Example (visit = print) b a c

  11. Inorder Traversal template <class T> voidinOrder(binaryTreeNode<T> *t) { if (t != NULL) { inOrder(t->leftChild); visit(t); inOrder(t->rightChild); } }

  12. a b c Postorder Example (visit = print) b c a

  13. Postorder Traversal template <class T> voidpostOrder(binaryTreeNode<T> *t) { if (t != NULL) { postOrder(t->leftChild); postOrder(t->rightChild); visit(t); } }

  14. Level Order Let t be the tree root. while (t != NULL) { visit t and put its children on a FIFO queue; if FIFO queue is empty, set t = NULL; otherwise, pop a node from the FIFO queue and call it t; }

  15. queue<binaryTreeNode<T>*> q; while (t != NULL) { visit(t); // visit t // put t's children on queue if (t->leftChild != NULL) q.push(t->leftChild); if (t->rightChild != NULL) q.push(t->rightChild); // get next node to visit t = q.front(); if(!q.empty()) q.pop(); }

  16. a b c f e d j g h i Level-Order Example (visit = print) a b c d e f g h i j

  17. Practice create a tree as below, and test your code.

  18. Implement functions • Preorder traversal • In-order traversal • Post-order traversal • Level-order traversal • And test your program using previous example

More Related