Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int keyValue;
- Node *left;
- Node *right;
- };
- class BinaryTree
- {
- public:
- BinaryTree();
- ~BinaryTree();
- void insert(int key);
- void destroyTree();
- int size(Node *leaf);
- int maxDepth(Node *leaf);
- int height(Node *leaf);
- void printTree(Node *leaf);
- void printPostorder(Node *leaf);
- // private ?!
- void destroyTree(Node *leaf);
- void insert(int key, Node *leaf);
- Node *root;
- };
- BinaryTree::BinaryTree()
- {
- root = NULL;
- }
- BinaryTree::~BinaryTree()
- {
- destroyTree();
- }
- void BinaryTree::destroyTree(Node *leaf)
- {
- if(leaf != NULL)
- {
- destroyTree(leaf->left);
- destroyTree(leaf->right);
- delete leaf;
- }
- }
- void BinaryTree::insert(int key, Node *leaf)
- {
- if(key < leaf->keyValue)
- {
- if(leaf->left != NULL) insert(key, leaf->left);
- else
- {
- leaf->left = new Node;
- leaf->left->keyValue = key;
- leaf->left->left = NULL; //Sets the left child of the child node to null
- leaf->left->right = NULL; //Sets the right child of the child node to null
- }
- }
- else if(key >= leaf->keyValue)
- {
- if(leaf->right!=NULL) insert(key, leaf->right);
- else
- {
- leaf->right = new Node;
- leaf->right->keyValue = key;
- leaf->right->left = NULL; //Sets the left child of the child node to null
- leaf->right->right = NULL; //Sets the right child of the child node to null
- }
- }
- }
- int BinaryTree::size(Node *leaf)
- {
- if (leaf == NULL)
- {
- return(0);
- }
- else
- {
- return(size(leaf->left) + 1 + size(leaf->right));
- }
- }
- // computes the maxDepth
- int BinaryTree::maxDepth(Node* leaf)
- {
- if (leaf == NULL) return(0);
- else
- {
- // depth of each subtree
- int leftDepth = maxDepth(leaf->left);
- int rightDepth = maxDepth(leaf->right);
- // use the larger one
- if (leftDepth > rightDepth) return(leftDepth + 1);
- else return(rightDepth + 1);
- }
- }
- int BinaryTree::height(Node *leaf)
- {
- int heightOne = 0;
- int heightTwo = 0;
- if (leaf == NULL) return 0;
- if (leaf->left) heightOne = height(leaf->left);
- if (leaf->right) heightTwo = height(leaf->right);
- int h = max(heightOne, heightTwo) + 1;
- return h;
- }
- void BinaryTree::printTree(Node *leaf)
- {
- if (leaf == NULL) return;
- printTree(leaf->left);
- cout << " " << leaf->keyValue;
- printTree(leaf->right);
- }
- void BinaryTree::printPostorder(Node *leaf)
- {
- if (leaf == NULL) return;
- // recursive call on both subtrees
- printPostorder(leaf->left);
- printPostorder(leaf->right);
- // then the node itself
- cout << " " << leaf->keyValue;
- }
- void BinaryTree::insert(int key)
- {
- if(root != NULL)
- insert(key, root);
- else
- {
- root = new Node;
- root->keyValue = key;
- root->left = NULL;
- root->right = NULL;
- }
- }
- void BinaryTree::destroyTree()
- {
- destroyTree(root);
- }
- int main()
- {
- BinaryTree exampleTree;
- // inserting elements
- for (int i = 1; i <= 10; i++)
- {
- exampleTree.insert(i);
- }
- cout << "Size: " << exampleTree.size(exampleTree.root) << endl;
- cout << "Height: " << exampleTree.height(exampleTree.root) << endl;
- cout << "Depth: " << exampleTree.maxDepth(exampleTree.root) << endl;
- cout << "Print: ";
- exampleTree.printTree(exampleTree.root);
- cout << endl;
- cout << "Postorder: ";
- exampleTree.printPostorder(exampleTree.root);
- cout << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement