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

Binary Search Tree

A binary search tree is a binary tree where the value of each node is greater than all values in its left subtree and less than all values in its right subtree, allowing for efficient search, insertion, and deletion operations in O(log n) time on average; however, the tree can become unbalanced, resulting in O(n) worst-case time for some operations. Binary search trees can be used to efficiently sort data, remove duplicates, and solve other algorithmic problems by taking advantage of their self-balancing and search tree properties.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Binary Search Tree

A binary search tree is a binary tree where the value of each node is greater than all values in its left subtree and less than all values in its right subtree, allowing for efficient search, insertion, and deletion operations in O(log n) time on average; however, the tree can become unbalanced, resulting in O(n) worst-case time for some operations. Binary search trees can be used to efficiently sort data, remove duplicates, and solve other algorithmic problems by taking advantage of their self-balancing and search tree properties.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Binary Search Trees

Binary Search Trees

 A binary search tree is a binary tree that has the following properties
 Given a node with value K
 All values on the left subtree of K are smaller than K
 All values on the right subtree of K are greater than K
 There are no repeated values in the tree (optional)

12

9 16

2 11 13 21

1 3 10
Binary Search Trees

 Desirable characteristics of a binary search tree


 They support many operations
 Search
 Delete
 Maximum / Minimum
 Basic operations take time proportional to the height of the tree
 For complete trees this is O(log n)
 Undesirable characteristics of a binary search tree
 The time of O(log n) cannot be guaranteed in all cases since the
tree can be a linear chain. This can give us worst case time
proportional to O(n)
Binary Search Trees (BSTs)

In a BST, the value


stored at the root of
a subtree is greater
than any value in its
left subtree and less
than any value in its
right subtree!
How to search a binary search tree?

(1) Start at the root


(2) Compare the value of
the item you are
searching for with
the value stored at
the root
(3) If the values are
equal, then item
found; otherwise, if it
is a leaf node, then
not found
How to search a binary search tree?

(4) If it is less than the value


stored at the root, then
search the left subtree
(5) If it is greater than the
value stored at the root,
then search the right
subtree
(6) Repeat steps 2-6 for the
root of the subtree chosen
in the previous step 4 or
5
search(10)

12

9 16

2 11 13 21

1 3 10
search(10)

12

9 16

2 11 13 21

1 3 10
search(10)

12

9 16

2 11 13 21

1 3 10
search(10)

12

9 16

2 11 13 21

1 3 10
search(10)

12

9 16

2 11 13 21

1 3 10
search(10)

12

9 16

2 11 13 21

1 3 10
Implementation of Searching
TREE search(TREE tree, int key)
{
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->left_child;
else tree = tree->right_child;
}
return NULL;
}
Binary Search Trees (BSTs)

Where is the
smallest element?
Ans: leftmost element

Where is the largest


element?
Ans: rightmost element
Where is second smallest value present?
Where is second smallest value present?

56

26 200

18 28 190 213

12 24 27
Where is second smallest value present?

15

8 20

2 11 27

6 10 12 22 30

3 7 14
Predecessor and Successor

 Successor of node x is the node y such that


key[y] is the smallest key greater than key[x].
 The successor of the largest key is NIL.
 Search consists of two cases.
 If node x has a non-empty right subtree, then x’s
successor is the minimum in the right subtree of x.
 If node x has an empty right subtree, then:
 As long as we move to the right up the tree (move

up through right children), we are visiting smaller


keys.
 The first node y, whose left subtree contains x, is

the successor of x.
Predecessor and Successor

15

8 20

2 11 27

6 10 12 22 30

3 7 14
Insertion in Binary Search Trees
 To insert an element in a BST we follow the same principle applied in
the searching.
 Compare the value to be inserted with the current key and decide if
the new element should be inserted on the left or on the right
 The element is always inserted as a leaf in the tree.

insert (target)

1. If head of the tree is null create new node to store target;


2. Test whether the target is the same as the key in the
head of the tree and return null (this means that the
element cannot be added to the tree)
3. If not true, if target is less than the key in the head insert
the target in the in the left subtree
4. If target is greater than the key in the head
insert the target in the right subtree
insert(14)

12

9 16

2 11 13 21

1 3 10
insert(14)

12

9 16

2 11 13 21

1 3 10
insert(14)

12

9 16

2 11 13 21

1 3 10
insert(14)

12

9 16

2 11 13 21

1 3 10
insert(14)

12

9 16

2 11 13 21

1 3 10 14
Does the order of inserting elements into a tree
matter?

 Yes, certain orders might produce very


unbalanced trees!
Does the
order of
inserting
elements
into a tree
matter?
(cont.)
An Exercise on Binary Search Tree

 Create BSTs from the same set of data presented


