#include<iostream> #include <cstdio> #include <cstdlib> #include <string> #define M 10 using namespace std; class Node { public: int data; struct Node* left; struct Node* right; }; Node *bstInsert(Node *root, int data); void displayTree(Node *root); void displayNode(Node *root); int main(int argc, char **argv) { int num, n = 0; freopen("in.txt","r",stdin); Node *root1 = NULL; while (scanf("%d", &num) == 1) { root1 = bstInsert(root1, num); } displayTree(root1); return 0; } Node *bstInsert(Node *root, int data) { if (root == NULL) { root = new Node(); root->data = data; root->left = root->right = NULL; } else if (data < root->data) root->left = bstInsert(root->left, data); else root->right = bstInsert(root->right, data); return root; } void displayTree(Node *root) { if (root == NULL) { return; } cout<<"Root: "<<root->data; if(root->left != NULL) { root->left->data; cout<<", Left: "<<root->left->data; } if(root->right != NULL) { cout<<", Right: "<<root->right->data; } cout<<endl; displayTree(root->left); displayTree(root->right); }
ACM Programming Solutions
This blog is developed to encourage the beginners. Leave comments if you found any bugs or if you have any suggestions or requirements......
Saturday, June 28, 2014
Binary Search Tree construction from unsorted list of integers
Friday, June 10, 2011
Subscribe to:
Posts (Atom)