bst.cpp - #include <iostream> using namespace std struct Node int data Node left*right Node makeTree(int data Node*left = nullptr Node*right = nullptr

bst.cpp - #include <iostream> using namespace std struct...

This preview shows page 1 - 3 out of 3 pages.

#include <iostream> using namespace std; struct Node{ int data; Node* left, *right; }; Node* makeTree(int data, Node *left = nullptr, Node *right = nullptr) { Node *temp = new Node; temp->left = left; temp->right = right; temp->data = data; return temp; } int getSmallest(Node *r) { if (r->left == nullptr) return r->data; return getSmallest(r->left); } Node *removeSmallest(Node *r) { if(r->left == nullptr) { Node *temp = r; r = r->right; delete temp; return r; } r->left = removeSmallest(r->left); return r; } void printTree(Node * r, int offset = 4) { char c = r == nullptr ? '.' : '-'; for (int i = 0; i < offset - 4; i++) cout << ' '; for (int i = 0; i < 4; i++) cout << c; if (r != nullptr) { cout << r->data << endl; offset += 4; printTree(r->left, offset); printTree(r->right, offset); } else cout << '.' << endl; } // Return a pointer to the node that will be // the parent of a new node containing v, // or return nullptr if v is already in tree. // This function is complete. There is no code to add. Node * findInsertionPoint(Node *r, int v) { if (r->data == v) return nullptr; if (v < r->data) { if (r->left == nullptr) return r; return findInsertionPoint(r->left, v); }
Image of page 1

Subscribe to view the full document.

else { if (r->right == nullptr) return r;
Image of page 2
Image of page 3
  • Spring '14
  • Void type, Carriage return, Tree traversal, Nested set model

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes