Wyo Data Structures Ch. 6 Worksheet #6 |
Name - |
Write a program that could be used as a driver for binary tree functions. You are not creating a binary tree class and you will not need helper (auxiliary) functions. The functions should be recursive where applicable. Note that some functions have been given.
#include <iostream.h>struct node { int data; node * left; node * right; }; bool insert(node * &root, const int &data); bool remove(node * &root, const int &data); node * find(node * root, const int &target); void destroy(node * root); void traverseInorder(node *root); void traversePreorder(node *root); void traversePostorder(node *root); int main() { node *root = 0; insert(root, 5); insert(root, 2); insert(root, 7); insert(root, 3); cout << "Preorder: " << endl; traversePreorder(root); return 0; } void destroy(node * root) { } node * find(node * root, const int &target) { } bool insert(node * &root, const int &data) { if (!root) { node * newNode = new node; if (!newNode) return false; newNode->data = data; newNode->left = 0; newNode->right = 0; root = newNode; return true; } if (data == root->data) return true; else if (data < root->data) return insert(root->left, data); else return insert(root->right, data); } void visit(node *myNode) { cout << myNode->data << ' '; } void traversePreorder(node *root) { if (!root) return; visit(root); traversePreorder(root->left); traversePreorder(root->right); } void traversePostorder(node *root) { } void traverseInorder(node *root) { } bool remove(node * &root, const int &data) { }