0% found this document useful (0 votes)
517 views

Binary Search Tree Is A Node-Based Binary Tree

Binary Search Tree is a node-based binary tree data structure where: - The left subtree of a node contains nodes with keys lesser than the node’s key - The right subtree contains nodes with greater keys - Searching, minimum and maximum operations can be done quickly in O(h) time where h is the height - AVL trees and Red-Black trees are examples that self-balance to keep the height small, O(log n), after insertions or deletions.

Uploaded by

Jatin Valecha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
517 views

Binary Search Tree Is A Node-Based Binary Tree

Binary Search Tree is a node-based binary tree data structure where: - The left subtree of a node contains nodes with keys lesser than the node’s key - The right subtree contains nodes with greater keys - Searching, minimum and maximum operations can be done quickly in O(h) time where h is the height - AVL trees and Red-Black trees are examples that self-balance to keep the height small, O(log n), after insertions or deletions.

Uploaded by

Jatin Valecha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Binary Search Tree

Binary Search Tree is a node-based binary tree


data structure which has the following
properties:
• The left subtree of a node contains only nodes
with keys lesser than the node’s key.
• The right subtree of a node contains only nodes
with keys greater than the node’s key.
• The left and right subtree each must also be a
binary search tree.
Example

The above properties of Binary Search Tree provides an ordering among keys so
that the operations like search, minimum and maximum can be done fast. If there
is no ordering, then we may have to compare every key to search for a given key.
Searching in binary search tree

• Start at the root, and then we will compare the value to be


searched with the value of the root if it’s equal we are done
with the search.
• if it’s lesser we know that we need to go to the left subtree
because in a binary search tree all the elements in the left
subtree are lesser and all the elements in the right subtree
are greater.
• Searching an element in the binary search tree is basically this
traversal in which at each step we will go either towards left
or right and hence in at each step we discard one of the sub-
trees.
• If the tree is balanced, we call a tree balanced if for all nodes
the difference between the heights of left and right subtrees
is not greater than one.
• we will start with a search space of ‘n’nodes and
when we will discard one of the sub-trees we will
discard ‘n/2’ nodes so our search space will be
reduced to ‘n/2’ and then in the next step we will
reduce the search space to ‘n/4’ and we will go
on reducing like this till we find the element or till
our search space is reduced to only one node.
• The search here is also a binary search and that’s
why the name binary search tree.
Insertion of a key 
Time Complexity

• The worst-case time complexity


of search and insert operations is
O(h) where h is the height of the
Binary Search Tree.
• For balanced BST h=logn
• In the worst case, we may have to
travel from root to the deepest
leaf node. The height of a skewed
tree may become n and the time
complexity of search and insert
operation may become O(n). 
Balanced Binary Tree
A balanced binary tree, also referred to as a height-balanced
binary tree, is defined as a binary tree in which the height of
the left and right subtree of any node differ by not more than
1.
Following are the conditions for a height-balanced binary tree:
• difference between the left and the right subtree for any node
is not more than one
• the left subtree is balanced
• the right subtree is balanced
AVL Tree

• AVL tree is a self-balancing binary search tree


in which each node maintains extra
information called a balance factor whose
value is either -1, 0 or +1.
Why AVL Trees? 

• Most of the BST operations (e.g., search, max, min,


insert, delete.. etc) take O(h) time where h is the
height of the BST. The cost of these operations may
become O(n) for a skewed Binary tree.
• If we make sure that height of the tree remains
O(Logn) after every insertion and deletion, then we
can guarantee an upper bound of O(Logn) for all
these operations. The height of an AVL tree is always
O(Logn) where n is the number of nodes in the tree 
AVL Rotations

If the difference in the height of left and right sub-trees is more


than 1, the tree is balanced using some rotation techniques.
To balance itself, an AVL tree may perform the following four
kinds of rotations −
• Left rotation
• Right rotation
• Left-Right rotation
• Right-Left rotation
The first two rotations are single rotations and the next two
rotations are double rotations. 
Left-Right Rotation
• A left-right rotation is a combination of left rotation followed
by right rotation.
Right-Left Rotation
It is a combination of right rotation followed by left rotation.
Red-Black Tree

• A red-black tree is a binary search tree with one extra attribute for
each node: the colour, which is either red or black. 
A red-black tree is a binary search tree which has the following red-
black properties:
• Every node is either red or black.
• The root of the tree is always black.
• Every leaf (NULL) is black.
• If a node is red, then both its children are black. Means that on any
path from the root to a leaf, red nodes must not be adjacent.
However, any number of black nodes may appear in a sequence.
• Every simple path from a node to a descendant leaf contains the
same number of black nodes.

You might also like