Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std ;
- struct BST{
- BST * left ;
- int data ;
- BST*right ;
- };
- void push () ;
- void pop () ;
- void search () ;
- void preorder (BST*) ;
- void inorder (BST *) ;
- void postorder (BST *) ;
- BST*root = NULL ;
- int main (void){
- int pilihan ;
- while (1) {
- cout<<"Pilih Operasi Anda : " <<endl ;
- cout<<"1. Push " <<endl ;
- cout<<"2. Pop " <<endl ;
- cout<<"3. Search" <<endl ;
- cout<<"4. Traverse Preorder " <<endl ;
- cout<<"5. Traverse Inorder " <<endl ;
- cout<<"6. Traverse Postorder " <<endl ;
- cout<<"7. Keluar " <<endl ;
- cout<<"Pilihan anda : " <<endl ;
- cin>> pilihan ;
- switch (pilihan) {
- case 1 : push () ; break ;
- case 2 : pop () ; break ;
- case 3 : search () ; break ;
- case 4 : preorder (root) ; break ;
- case 5 : inorder (root) ; break ;
- case 6 : postorder (root) ; break ;
- }
- }
- system("pause") ;
- return 0 ;
- }
- void push (){
- BST * tmp = root ;
- BST * chelsea = new BST ;
- bool jumpa = false ;
- cout<<"mu nok wak masuk mende hux aloh : " ;
- cin>>chelsea -> data ;
- chelsea -> left = NULL ;
- chelsea -> right = NULL ;
- //true utk 1st node shj
- if(root == NULL){
- root=chelsea ;
- }
- //UTK 2ND NODE AND D REST
- else{
- while(!jumpa){
- if (chelsea-> data <tmp -> data){
- // maka node ni anok jd anok kiri
- if(tmp -> left == NULL) {
- tmp -> left = chelsea ;
- jumpa = true ;
- }
- else
- tmp = tmp -> left ;
- }
- else if(chelsea -> data > tmp -> data){
- // maka node ni anok jd anok kanan
- if(tmp -> right == NULL) {
- tmp -> right = chelsea ;
- jumpa = true ;
- }
- else
- tmp = tmp -> right ;
- }
- else{
- //data duplicate....x leh push bewok weh
- cout<<"data duplicate...xleh push bewok weh"<<endl ;
- }
- }
- }
- }
- void pop () {
- }
- void search () {
- }
- void preorder (BST* tmp) {
- if(tmp != NULL) {
- cout<< tmp -> data ;
- preorder (tmp -> left ) ;
- preorder (tmp -> right ) ;
- }
- }
- void inorder (BST* tmp) {
- if(tmp != NULL) {
- inorder (tmp -> left ) ;
- cout<< tmp -> data ;
- inorder (tmp -> right ) ;
- }
- }
- void postorder (BST* tmp) {
- if(tmp != NULL) {
- postorder (tmp -> left ) ;
- postorder (tmp -> right ) ;
- cout<<tmp-> data ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement