#include #include using namespace std; struct btnode { int value; struct btnode *left, *right; }; typedef struct btnode node; /* function declarations */ void insert(node *, node *); void bfs_traverse(node *); /*global declarations */ node *root = NULL; int val, front = 0, rear = -1, i; int queue[20]; void main() { node *New = NULL; int num = 1; cout << "Enter the elements of the tree(enter 0 to exit)\n"; while (1) { cin >> num; if (num == 0) break; New = new node; New->left = New->right = NULL; New->value = num; if (root == NULL) root = New; else { insert(New, root); } } cout << "elements in a tree in inorder are\n"; queue[++rear] = root->value; bfs_traverse(root); for (i = 0;i <= rear;i++) cout << queue[i]; cout << root->right->right->right->value; } /* inserting nodes of a tree */ void insert(node * New, node *root) { if (New->value>root->value) { if (root->right == NULL) root->right = New; else insert(New, root->right); } if (New->value < root->value) { if (root->left == NULL) root->left = New; else insert(New, root->left); } } /* displaying elements using BFS traversal */ void bfs_traverse(node *root) { val = root->value; if ((front <= rear) && (root->value == queue[front])) { if (root->left != NULL) queue[++rear] = root->left->value; if (root->right != NULL) queue[++rear] = root->right->value; front++; } if (root->left != NULL) { bfs_traverse(root->left); } if (root->right != NULL) { bfs_traverse(root->right); } }