// sl-4-DHBW-Inf2 #include #include "suchbaum.h" #include using namespace std; void bstInsert(bstPtr & root, const DataType value) { // inserts into a binary search tree rooted at // won't insert it again if it's already in the tree if (root==NULL) { root = new bsTree(value); // children become NULL by default } else if (valuevalue) { bstInsert(root->left,value); } else if (value>root->value) { bstInsert(root->right,value); } else { // it is == cout << "Element " << value << " is already in the tree!" << endl; } } bool bstFind(const bstPtr root, const DataType value) { // tries to find in a binary search tree rooted at if (root==NULL) { return false; } else if (valuevalue) { return bstFind(root->left,value); } else if (value>root->value) { return bstFind(root->right,value); } else { // it is == return true; } } bool bstDelete(bstPtr & root, const DataType value) { // tries to delete in a binary search tree rooted at // returns true if was deleted, false if it isn't in the tree if (root==NULL) { cout << "Element " << value << " isn't in the tree!" << endl; return false; // no, the element was not in the tree } else if (valuevalue) { return bstDelete(root->left,value); } else if (value>root->value) { return bstDelete(root->right,value); } else { // it is == cout << "Element " << value << " is in the tree, I'll delete it!" << endl; if (root->left==NULL && root->right==NULL) { // it's a leaf => delete it delete root; root=NULL; } else if (root->left==NULL) { // right subtree must be non-empty, delete root and shift subtree up bstPtr tbd=root; root=root->right; delete tbd; } else if (root->right==NULL) { // left subtree must be non-empty, delete root and shift subtree up bstPtr tbd=root; root=root->left; delete tbd; } else { // both subtrees are non-empty => find inorder successor s, // put s to the actual position, shift right subtree of s up bstPtr iosucc=root->right; // root is not NULL bstPtr iosparent=root; while (iosucc->left!=NULL) { iosparent=iosucc; iosucc=iosucc->left; } // now iosucc is the inorder successor of root if (iosparent!=root) { // iosucc is not right child of root iosparent->left=iosucc->right; // may be NULL, doesn't matter iosucc->right=root->right; } iosucc->left=root->left; bstPtr tbd=root; root=iosucc; // root parent's child ptr will be corrected by the & delete tbd; } return true; // yes, we deleted an element } } void preorder(const bstPtr root) { if (root!=NULL) { cout << root->value << ' '; preorder(root->left); preorder(root->right); } } void inorder(const bstPtr root) { if (root!=NULL) { inorder(root->left); cout << root->value << ' '; inorder(root->right); } } void postorder(const bstPtr root) { if (root!=NULL) { postorder(root->left); postorder(root->right); cout << root->value << ' '; } } void delTree(const bstPtr root) { if (root!=NULL) { delTree(root->left); delTree(root->right); delete root; } } #define CreateDyn2DArray(typ,name,size) typ **name=new typ*[size]; \ for(int i=0;ir) { return NULL; } else { int rootidx=roots[l][r]; bstPtr root=new bsTree(elems[rootidx]); root->left=genBST(elems,roots,size,l,rootidx-1); root->right=genBST(elems,roots,size,rootidx+1,r); return root; } }