Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- int counterNodes = 0;
- int counterLeaves = 0;
- struct Node {
- struct Node* lchild;
- int data;
- struct Node* rchild;
- }* root = NULL;
- void Insert(int key)
- {
- struct Node *p;
- struct Node *t = root;
- struct Node *r = NULL;
- if(root == NULL)
- {
- p = new Node;
- p->data = key;
- p->rchild = p->lchild = NULL;
- root = p;
- return;
- }
- while(t!=NULL)
- {
- if(key<t->data)
- t = t->lchild;
- if(key>t->data)
- t = t->rchild;
- else
- return;
- }
- p = new Node;
- p->data = key;
- p->rchild=p->lchild = NULL;
- if(key < r->data)
- {
- r->lchild = p;
- }
- if(key > r->data)
- {
- r->rchild = p;
- }
- }
- void Inorder(struct Node* p)
- {
- if (p) {
- Inorder(p->lchild);
- printf("%d ", p->data);
- Inorder(p->rchild);
- }
- }
- struct Node* Search(int key)
- {
- struct Node* t = root;
- while (t != NULL) {
- if (key == t->data)
- return t;
- else if (key < t->data)
- t = t->lchild;
- else
- t = t->rchild;
- }
- return NULL;
- }
- struct Node* RInsert(struct Node* p, int key)
- {
- struct Node* t = NULL;
- if (p == NULL) {
- t = new Node;
- t->data = key;
- t->lchild = t->rchild = NULL;
- return t;
- }
- if (key < p->data)
- p->lchild = RInsert(p->lchild, key);
- else if (key > p->data)
- p->rchild = RInsert(p->rchild, key);
- return p;
- }
- // 01. Procedure for counting nodes
- void countNodesProcedure(struct Node* p, int &counter)
- {
- if(p)
- {
- ++counter;
- countNodesProcedure(p->rchild, counter);
- countNodesProcedure(p->lchild, counter);
- }
- }
- // 02. Function for counting nodes
- int countNodesFunction(struct Node *p)
- {
- int nodesCounter = 1; //Node itself should be counted
- if (p == NULL)
- return 0;
- else
- {
- nodesCounter += countNodesFunction(p->lchild);
- nodesCounter += countNodesFunction(p->rchild);
- return nodesCounter;
- }
- }
- int countLeavesFunction(struct Node *p)
- {
- if(p==NULL) {
- return 0;
- }
- if(p->lchild==NULL && p->rchild==NULL)
- {
- return 1;
- }
- return countLeavesFunction(p->lchild) + countLeavesFunction(p->rchild);
- }
- void countLeavesProcedure(struct Node *p)
- {
- if(p)
- {
- if(p->rchild == NULL && p->lchild == NULL)
- {
- counterLeaves++;
- }
- countLeavesProcedure(p->lchild);
- countLeavesProcedure(p->rchild);
- }
- }
- // 05. Function for returning the height of a tree
- int height(struct Node *p)
- {
- if(p)
- {
- return 1+ max(height(p->rchild),height(p->lchild));
- }
- return 0;
- }
- // 06. Show path bool check
- int existCheck(int key) {
- struct Node* t = root;
- static int counter = 0;
- while (t != NULL) {
- if (key == t->data)
- return 1;
- else if (key < t->data)
- {
- counter++;
- t = t->lchild;
- }
- else
- {
- counter++;
- t = t->rchild;
- }
- }
- return 0;
- }
- // 06. Show Path
- void showPathOfKey(int key) {
- if(!existCheck(key))
- {
- cout << "The node with value " << key << " does not exist." << endl;
- return;
- }
- struct Node* t = root;
- static int counter = 0;
- while (t != NULL) {
- if (key == t->data)
- {
- cout << t->data << endl;
- return;
- }
- else if (key < t->data)
- {
- cout << t->data << endl;
- t = t->lchild;
- }
- else
- {
- cout << t->data << endl;
- t = t->rchild;
- }
- }
- return;
- }
- int main()
- {
- struct Node* temp;
- root = RInsert(root, 50);
- RInsert(root, 10);
- RInsert(root, 60);
- RInsert(root,70);
- /*
- RInsert(root, 40);
- RInsert(root, 70);
- RInsert(root, 10);3
- */
- // Inorder(root);
- cout << endl;
- // 01. Procedure for counting nodes with &
- /*
- int nodesCounterProcedure = 0;
- countNodesProcedure(root, nodesCounterProcedure);
- cout << "01. (procedure) The number of nodes is: " << nodesCounterProcedure << endl;
- */
- // --- TODO!!!!!!!!!!!! - check 06. make it with recursion
- // 02. Function for counting nodes
- // cout << "02. (function) The number of nodes is: " << countNodesFunction(root) << endl;
- // 03. Count leaves of a tree function
- // cout << "03. (function) The amount of leaves is: " << countLeavesFunction(root) << endl;
- // 04. Count leaves of a tree procedure
- // countLeavesProcedure(root);
- // cout << "04. (procedure) The amount of leaves is " << counterLeaves << endl;
- // 05. Function for returning the height of the tree
- // cout <<"05. (function) The height of the tree is " << height(root) << endl;
- // 06. Procedure WITH FUNCTION for showing the path of the binary tree.
- // showPathOfKey(70);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement