Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #ifndef BST_H
  2. #define BST_H
  3.  
  4. struct BSTNode {
  5. BSTNode* _left,_right;
  6. int val;
  7. BST(int val){
  8. this->val=val;
  9. _left=_right=NULL;
  10. }
  11. };
  12. class BST
  13. {
  14. public:
  15. BST();
  16. void insert(int val,BSTNode*);
  17. void find(int val,BSTNode*);
  18. void remove(int val,BSTNode*);
  19.  
  20. protected:
  21.  
  22. private:
  23. BSTNode* _head;
  24. };
  25.  
  26. void BST::insert(int val, BSTNode* &node=_head){
  27.  
  28. if(node==NULL){
  29. node=new BSTNode(val);
  30. return;
  31. }
  32. if(val>=node.val){
  33. insert(val,node->_right);
  34. }else{
  35. insert(val,node->_left);
  36. }
  37. }
  38.  
  39. bool BST::find(int val, BSTNode* &node=_head){
  40. if(node==NULL){
  41. return false;
  42. }
  43. if(val>=node.val){
  44. return find(val,node->_right);
  45. }else{
  46. return find(val,node->_left);
  47. }
  48.  
  49.  
  50. }
  51. bool isleaf(BSTNode* node){
  52. return node->left==node->right && node->right==NULL;
  53. }
  54.  
  55. void BST::remove(int val, BSTNode* &node= _head){
  56.  
  57. if(node==NULL){
  58. return;
  59. }
  60. if(val==node->val){
  61. if(isleaf(node)){
  62. delete node;
  63. node=NULL;
  64. return;
  65. }
  66.  
  67.  
  68. }
  69.  
  70. if(val>node->val){
  71. remove(val,node->_right);
  72. }else if(val<node->val){
  73. remove(val,node->_left);
  74. }
  75.  
  76.  
  77.  
  78. }
  79. #endif // BST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement