Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- class node {
- public:
- int data;
- node* left;
- node* right;
- node(int value) {
- data = value;
- left = NULL;
- right = NULL;
- }
- };
- node* add(node*, int);
- void inorder(node*);
- void anotherinorder(node*);
- int main() {
- int temp;
- queue<int> q;
- srand(time(NULL));
- for (int h = 0; h < 15; h++) {
- temp = rand() % 99 + 1; //limit random numbers from 1-99
- q.push(temp); //store on queue to use later
- }
- node* root = new node(q.front());
- q.pop();
- while (!q.empty()) {
- add(root, q.front());
- q.pop();
- }
- cout << " Random BST values in Inorder: ";
- inorder(root);
- cout << "\n Nodes that has no Left child valuea are: ";
- anotherinorder(root);
- return 0;
- }
- node* add(node* root, int value) {
- if (root == NULL) {
- root = new node(value);
- }
- else if (value <= root->data) {
- root->left = add(root->left, value);
- }
- else if (value > root->data) {
- root->right = add(root->right, value);
- }
- return root;
- }
- void inorder(node* nnode) {
- if (nnode == NULL) {
- return;
- }
- inorder(nnode->left);
- cout << nnode->data << ' ';
- inorder(nnode->right);
- }
- void anotherinorder(node* nnode) {
- if (nnode == NULL) {
- return;
- }
- anotherinorder(nnode->left);
- if (nnode->left == NULL) { //another inorder function but has the cout-if-left-is-null proccess
- cout << nnode->data << ' ';
- }
- anotherinorder(nnode->right);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement