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

Binary Search Tree 1

A binary search tree is a binary tree where the value of each node is greater than all values in the left subtree and less than all values in the right subtree. Basic operations on a balanced binary search tree take O(log n) time while unbalanced trees can take O(n) time. To search a value in a binary search tree, the value is compared to the root node and traversal continues left or right depending on whether the value is less than or greater than the root value.

Uploaded by

enatholdings
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Binary Search Tree 1

A binary search tree is a binary tree where the value of each node is greater than all values in the left subtree and less than all values in the right subtree. Basic operations on a balanced binary search tree take O(log n) time while unbalanced trees can take O(n) time. To search a value in a binary search tree, the value is compared to the root node and traversal continues left or right depending on whether the value is less than or greater than the root value.

Uploaded by

enatholdings
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

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

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!
Binary Search Trees (BSTs)

Where is the
smallest element?
Ans: leftmost
element

Where is the largest


element?
Ans: rightmost
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;
}
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
insert (target)
(target)

1.
1. IfIf head
head of of the
the treetree is
is null
null create
create new
new node
node toto store
store target;
target;
2.
2. Test
Test whether
whether the the target
target isis the
the same
same as as the
the key
key inin the
the
head
head ofof the
the tree
tree and
and return
return null
null (this
(this means
means that
that the
the
element
element cannotcannot be be added
added to to the
the tree)
tree)
3.
3. IfIf not
not true,
true, ifif target
target isis less
less than
than the
the key
key in
in the
the head
head insert
insert
the
the target
target inin thethe in
in the
the left
left subtree
subtree
4.
4. IfIf target
target isis greater
greater than than the
the key
key in
in the
the head
head
insert
insert the
the target
target in in the
the right
right subtree
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.)
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;
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);
}
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
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
BINARY SEARCH TREE: AN APPLICATION OF
BINARY TREES


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.
Application of Binary Search Tree

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 tree. The
first number in the list is placed in a node that is the root of the
binary tree with empty left and right sub-trees. The other numbers in
the list is than compared to the number in the root. If it is matches,
we have duplicate. If it is smaller, we examine the left sub-tree; if it
is larger we examine the right sub-tree. If the sub-tree is empty, the
number is not a duplicate and is placed into a new node at that
position in the tree. If the sub-tree is nonempty, we compare the
number to the contents of the root of the sub-tree and the entire
process is repeated with the sub-tree. A program for doing this
follows .
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
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 Interview Questions
1. Write a code that can find the sum of values in the left-subtree nodes
after entering the root node of the tree.[Microsoft Interview Question]
2. Write a code to delete a tree in C. [Amazon Interview Question]
3. 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]
4. Write a program that takes a binary tree and prints out elements of
each level at the same line. [Facebook Interview Question]
5. Write a function to check if two binary trees have the same
elements.[Facebook Interview Question]
6. Write a program to check if a given sum exists on a following path of
a binary search tree.[Adobe]
7. Write a program to count the number of leaf nodes in a binary
tree.[Adobe]
8. How to verify whether a binary tree is a binary search tree?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy