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

Tree (BST) Implementation Half

The document discusses binary search trees, which provide efficient insertion and deletion operations like linked lists while also supporting efficient search operations like arrays. It explains that a binary search tree is a binary tree where all left descendants of a node are less than or equal to the node's value, and all right descendants are greater than or equal. The document provides examples of binary search tree insertion, searching, and traversal algorithms. It notes that binary search trees allow for fast access to specific nodes due to short paths from the root.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Tree (BST) Implementation Half

The document discusses binary search trees, which provide efficient insertion and deletion operations like linked lists while also supporting efficient search operations like arrays. It explains that a binary search tree is a binary tree where all left descendants of a node are less than or equal to the node's value, and all right descendants are greater than or equal. The document provides examples of binary search tree insertion, searching, and traversal algorithms. It notes that binary search trees allow for fast access to specific nodes due to short paths from the root.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Structures

Binary Search Trees

M. Ehtasham Ul Haq
Chenab College of Engineering and Technology (CCET)

Introduction
When we store ordered data in an array, we
have a very efficient search algorithm, the
binary search.
However, we have very inefficient insertion
and deletion algorithms that require shifting
data in the array.
To provide for efficient insertions and
deletions, we developed the linked list.
The problem with linked lists, however, is
that their search algorithms are sequential
searches, which are very inefficient.
Cant we get the best of both worlds (i.e.,

Binary Search Trees


The binary search tree is our answer!
The binary search tree is a binary tree with
the following properties:

All items (keys) in the left subtree are less than


the roots key.
All items (keys) in the right subtree are greater
than the roots key.
Each subtree is, itself, a binary search tree.

Binary Search Trees


Assume each node of a binary tree stores a data
item
Assume data items are of some type that be
ordered and all items are distinct. No two items
have the same value.

A binary search tree is a binary tree


such that
for every node X in the tree:

the values of all the items in its left subtree are


smaller than the value of the item in X
the values of all items in its right subtree are
greater than the value of the item in X.

Binary Search Trees

Here you can


see the basic
structure of a
binary search
tree.

Binary Search Trees


A binary search tree is organized as the
name suggests
A tree can be represented by a linked data
structure where each node is an object
In addition to a key field, each node contains
fields left, and right, that correspond to its
left child and right child

Key
Key Left
Left Right
Right

Key
Key LeftRight

Key
Key LeftRight

Binary Search Trees (Note


Structure)
Key
Key Left
Left Right
Right
class Node
{
Key
Key LeftRight
Key
Key
int iData; //key value
double dData;
Node leftChild;
// this nodes left child
Node rightChild;
// this nodes right child.
public void displayNode()
{
System.out.println("Data: " + dData);
}
// end display()
}
// end class Node.

LeftRight

Binary Search Trees


A binary search tree
6
2
1

8
4

6
2
1

8
4

Not a binary search tree,


but a binary tree

Binary Search Trees

Here we see examples of


binary trees that are not
binary search trees
why?

a) 22 > 17
b) 11 < 17
c) 11 > 6
d) 22 > 17

Search Tree Property: Duplicate


Keys
The search tree property does not say what happens with
the elements with the same key. In our examples, all the
keys will be distinct but in practice it is important to cover
the case of multiple elements with the same key (duplicate
keys).
The duplicate keys can all be kept in the left subtree, or all
in the right subtree. It doesnt matter which we choose, but
it matters that the choice is the same for the whole
implementation.
Another issue: with duplicate keys, it is important to have
extra operations in the interface: getAll, and removeAll

Binary Search Trees


Binary trees offer short paths from root to
specific node
Data is organized by key value
Operations:

Insertion
Search
Traversal
Deletion
Find Minimum

Find the item that has the minimum value in the tree

Find Maximum

Find the item that has the maximum value in the


tree

Print

Print the values of all items using a appropriate

Inserting a value into a BST


The first value inserted goes at the root
Every node inserted becomes a leaf
Descend left or right depending on value

Inserting Item 5 to the Tree


t
Tree root node

t
2

NULL

NULL

NULL

NULL

t
1

NULL

NULL

NULL

NULL

NULL

New Node

Binary Search Trees (Insertion)


public void insert(int id, double dd) { // we are within
TreeApp.java file

Node newNode = new Node();


// make new node
newNode.iData = id;
// insert data
newNode.dData = dd;
if(root==null)
// no node in root.
root = newNode;
// if true, we are done.
else {
// else not root
Node current = root;
// current is a pointer to root
node
Node parent;
// creating a reference (Needed
previous)
while(true) {
// here is our search to find right
spot
parent = current;
if (id < current.iData) {
// go left?
current = current.leftChild;
// maybe no
left. So:
if(current == null) {
// if no left

Binary Search Trees (Insertion)


else {

}
}
}

// current >= current.iData?

If so, go right?

current = current.rightChild;
if(current == null) // if no right child
{
parent.rightChild = newNode;.
return; // we are done if we get here.
}
// end if
}
// end else
// end while
// end else not root
// end insert()

Binary Search Trees (Searching)


public Node find (int key) {
// we are within TreeApp.java
file
if (root == null) {
System.out.println(Tree is Emply);
return null;
}
Node current = root;
// start at root
while (current.iData != key) { // if no match
if (key < current.iData)
current = current.leftChild;
else
current = current.rightChild;
if (current == null)
return null; // not found; boundary condition
}
// end while
current.displayNode();
return current;
// returns reference of node
}
// end find()

Binary Search Trees (Searching)


import java.util.Scanner;
class TreeApp {
public static void main (String [ ] args) {
Tree theTree = new Tree();
// make a tree.
// creates an object of type Tree. (previous slide)
theTree.insert (50, 1.5);
//insert three nodes
theTree.insert(25, 1.7);
// invoking tree methods
System.out.println("Enter number to Find");
Scanner input= new Scanner(System.in);
int no=input.nextInt();
Node found = theTree.find(no);
// find node with input key
if (found != null)
// So what to do if Found
System.out.println ("Node Found with key " + no);
else
System.out.println ("Not Not Found with key " + no);
}
// end main()
}
// end class TreeApp.

Binary Search Trees (Searching)


import java.util.Scanner;
class TreeApp {
public static void main (String [ ] args) {
Tree theTree = new Tree();
// make a tree.
// creates an object of type Tree. (previous slide)
theTree.insert (50, 1.5);
//insert three nodes
theTree.insert(25, 1.7);
// invoking tree methods
System.out.println("Enter number to Find");
Scanner input= new Scanner(System.in);
int no=input.nextInt();
Node found = theTree.find(no);
// find node with input key
if (found != null)
// So what to do if Found
System.out.println ("Node Found with key " + no);
else
System.out.println ("Not Not Found with key " + no);
}
// end main()
}
// end class TreeApp.

You might also like