• Source
    1. /***
    2.  
    3. Bismillahir Rahmanir Rahim
    4.  
    5.  _______
    6. | __ \ _____
    7. | |__| )_______ | / ____ ______
    8. | ____/ \__ \ | |__ / \ | \
    9. | ( / __ \ | __ \ / __ \ | /\ \
    10. | | (____ / | / \ / |__/ \ )
    11. |__| \/ |____/ \____/ \/
    12.  
    13.  
    14. Sample Input
    15.  
    16. 8
    17.  
    18. 8 3 1 5 4 9 12 11
    19.  
    20. ***/
    21.  
    22. #include <bits/stdc++.h>
    23.  
    24. using namespace std;
    25.  
    26. /* এটি আমাদের স্ট্রাকচার */
    27.  
    28. struct BstNode
    29. {
    30. int data;
    31.  
    32. BstNode* left;
    33.  
    34. BstNode* right;
    35.  
    36. };
    37.  
    38. /* মেমরিতে নতুন নোড নিচ্ছি */
    39.  
    40. BstNode* GetNewNode(int data)
    41. {
    42. BstNode* newNode = new BstNode();
    43.  
    44. newNode->data = data;
    45.  
    46. newNode->left = newNode->right = NULL;
    47.  
    48. return newNode;
    49. }
    50.  
    51. BstNode* Insert(BstNode* root, int data)
    52. {
    53. if(root==NULL)
    54. {
    55. root = GetNewNode(data);
    56. }
    57. else if(data <= root->data)
    58. {
    59. root->left = Insert(root->left, data);
    60. }
    61. else
    62. {
    63. root->right = Insert(root->right, data);
    64. }
    65. return root;
    66.  
    67. }
    68.  
    69. BstNode* Search(BstNode* root, int key)
    70. {
    71. if (root == NULL || root->data == key)
    72. {
    73. return root;
    74. }
    75. if (root->data < key)
    76. {
    77. return Search(root->right, key);
    78. }
    79. else
    80. {
    81. return Search(root->left, key);
    82. }
    83. }
    84.  
    85. void printPreOrder(BstNode* root)
    86. {
    87. if(root==NULL)
    88. {
    89. return;
    90. }
    91.  
    92. printf("%d ", root->data);
    93.  
    94. printPreOrder(root->left);
    95.  
    96. printPreOrder(root->right);
    97.  
    98. }
    99.  
    100. void printInOrder(BstNode* root)
    101. {
    102. if(root==NULL)
    103. {
    104. return;
    105. }
    106.  
    107. printInOrder(root->left);
    108.  
    109. printf("%d ", root->data);
    110.  
    111. printInOrder(root->right);
    112. }
    113.  
    114. void printPostOrder(BstNode* root)
    115. {
    116. if(root==NULL)
    117. {
    118. return;
    119. }
    120.  
    121. printPostOrder(root->left);
    122.  
    123. printPostOrder(root->right);
    124.  
    125. printf("%d ", root->data);
    126. }
    127.  
    128. BstNode* maxValueNode(BstNode* node)
    129. {
    130. BstNode* current = node;
    131.  
    132. /* সবচেয়ে রাইটে যে আছে সেটিই সবচেয়ে বড় */
    133.  
    134. while (current->right != NULL)
    135. current = current->right;
    136.  
    137. return current;
    138. }
    139.  
    140. BstNode* deleteNode(BstNode* root, int key)
    141. {
    142. /* বেস কেস */
    143.  
    144. if (root == NULL)
    145. {
    146. return root;
    147. }
    148.  
    149. /* যে ডাটাটি ডিলিট করবো তা রুটের ভ্যালু থেকে ছোট হলে সেটি রুটের লেফট সাবট্রিতে আছে */
    150.  
    151. if (key < root->data)
    152. {
    153. root->left = deleteNode(root->left, key);
    154. }
    155.  
    156. /* যে ডাটাটি ডিলিট করবো তা রুটের ভ্যালু থেকে বড় হলে সেটি রুটের রাইট সাবট্রিতে আছে */
    157.  
    158. else if (key > root->data)
    159. {
    160. root->right = deleteNode(root->right, key);
    161. }
    162.  
    163. /* ডাটাটি রুটের ভ্যালুর সমান হলে এটিই সেই ডাটা যেটি আমরা ডিলিট করতে চাচ্ছি */
    164.  
    165. else
    166. {
    167. /* নোডের যদি চাইল্ড না থাকে বা একটি চাইল্ড থাকে */
    168.  
    169. if (root->left == NULL)
    170. {
    171. BstNode* temp = root->right;
    172. free(root);
    173. return temp;
    174. }
    175. else if (root->right == NULL)
    176. {
    177. BstNode* temp = root->left;
    178. free(root);
    179. return temp;
    180. }
    181.  
    182. /* নোডের দুটি চাইল্ড থাকলে তার লেফট সাবট্রিয়ের সবচেয়ে বড় ভ্যালুকে আমরা খুঁজে বের করবো
    183.   অথবা রাইট সাবট্রিয়ের সবচেয়ে ছোট ভ্যালুকে আমরা খুঁজে বের করবো
    184.   এখানে আমরা লেফট সাবট্রিতে সার্চ দিয়েছি */
    185.  
    186. BstNode* temp = maxValueNode(root->left);
    187.  
    188. /* লেফট সাবট্রিয়ের সবচেয়ে বড় ভ্যালুই এখন আমাদের রুট হবে */
    189.  
    190. root->data = temp->data;
    191.  
    192. /* লেফট সাবট্রিয়ের সবচেয়ে বড় ভ্যালুটি যেহেতু আমাদের রুটে নিয়ে এসেছি
    193.   তাই যেখান থেকে ভ্যালুটি এনেছি সেটিকে ডিলিট করে দেবো */
    194.  
    195. root->left = deleteNode(root->left, temp->data);
    196. }
    197.  
    198. return root;
    199. }
    200.  
    201. int main()
    202. {
    203. BstNode* root = NULL;
    204.  
    205. int n, data,i,choice,val;
    206.  
    207. printf("How many data's do you want to insert ?\n");
    208.  
    209. scanf("%d", &n);
    210.  
    211. for(i=1; i<=n; i++)
    212. {
    213. printf("Data %d: ", i);
    214.  
    215. scanf("%d", &data);
    216.  
    217. root = Insert(root, data);
    218. }
    219.  
    220. printf("\nChoice 1 : Add a Node\n");
    221. printf("Choice 2 : Search a Node\n");
    222. printf("Choice 3 : Delete a Node\n");
    223. printf("Choice 4 : Traverse BST in Pre-Order\n");
    224. printf("Choice 5 : Traverse BST in In-Order\n");
    225. printf("Choice 6 : Traverse BST in Post-Order\n");
    226.  
    227. puts("");
    228.  
    229. printf("Enter your Choice : ");
    230.  
    231. while(scanf("%d",&choice)==1)
    232. {
    233. if(choice==1)
    234. {
    235. printf("Enter a value to be added : ");
    236.  
    237. scanf("%d",&val);
    238.  
    239. root = Insert(root, val);
    240. }
    241. else if(choice==2)
    242. {
    243. printf("Enter a value to be Searched : ");
    244.  
    245. scanf("%d",&val);
    246.  
    247. if(Search(root,val)==NULL)
    248. {
    249. printf("The value doesn't exist in BST\n\n");
    250. }
    251. else
    252. {
    253. printf("The value exists in BST\n\n");
    254. }
    255. }
    256. else if(choice==3)
    257. {
    258. printf("Enter a value to be Deleted : ");
    259.  
    260. scanf("%d",&val);
    261.  
    262. deleteNode(root,val);
    263. }
    264. else if(choice==4)
    265. {
    266. printf("PreOrder Traversal: ");
    267.  
    268. printPreOrder(root);
    269.  
    270. printf("\n\n");
    271. }
    272. else if(choice==5)
    273. {
    274. printf("InOrder Traversal: ");
    275.  
    276. printInOrder(root);
    277.  
    278. printf("\n\n");
    279. }
    280. else if(choice==6)
    281. {
    282. printf("PostOrder Traversal: ");
    283.  
    284. printPostOrder(root);
    285.  
    286. printf("\n\n");
    287. }
    288. else
    289. {
    290. printf("Invalid Choice\n\n");
    291. }
    292.  
    293. printf("Enter your Choice : ");
    294. }
    295. return 0;
    296. }