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

Tree - Binary Search Tree

Tree

Uploaded by

Tahir2 Siddiqui
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)
13 views

Tree - Binary Search Tree

Tree

Uploaded by

Tahir2 Siddiqui
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/ 11

Data Structures – CSC211

Binary Search Tree (BST)


Introduction
 Implementation of Binary Search Algorithm on
Dynamic Data Structure is complex and
expensive
 Searching a Key or a Value in an ordinary
Binary Tree is very slow
 This can be made faster by sorting the values
in the Binary Tree in an order
 Then use Binary Search method to search the
values
 Corresponding tree is called a Binary Search
Tree (BST)

2
Binary Search Tree
 A Binary Search Tree is a binary tree, which is
either empty or satisfies the following
properties :

1. Every node has a value and no two nodes have


the same value (i.e., all the values are unique).

2. If there exists a left child or left sub tree then its


value is less than the value of the root.

3. The value(s) in the right child or right sub tree is


larger than the value of the root node.

3
Example
 Here the root node information
is 50.

 Note that the right sub tree


node’s value is greater than 50,
and the left sub tree nodes
value is less than 50.

 Again right child node of 25 has


greater values than 25 and left
child node has smaller values
than 25.

 Similarly right child node of 75


has greater values than 75 and
left child node has smaller
values than 75 and so on.
4
Binary Search Tree – Operations
 The operations performed on binary tree can
also be applied to Binary Search Tree (BST)
1. Inserting a node
2. Searching a node
3. Deleting a node
 Another most commonly performed operation
on BST is, traversal
 The tree traversal algorithm (pre-order, post-order
and in-order) are the standard way of traversing a
binary search tree

5
Create BST
 A BST is constructed by the repeated insertion
of new nodes to the tree structure.
 Inserting a node in to a tree is achieved by
performing two separate operations.
 The tree must be searched to determine where the
node is to be inserted.
 Then the node is inserted into the tree.

 Construct BST of following Input Data:


55, 78, 23, 67, 10, 90, 15, 63, 40, 59

6
BST Structure
struct bst
{
int data;
struct bst *left;
struct bst *right;
} *root=NULL;

7
Finding Node
public Node find(int key) // find node with given key
{ // (assumes non-empty tree)
Node current = root; // start at root
while(current.iData != key) // while no
match,
{
if(key < current.iData) // go left?
current = current.leftChild;
else
current = current.rightChild; // or go right?
if(current == null)
return null; // didn’t find it
}
return current; // found it
}

8
Inserting Node
public void insert(int id, double dd)
{
Node newNode = new Node(); // make new node
newNode.iData = id; // insert data
newNode.dData = dd;
if(root==null) // no node in root
root = newNode;
else // root occupied
{
Node current = root; // start at root
Node parent;
while(true) // (exits internally)
{
parent = current;

9
Insertion
if(id < current.iData) // go left?
{
current = current.leftChild;
if(current == null)
{ // insert on left
parent.leftChild = newNode;
return;
}
} // end if go left
else // or go right?
{

10
Insertion
current = current.rightChild;
if(current == null)
{ // insert on right
parent.rightChild = newNode;
return;
}
} // end else go right
} // end while
} // end else not root
} // end insert()

11

You might also like