Advertisement
gpric001

CS 12 SI w8

Mar 3rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. //Binary node structure
  6. struct BinaryNode {
  7.     int data;
  8.     BinaryNode* left;
  9.     BinaryNode* right;
  10.     BinaryNode(int data) : data(data), left(0), right(0) {}
  11. };
  12.  
  13. //Generate a binary tree given a vector of integers. Return the root of the tree
  14. BinaryNode* generateBinaryTree(vector<int> data);
  15.  
  16. //Add a single element to a binary tree if it doesn't exist in the tree already.
  17. //Return true if successfully added and false otherwise
  18. bool addToTree(BinaryNode* root, int data);
  19.  
  20. //Returns a pointer to a node withe a given data value, otherwise return a
  21. //null pointer.
  22. BinaryNode* findElement(BinaryNode* root, int data);
  23.  
  24. //Output nodes in the order of: left subtree, current node, right subtree.
  25. void inOrderTraversal(BinaryNode* root);
  26.  
  27. //Reverses a binary tree
  28. void reverseTree(BinaryNode* root);
  29.  
  30.  
  31. int main() {
  32. //---------------------------------------------------------------------------
  33.     //Problem 1: Draw a diagram that shows the structure of the tree generated
  34.     //           below.
  35.     vector<int> v {5, 2, 8, 1, 3, 6, 9, 2};
  36.     BinaryNode* root = generateBinaryTree(v);
  37. //---------------------------------------------------------------------------
  38.  
  39. //---------------------------------------------------------------------------
  40.     //Problem 2: Complete the implementation of the function findElement
  41.     //           so that the following code works correctly.
  42.    
  43.     cout << "PROBLEM 2 OUTPUT" << endl;
  44.    
  45.     //The following should output: "NULL 1 2 3 NULL 5 6 NULL 8 9"
  46.     for (int i = 0; i < 10; ++i) {
  47.         BinaryNode* elem = findElement(root, i);
  48.         if (elem) {
  49.             cout << elem->data << ' ';
  50.         }
  51.         else {
  52.             cout << "NULL" << ' ';
  53.         }
  54.     }
  55.     cout << endl << endl;
  56. //---------------------------------------------------------------------------
  57.  
  58. //---------------------------------------------------------------------------
  59.     //Problem 3: Implement an "in order traversal." What that means is that our
  60.     //           algorithm will visit every node by first visiting the left node
  61.     //           then the root node, then the right node. To test if it works
  62.     //           try printing out the nodes in the order your algorithm visits
  63.     //           them.
  64.    
  65.     cout << "PROBLEM 3 OUTPUT" << endl;
  66.    
  67.     //The following code should output: 1 2 3 5 6 8 9
  68.     inOrderTraversal(root);
  69.    
  70.     cout << endl << endl;
  71. //---------------------------------------------------------------------------
  72.  
  73. //---------------------------------------------------------------------------
  74.     //Challenge Problem: The following problem is often asked as an interview
  75.     //                   question: Given a binary tree, write a function to
  76.     //                   reverse the binary tree. Below is a simple illustration
  77.     //                   of what it means to reverse a binary tree.
  78.    
  79.     //           1                             1
  80.     //         /   \                         /   \
  81.     //        2     3           =>          3     2
  82.     //       / \   / \                     / \   / \
  83.     //      4   5 6   7                   7   6 5   4
  84.    
  85.     cout << "CHALLENGE PROBLEM OUTPUT" << endl;
  86.    
  87.     //The following code should output:
  88.     //    9 8 6 5 3 2 1
  89.     //    1 2 3 5 6 8 9
  90.    
  91.     for (int i = 0; i < 2; ++i) {
  92.         reverseTree(root);
  93.         inOrderTraversal(root);
  94.         cout << endl;
  95.     }
  96. //---------------------------------------------------------------------------
  97.  
  98.     return 0;
  99. }
  100.    
  101. BinaryNode* generateBinaryTree(vector<int> data) {
  102.     BinaryNode* head = new BinaryNode(data.at(0));
  103.     for(int i = 1; i < data.size(); ++i) {
  104.         addToTree(head, data.at(i));
  105.     }
  106.     return head;
  107. }
  108.  
  109. bool addToTree(BinaryNode* currentNode, int data) {
  110.     if (data < currentNode->data) {
  111.         if (currentNode->left) {
  112.             return addToTree(currentNode->left, data);
  113.         }
  114.         currentNode->left = new BinaryNode(data);
  115.         return true;
  116.     }
  117.     if (data > currentNode->data) {
  118.         if(currentNode->right) {
  119.             return addToTree(currentNode->right, data);
  120.         }
  121.         currentNode->right = new BinaryNode(data);
  122.         return true;
  123.     }
  124.     return false;
  125. }
  126.  
  127. //Stub for Problem 2
  128. BinaryNode* findElement(BinaryNode* root, int data) {
  129.     return 0;
  130. }
  131.  
  132. //Stub for Problem 3
  133. void inOrderTraversal(BinaryNode* root) {
  134.     return;
  135. }
  136.  
  137. //Stub for Challenge Problem
  138. void reverseTree(BinaryNode* root) {
  139.     return;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement