Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class BST {
- struct node {
- int data;
- node* left;
- node* right;
- };
- node* root;
- node* insert(int x, node* t)
- {
- if(t == NULL)
- {
- t = new node;
- t->data = x;
- t->left = t->right = NULL;
- }
- else if(x < t->data)
- t->left = insert(x, t->left);
- else if(x > t->data)
- t->right = insert(x, t->right);
- return t;
- }
- void inorder(node* t) {
- if(t == NULL)
- return;
- inorder(t->left);
- cout << t->data << " ";
- inorder(t->right);
- }
- void Postorder(node* t) {
- if(t == NULL)
- return;
- inorder(t->left);
- inorder(t->right);
- cout << t->data << " ";
- }
- void Preorder(node* t) {
- if(t == NULL)
- return;
- cout << t->data << " ";
- inorder(t->left);
- inorder(t->right);
- }
- public:
- BST() {
- root = NULL;
- }
- void insert(int x) {
- root = insert(x, root);
- }
- void display(int i) {
- if(i==1)
- inorder(root);
- if(i==2)
- Preorder(root);
- if(i==3)
- Postorder(root);
- cout << endl;
- }
- };
- int main() {
- BST t;
- int a;
- char s[13]="Danci Daniel";
- //for(int i=1;i<13;i++){
- //t.insert((int)s[i]);
- //t.insert(i);
- //}
- t.insert(3);
- t.insert(2);
- t.insert(7);
- t.insert(1);
- t.insert(8);
- t.insert(5);
- t.insert(4);
- t.insert(9);
- t.insert(6);
- t.display(1);
- t.display(2);
- t.display(3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement