Advertisement
Fakhru

BST TREE Data Struct

Sep 3rd, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std ;
  3.  
  4. struct BST{
  5. BST * left ;
  6. int data ;
  7. BST*right ;
  8.  
  9.  
  10. };
  11.  
  12. void push () ;
  13. void pop () ;
  14. void search () ;
  15. void preorder (BST*) ;
  16. void inorder (BST *) ;
  17. void postorder (BST *) ;
  18.  
  19. BST*root = NULL ;
  20.  
  21. int main (void){
  22.  
  23. int pilihan ;
  24.  
  25. while (1) {
  26. cout<<"Pilih Operasi Anda : " <<endl ;
  27. cout<<"1. Push " <<endl ;
  28. cout<<"2. Pop " <<endl ;
  29. cout<<"3. Search" <<endl ;
  30. cout<<"4. Traverse Preorder " <<endl ;
  31. cout<<"5. Traverse Inorder " <<endl ;
  32. cout<<"6. Traverse Postorder " <<endl ;
  33. cout<<"7. Keluar " <<endl ;
  34. cout<<"Pilihan anda : " <<endl ;
  35. cin>> pilihan ;
  36.  
  37. switch (pilihan) {
  38. case 1 : push () ; break ;
  39. case 2 : pop () ; break ;
  40. case 3 : search () ; break ;
  41. case 4 : preorder (root) ; break ;
  42. case 5 : inorder (root) ; break ;
  43. case 6 : postorder (root) ; break ;
  44.  
  45. }
  46. }
  47. system("pause") ;
  48.  
  49. return 0 ;
  50.  
  51. }
  52.  
  53. void push (){
  54. BST * tmp = root ;
  55. BST * chelsea = new BST ;
  56. bool jumpa = false ;
  57.  
  58.  
  59. cout<<"mu nok wak masuk mende hux aloh : " ;
  60. cin>>chelsea -> data ;
  61. chelsea -> left = NULL ;
  62. chelsea -> right = NULL ;
  63.  
  64. //true utk 1st node shj
  65. if(root == NULL){
  66. root=chelsea ;
  67.  
  68.  
  69. }
  70. //UTK 2ND NODE AND D REST
  71. else{
  72.  
  73. while(!jumpa){
  74.  
  75. if (chelsea-> data <tmp -> data){
  76. // maka node ni anok jd anok kiri
  77. if(tmp -> left == NULL) {
  78. tmp -> left = chelsea ;
  79. jumpa = true ;
  80. }
  81. else
  82. tmp = tmp -> left ;
  83.  
  84. }
  85.  
  86. else if(chelsea -> data > tmp -> data){
  87. // maka node ni anok jd anok kanan
  88. if(tmp -> right == NULL) {
  89. tmp -> right = chelsea ;
  90. jumpa = true ;
  91. }
  92. else
  93. tmp = tmp -> right ;
  94. }
  95. else{
  96. //data duplicate....x leh push bewok weh
  97. cout<<"data duplicate...xleh push bewok weh"<<endl ;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. void pop () {
  104.  
  105. }
  106.  
  107. void search () {
  108.  
  109. }
  110.  
  111. void preorder (BST* tmp) {
  112.  
  113. if(tmp != NULL) {
  114. cout<< tmp -> data ;
  115. preorder (tmp -> left ) ;
  116. preorder (tmp -> right ) ;
  117. }
  118. }
  119.  
  120. void inorder (BST* tmp) {
  121.  
  122. if(tmp != NULL) {
  123.  
  124. inorder (tmp -> left ) ;
  125. cout<< tmp -> data ;
  126. inorder (tmp -> right ) ;
  127. }
  128.  
  129. }
  130.  
  131. void postorder (BST* tmp) {
  132.  
  133. if(tmp != NULL) {
  134.  
  135. postorder (tmp -> left ) ;
  136. postorder (tmp -> right ) ;
  137. cout<<tmp-> data ;
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement