Download presentation
Presentation is loading. Please wait.
1
The Binary Search Tree Data Structure
Mugurel Ionuț Andreica Spring 2012
2
The Properties of a Binary Search Tree
A binary search tree is a binary tree with some extra properties This implies that it maintains the same information as a binary tree (left son, right son, parent, pointer to some useful information) Extra properties: The elements (useful information) stored in the tree nodes are comparable The elements stored in the left sub-tree of a node p are ≤ the element stored by p The elements stored in the right sub-tree of a node p are > the element stored by p Faster search More complicated removal (because the property of the BST must be maintained)
3
Binary Search Tree - example
4
Binary Search Tree – C++ code
template<typename T> class BSTNode { public: BSTNode<T> *root, *left_son, *right_son, *parent; T *pinfo; BSTNode() { left_son = right_son = NULL; root = this; pinfo = NULL; } void setInfo(T info) { pinfo = new T; *pinfo = info; void insertInfo(T x) { if (pinfo == NULL) setInfo(x); else insert_rec(x); void insert_rec(T x) { int next_son; if (x <= (*pinfo)) next_son = 0; else next_son = 1; if (next_son == 0) { // left son if (left_son == NULL) { left_son = new BSTNode<T>; left_son->pinfo = new T; *(left_son->pinfo) = x; left_son->left_son = left_son->right_son = NULL; left_son->parent = this; left_son->root = root; } else left_son->insert_rec(x); } else { // right son if (right_son == NULL) { right_son = new BSTNode<T>; right_son->pinfo = new T; *(right_son->pinfo) = x; right_son->left_son = right_son->right_son = NULL; right_son->parent = this; right_son->root = root; right_son->insert_rec(x); }
5
Binary Search Tree – C++ code (cont.)
BSTNode<T>* findInfo(T x) { BSTNode<T> *rez; if (pinfo == NULL) return NULL; if ((*pinfo) == x) return this; if (x <= (*pinfo)) { if (left_son != NULL) return left_son->findInfo(x); else } else { if (right_son != NULL) return right_son->findInfo(x); } void removeInfo(T x) { BSTNode<T> *t = findInfo(x); if (t != NULL) t->remove(); void remove() { BSTNode<T> *p; T *paux; if (left_son == NULL && right_son == NULL) { if (parent == NULL) { // this == root delete this->pinfo; root->pinfo = NULL; } else { if (parent->left_son == this) parent->left_son = NULL; else parent->right_son = NULL; delete this; } if (left_son != NULL) { p = left_son; while (p->right_son != NULL) p = p->right_son; } else { // right_son != NULL p = right_son; while (p->left_son != NULL) p = p->left_son;
6
Binary Search Tree – C++ code (cont.)
paux = p->pinfo; p->pinfo = this->pinfo; this->pinfo = paux; p->remove(); } void inOrderTraversal() { if (left_son != NULL) left_son->inOrderTraversal(); printf("%d\n", *pinfo); // we should use the correct format // for printing type T values if (right_son != NULL) right_son->inOrderTraversal(); }; int main() { srand(7290); BSTNode<int> *r = new BSTNode<int>; r->insertInfo(6); r->insertInfo(8); r->insertInfo(1); r->insertInfo(9); r->insertInfo(10); r->insertInfo(4); r->insertInfo(13); r->insertInfo(1); r->insertInfo(12); r->inOrderTraversal(); printf("%d\n", r->findInfo(100)); printf("%d\n", r->findInfo(1)); printf("%d\n", r->findInfo(12)); printf("%d\n", r->findInfo(8)); printf("%d\n", r->findInfo(10)); printf("%d\n", r->findInfo(4)); (r->findInfo(6))->remove(); r->removeInfo(9); return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.