Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node *left;
- Node *right;
- Node() {;}
- Node(int constData)
- {
- data = constData;
- left = NULL;
- right = NULL;
- }
- };
- void addElement(Node *&root, int myData)
- {
- if(root == NULL)
- {
- root = new Node(myData);
- return;
- }
- else if(myData < root->data)
- addElement(root->left, myData);
- else
- addElement(root->right, myData);
- }
- void preOrderPrint(Node *root)
- {
- if(root != NULL)
- {
- cout << root->data << " ";
- preOrderPrint(root->left);
- preOrderPrint(root->right);
- }
- }
- void postOrderPrint(Node *root)
- {
- if(root != NULL)
- {
- preOrderPrint(root->left);
- preOrderPrint(root->right);
- cout << root->data << " ";
- }
- }
- void inOrderPrint(Node *root)
- {
- if(root != NULL)
- {
- preOrderPrint(root->left);
- cout << root->data << " ";
- preOrderPrint(root->right);
- }
- }
- int countNodes(Node *root)
- {
- if(root == NULL)
- {
- cout << "Empty tree\n";
- return 0;
- }
- else
- {
- static int count = 1;
- count += countNodes(root->left);
- count += countNodes(root->right);
- return count;
- }
- }
- int main(void)
- {
- Node *root;
- root = NULL;
- addElement(root, 100);
- addElement(root, 90;
- addElement(root, 110);
- inOrderPrint(root);
- cout << countNodes(root) << "\n";
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement