/* Tanzila Islam CSE, Southeast University. Dhaka-Bangladesh. id : https://www.facebook.com/prieontypurnotamohita mail: tanzilamohita@gmail.com blog: http://tanzilamohita.blogspot.com/ */ #include <iostream> #include <cstdlib> using namespace std; class Node { public: int key; Node* p; Node* left; Node* right; Node(int data) { key = data; p = NULL; left = NULL; right = NULL; } }; class BinarySearchTree { public: Node* root; BinarySearchTree() { root = NULL; } void treeInsert(int data) { Node* z = new Node(data); Node* y = NULL; Node* x = root; while (x != NULL) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->p = y; if (y == NULL) root = z; else if (z->key < y->key) y->left = z; else y->right = z; } void postorderTreeWalk(Node* x) { if (x != NULL) { postorderTreeWalk(x->left); postorderTreeWalk(x->right); cout << x->key << endl; } } }; int main() { BinarySearchTree t; int n; while(cin>>n){ t.treeInsert(n); } t.postorderTreeWalk(t.root); return 0; }
Wednesday, 24 June 2015
UVA _12347_Binary Search Tree
Labels:
UVA
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment