Sie sind auf Seite 1von 8

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

UNIT-6
Q. What is a binary search tree? Explain its operations with its ADT.
Binary Search Tree (BST) is a binary tree (has at most 2 children).
It is also referred as sorted/ ordered binary tree.
BST has the following properties.
o The left subtree of a node contains only nodes with keys less than the node's key.
o The right subtree of a node contains only nodes with keys greater than the node's key.
o Both the left and right subtrees must also be binary search trees.

A Binary Search Tree

Not a Binary Search Tree

BST ADT:class BinarySearchTree {


public:
class BSTNode {
private:
int value;
BSTNode* left;
BSTNode* right;
public:
BSTNode(int value) {

Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

this->value = value;
left = NULL;
right = NULL;
}
};
private:
BSTNode* root;
public:
BinarySearchTree() {
root = NULL;
}
bool search(int value);
bool add(int value);
bool remove(int value) {
};

BST Operations:o Searching in a BST


Examine the root node. If tree is NULL value doesn't exist.
If value equals the key in root search is successful and return.
If value is less than root, search the left sub-tree.
If value is greater than root, search the right sub-tree.
Continue until the value is found or the sub tree is NULL.
Time complexity. Average: O(log n), Worst: O(n) if the BST is unbalanced and
resembles a linked list.

bool BSTNode::search(int value) {


if (value == this->value)
return true;
else if (value < this->value) {
if (left == NULL)
return false;
else
return left->search(value);
} else if (value > this->value) {
if (right == NULL)
return false;
else
return right->search(value);
}
return false;
}

Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

Insertion in BST
Insertion begins as a search.
Compare the key with root. If not equal search the left or right sub tre
When a leaf node is reached add the new node to left or right based on the value.
Time complexity. Average: O(log n), Worst O(n)

Before inserting 35

After inserting 35

Code:
bool BinarySearchTree::add(int value) {
if (root == NULL) {
root = new BSTNode(value);
return true;
} else
return root->add(value);
}
bool BSTNode::add(int value) {
if (value == this->value)
return false;
else if (value < this->value) {
if (left == NULL) {
left = new BSTNode(value);
return true;
} else
return left->add(value);
} else if (value > this->value) {
if (right == NULL) {
right = new BSTNode(value);
return true;
} else
return right->add(value);
}
return false;
}
o

Deletion in BST
There are three possible cases to consider:
Deleting a leaf (node with no children): Deleting a leaf is easy, as we can
simply remove it from the tree.

Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

Deleting a node with one child: Remove the node and replace it with its
child.
Deleting a node with two children: Call the node to be deleted N. Do not
delete N. Instead, choose either its in-order successor node or its in-order
predecessor node, R. Replace the value of N with the value of R, then
delete R.

Before deleting a leaf node 7

Before deleting a node with children(40)

After deleting a leaf node 7

After deleting a node with children(40)

Code:
bool BinarySearchTree::remove(int value) {
if (root == NULL)
return false;
else {
if (root->getValue() == value) {
BSTNode auxRoot(0);
auxRoot.setLeftChild(root);
BSTNode* removedNode = root->remove(value, &auxRoot);
root = auxRoot.getLeft();
if (removedNode != NULL) {
delete removedNode;
return true;
} else
return false;
} else {
BSTNode* removedNode = root->remove(value, NULL);
if (removedNode != NULL) {

Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

delete removedNode;
return true;
} else
return false;
}
}
}

AVL Trees:
Binary search trees are an excellent data structure to implement associative arrays, maps, sets, and
similar interfaces. The main difficulty is that they are efficient only when they are balanced.
It will take more time to search for 100 in first tree rather than in second tree.

Formally, a BST is an AVL-tree if the height of the left and right subtree of each node in the tree
differs by at most 1. Here is an example of two trees one of which is an AVL-tree and the other is not.

Height Invariant. At any node in the tree, the heights of the left and right subtrees differ by at
most 1. As an example, consider the following binary search tree of height 3.

If we insert a new element with a key of 14, the insertion algorithm for binary search trees
without rebalancing will put it to the right of 13.

Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

Now consider another insertion, this time of an element with key 15. This is inserted to the right
of the node with key 14.

We therefore have to take steps to rebalance tree. We can restore the height invariant if we move
the node labeled 14 up and push node 13 down and to the right- called as rotation, resulting in the
following tree.

Left and Right Rotations:


Below, we show the situation before a left rotation. We have generically denoted the crucial key
values in question with x and y. Also, we have summarized whole subtrees with the intervals bounding
their key values. Even though we wrote - and +, when the whole tree is a subtree of a larger tree these
bounds will be generic bounds which is smaller than x and which is greater than y. The tree on the
right is after the left rotation.

Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

From the intervals we can see that the ordering invariants are preserved, as are the contents of the
tree. We can also see that it shifts some nodes from the right subtree to the left subtree. We would invoke
this operation if the invariants told us that we have to rebalance from right to left.
The right rotation is entirely symmetric. First in pictures:

Searching for a key:


Searching for a key in an AVL tree is identical to searching for it in a plain binary search tree.
The reason is that we only need the ordering invariant to find the element; the height invariant is only
relevant for inserting an element.
Inserting an Element:
The basic recursive structure of inserting an element is the same as for searching for an element.
We compare the elements key with the keys associated with the nodes of the trees, inserting recursively
into the left or right subtree. When we find an element with the exact key we overwrite the element in
that node. If we encounter a null tree, we construct a new tree with the element to be inserted and no
children and then return it. As we return the new subtrees (with the inserted element) towards the root,
we check if we violate the height invariant. If so, we rebalance to restore the invariant and then continue
up the tree to the root.
The main cleverness of the algorithm lies in analyzing the situations when we have to rebalance
and applying the appropriate rotations to restore the height invariant. It turns out that one or two rotations
on the whole tree always suffice for each insert operation, which is a very elegant result.
First, we keep in mind that the left and right subtrees heights before the insertion can differ by at
most one. Once we insert an element into one of the subtrees, they can differ by at most two. We now
draw the trees in such a way that the height of a node is indicated by the height that we are drawing it at.
The first situation we describe is where we insert into the right subtree, which is already of height
h + 1 where the left subtree has height h. If we are unlucky, the result of inserting into the right subtree
Prepared by A. Sharath Kumar

NRI Institute of Technology

Data Structures through C++

B.Tech II Yr I Sem(R09)

will give us a new right subtree of height h + 2 which raises the height of the overall tree to h + 3,
violating the height invariant. In the new right subtree has height h+2, either its right or the left subtree
must be of height h+1 (and only one of them; think about why). If it is the right subtree we are in the
situation depicted below on the left.

In the second case we consider we once again insert into the right subtree, but now the left subtree
of the right subtree has height h + 1. In that case, a left rotation alone will not restore the invariant.
Instead, we apply a so-called double rotation: first a right rotation at z, then a left rotation at the root.
When we do this we obtain the picture on the right, restoring the height invariant.

UNIT-7
B Trees:
In computer science, a B-tree is a tree data structure that keeps data sorted and allows
searches, insertions, deletions, and sequential access in logarithmic amortized time. The B-tree is a
generalization of a binary search tree in that more than two paths diverge from a single node. Unlike selfbalancing binary search trees, the B-tree is optimized for systems that read and write large blocks of data.
It is most commonly used in databases and file systems.
Definition:
A B-tree of order m (the maximum number of children for each node) is a tree which satisfies the
following properties:
Every node has at most m children(The number m should always be odd).
Every node (except root and leaves) has at least m2 children.
The root has at least two children if it is not a leaf node.
All leaves appear in the same level, and carry information.
A non-leaf node with k children contains k1 keys.

Prepared by A. Sharath Kumar

Das könnte Ihnen auch gefallen