Problem 1 | Problem 3 |
Problem 2 | Problem 4 |
50 20 60 10 40 70 15 30 64 80 28 37 75
In-order: 10 15 20 28 30 37 40 50 60 64 70 75 80 Pre-order: 50 20 10 15 40 30 28 37 60 70 64 80 75 Post-order: 15 10 28 37 30 40 20 64 75 80 70 60 50
One possibility is
50 28 60 10 40 70 15 37 64 80 75
Other possibilities have the left subtree of 50 being
15 10 40 37 28
or
15 10 40 28 37
struct Node { int data; Node* left; Node* right; Node* parent; };
void insertAuxiliary(Node*& n, int value, Node* par) { if (n == nullptr) set n to point to a new Node whose data field is set to value, whose left and right children are null, and whose parent field is set to par. else if (value < n->data) insertAuxiliary(n->left, value, n); else insertAuxiliary(n->right, value, n); } void insert(Node*& n, int value) { insertAuxiliary(n, value, nullptr); // pass nullptr as parent of root }
7 5 6 2 1 3
7 5 6 2 1 3
6 5 3 2 1