each time in a different order:
 a) 17 4 14 19 15 7 9 3 16 10
 b) 9 10 17 4 3 7 14 16 15 19
 c) 19 17 16 15 14 10 9 7 4 3
TREE maketree(int x)
{
TREE tree = (TREE)malloc(sizeof(struct tree));
tree->left = tree->right = NULL;
tree->data = x;
return tree;
}

void setleft(TREE tree, int x)


{
tree->left = maketree(x);
}

void setright(TREE tree, int x)


{
tree->right = maketree(x);
}
main()
{
TREE root = NULL, parent, child;
int i;
scanf("%d", &i);
root = maketree(i);
while(1)
{
scanf("%d", &i);
if(i<0)break;
parent = child = root;
while((i != parent->data) && (child != NULL))
{
parent = child;
if(i < parent->data)
child = parent->left;
else
child = parent->right;
}
if(i==parent->data)
printf("Duplicate %d\n", parent->data);
else
if(i<parent->data) setleft(parent,i);
else setright(parent,i);
}
}
Function DeleteItem

 First, find the item; then, delete it


 Binary search tree property must be preserved!!
 We need to consider three different cases:
(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(1) Deleting a leaf
(2) Deleting a node with
only one child
(3) Deleting a node with two
children
(3) Deleting a node with two
children (cont.)

 Find predecessor (i.e., rightmost node in the left


subtree)
 Replace the data of the node to be deleted with
predecessor's data
 Delete predecessor node
Exercise To Try
 Create a BST from following values
13 5 8 1 33 24 56 41 18 2 3
Delete the root node twice
Applications of Binary Search Trees
 Searching
 Sorting
 Removing Duplicates
 Counting frequency
 Finding Mode
Sorting

 The inorder (left-root-right) traversal of the


Binary Search Tree and printing the info part of
the nodes gives the sorted sequence in ascending
order. Therefore, the Binary search tree approach
can easily be used to sort a given array of
numbers.
Removing Duplicates

 Suppose that we wanted to find all duplicates in a


list of numbers. One way of doing this to compare
each number with all those that precede it. However
this involves a large number of comparison. The
number of comparison can be reduced by using a
binary search tree. Just insert all elements in a BST,
the duplicates will automatically be rejected.
Using Binary Search Trees Application:
Removing Duplicates

7
v v
3 9
7 3 2 5 3 2 9 3 2 3 5 7 9
2 5
Counting Frequency
 Suppose we want to know the frequency of all the words
occuring in the book, “Tale of two cities”
 Assumptions:
 book has 135,000+ words
 about 10,000 distinct words
 What is the best data structure?
 1) unordered array
 2) unordered linked list
 3) ordered array with binary search
 4) Binary Search Tree
struct tree
{
char data[20];
int count;
struct tree *left, *right;
};

TREE maketree(char x[])


{
TREE tree = (TREE)malloc(sizeof(struct tree));
tree->left = tree->right = NULL;
strcpy(tree->data, x);
tree->count = 1;
return tree;
}

void setleft(TREE tree, char x[])


{
tree->left = maketree(x);
}

void setright(TREE tree, char x[])


{
tree->right = maketree(x);
}
main()
{
TREE root = NULL, parent, child;
char first[20];
FILE *fp1;
fp1 = fopen("output.txt", "r");
fscanf(fp1, "%s", first);
root = maketree(first);
do
{
if(fscanf(fp1, "%s", first)==EOF)break;
parent = child = root;
while(strcmp(first, parent->data) && (child != NULL))
{
parent = child;
if(strcmp(first, parent->data)<0)
child = parent->left;
else
child = parent->right;
}
if(!strcmp(first,parent->data))
(parent->count)++;
else
if(strcmp(first, parent->data) setleft(parent,first);
else setright(parent,first);
}
}
Making Trees Efficient:
Possible Solutions
Keep BSTs shallow by maintaining “balance”
 AVL trees

… also exploit most-recently-used (mru) info


 Splay trees

Reduce disk access by increasing branching factor


 B-trees
Some more Interview Questions
1. Write a code to delete a tree in C. [Amazon Interview Question]
2. Given the root of a binary search tree along with a node inside it, find
the successor node for the given node.[Facebook Interview Question]
3. How to verify whether a binary tree is a binary search tree?
Another Interview Question
Determine whether an input array is a post-order
traversal sequence of a binary search tree or not. If it is,
return true; otherwise return false. Assume all numbers
in an input array are unique. For example, if the input
array is {5, 7, 6, 9, 11, 10, 8}, true should be returned,
since it is a post-order traversal sequence of the binary
search tree below. If the input array is {7, 4, 6, 5}, false
should be returned since there are no binary search
trees whose post-order traversal sequence is such an
array.

You might also like