BINARY SEARCH TREE (BST) JAVA
Transcript of BINARY SEARCH TREE (BST) JAVA
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
1/40
BINARY SEARCH TREE (BST)BINARY SEARCH TREE (BST)
.
6.3.1 What is Binary Search Tree?
6.3.2 Advantages
6.3.3 Building Binary Search Tree. .
6.3.5 Binary Search Tree Implementation
6.3.6 Binary Search Tree Application
. . . r m ve a a ype6.3.6.2 Abstract Data Type (ADT)
1Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
2/40
Binary Search TreeBinary Search Tree
What is Binary Search Tree (BST)
.
Value in left sub tree are less than the value in the subsequent parentnode
Value in ri ht sub tree are reater than or e ual the value in that
Example : Binary Search Tree with 9 values
subsequent parent node
67
55 70
46 69
68
90
85 100
2Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
3/40
Binary Search Tree (cont)Binary Search Tree (cont)
Advantages of Binary Search Tree (BST)
BST is mostly used as data structure to solve complexproblems.
BST is used to store a lot of data in sorted order.
recurs on a gor m.
BST is efficient in accessing the data.
3Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
4/40
Building BSTBuilding BST
BST characteristics :
All nodes in LEFT sub tree are < ROOT node
>=
Usually data is unique (no duplicated data)
INORDER traversal on BST will give data in
ascending order. PREORDER or POSTORDER traversal
will give data in different order.
An t e of data can be stored in BST.
4Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
5/40
Building BST (cont)Building BST (cont)
Example (a) :
- -
5
1 6
9
3
42-1
-3 Depth: 4Leaves: 4
5Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
6/40
Building BST (cont)Building BST (cont)
Example (b) :
ata :
M
A
A I
DDepth: 4Leaves: 2
6Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
7/40
Building BST (cont)Building BST (cont)
Example (c) :
a a : ac , ompaq, pp e, amsung, os a, e
Hitachi
Compaq Samsung
Apple ToshibaDell
Depth: 2
7Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
8/40
BST TraversalBST Traversal
Example (a) :
Data : 5 6 1 3 7 9 2 -1 0 4 -3
5
1
0 73
942
-1
-
Inorder : -3, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9Preorder: 5, 1, 0, -1, -3, 3, 2, 4, 6, 7, 9
, , , , , , , , , ,
8Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
9/40
BST Traversal (cont)BST Traversal (cont)
Example (b) :
Data : M A H A D I
M
A
H
A I
D Inorder: A, A, D, H, I, MPreorder: M, A, H, A, D, I
Postorder: D A I H A M
9Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
10/40
BST Traversal (cont)BST Traversal (cont)
Example (c) :
Data : Hitachi, Compaq, Apple, Samsung, Toshiba, Dell
Hitachi
Compaq Samsung
Apple ToshibaDell
Inorder: Apple, Compaq, Dell, Hitachi, Samsung, ToshibaPreorder: Hitachi, Compaq, Apple, Dell, Samsung, Toshiba
Postorder: Apple, Dell, Compaq, Toshiba, Samsung, Hitachi
10Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
11/40
BST ImplementationBST Implementation
How to implement BST Data Structure?
uses ynam c storage space.
BST ADT has to be modified to suit the type of data to be
stored.
to locate proper position.
uses recurs on me o ec n que.
11Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
12/40
BST Implementation (cont.)BST Implementation (cont.)
How to represent BST?
Each node will contain 3 elements :1. Left node reference/link
.
3. Right node reference/link node
left rightdata
12Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
13/40
BST Implementation (cont.)BST Implementation (cont.)
Tree node Structure
Class : TreeNodeAttributes: leftNode // left node reference/link
data // represent data
rightNode // right node reference/link
Methods :
Constructor() // normal constructor
insert() // insert a TreeNode into a tree
13Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
14/40
BST Implementation (cont.)BST Implementation (cont.)
Binary Search Tree (BST) Structure
Class : BSTree
ttr utes:
root// left node reference
public BSTree() // default constructor
public void insertNode(element)
public int calcSize()private int calcSizeAll(node)
ublic double calcSum()
private void preorderHelper(node)
public void inorderTraversal()
rivate void inorderHel er(node)
private double calcSumAll(node)public int countVal(value)private int countValAll(node, value)
public void postorderTraversal()private void postorderHelper(node)
private void printCatAll(node, category)etc
14Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
15/40
Class BST for PDTClass BST for PDT
BST for Primitive Data Type (PDT)
// TreeNode definition
public class TreeNode {TreeNode left; // left node
n a a; a a em
TreeNode right; // right node
node
public TreeNode(int d)
data = d
left=right = null;}
15Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
16/40
Class BST for PDT (cont)Class BST for PDT (cont)
public insert (int d)
{ if (d < data)
e t == nu
left = new TreeNode(d);else
.
}
else if (d >= data)==
right = new TreeNode(d);
else
ri ht.insert(d);
}}
}// end TreeNode
16Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
17/40
Class BST for PDTClass BST for PDT ((cont)cont)
public class BSTree
{
// Construct an empty Tree of integers
public BSTree( )
{ root = null; }
// Insert a new node in the binary search tree., .
// Otherwise, call the insert method of class TreeNode.
public void insertNode( int d )
{
if ( root == null )root = new TreeNode( d );
else
root.insert d
}
17Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
18/40
Class BST for PDTClass BST for PDT (cont.)(cont.)
// Preorder Traversal
public void preorderTraversal()
preor er e per root ;
// Recursive method to perform preorder traversal
{
if ( node == null )
System.out.print( " " + node.data );
reorderHel er( node.left );
preorderHelper( node.right );}
18Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
19/40
BST Implementation (cont.)BST Implementation (cont.)
// Inorder Traversal
public void inorderTraversal()
nor er e per root ;
// Recursive method to perform inorder traversal
{
if ( node == null )
inorderHelper( node.left );
S stem.out. rint( node.data + " " );
inorderHelper( node.right );}
19Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
20/40
Class BST for PDTClass BST for PDT ((cont)cont)
// Postorder Traversal
public void postorderTraversal()
{ postorderHelper( root ); }
// Recursive method to perform postorder traversal
private void postorderHelper( TreeNode node )
{
if ( node == null )re urn;
postorderHelper( node.left );
.
System.out.print( node.data + " " );
}
20Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
21/40
Class BST for PDTClass BST for PDT ((cont)cont)
public int calcSize()
return ca c ze root ;
// Recursive method to perform counting without condition
{
if ( node == null )
return 1 + calcSizeAll( node.left )+calcSizeAll( node.right );
}
21Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
22/40
Class BST for PDTClass BST for PDT ((cont)cont)
public double calcSum()
{ return calcSumAll( root ); }
// Recursive method to perform calcalution without condition
pr va e ou e ca c um ree o e no e
{
if ( node == null )
return node.data + calcSumAll( node.left ) +
calcSumAll node.ri ht
}
22Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
23/40
Class BST for PDTClass BST for PDT ((cont)cont)
public int countVal(double val)
,
// Recursive method to perform counting with condition
,
{
if ( node == null )
return 0
if (node.data > v)
return 1 + countValAll( node.left )+
countValAll( node.right );
elsereturn countValAll (node.left) +countValAll(node.right);
}
23Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
24/40
Class BST for PDTClass BST for PDT ((cont)cont)
public void printcat()
{
System.out.println(Category = 2");int num1 = countcategory(root,2);
System.out.println (num1);
System.out.println(Category = 5");
int num2 = countcategory(root,5);ys em.ou .pr n n num ;
System.out.println(Category = 10");
int num3 = countcategory(root,10);
. .
}
24Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
25/40
Class BST for PDTClass BST for PDT ((cont)cont)
// Recursive method to perform calcalution with condition
,
{if ( node == null )
if (node.data == num)
return 1+countcategory( node.left,num )+countcategory( node.right,num );
else
return countcategory (node.left,num)+
countcategory(node.right,num);}
}// end of BSTree
25Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
26/40
BST Application for PDTBST Application for PDT
Main Application for BST (Primitive Data Type)
public static void main( String args[] ){
=
int intVal;
System.out.println( "Inserting the following values: " );
for ( int i = 1; i
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
27/40
BST Application for PDT (cont)BST Application for PDT (cont)
System.out.println ("\n Calculation Size Of Tree +
tree.calcSize);
ystem.out.pr nt n n um ree +tree.ca c um ;
System.out.println ("\nNumber of value > 50 is "+tree.countVal(50));
" ". .
tree.preorderTraversal();
System.out.println ( "\n\nInorder traversal" );
System.out.println ( "\n\nPostorder traversal" );
tree.postorderTraversal();
S stem.out. rintln(" nNumber of values with diff cate or ");
tree.printcat();}// end main
}// end BSTTest
27Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
28/40
Class BST for ADTClass BST for ADT
class Student
BST for Abstract Data Type (ADT) - Student class
private int id;
private float cgpa;
private double fee;
public Student() {};
public void setData(int i, float c, double f)
{ id = i;cgpa = c;
fee = f;
}
public float rtncgpa() { return cgpa; }
public double rtnfee() { return fee; }
28Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
29/40
Class BST forClass BST for ADTADT ((cont.)cont.)
class TreeNode {
TreeNode left; //left nodeStudent data; // data item
// Constructor: initialize data to d and make this a leaf node
public TreeNode( Student d ){
data = d;
left = right = null; // this node has no children
}
29Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
30/40
Class BST for ADTClass BST for ADT ((cont)cont)
// Insert a TreeNode into a Tree that contains nodes.
public void insert( Student d )
if d.rtnid < data.rtnid
{ if ( left == null )
left = new TreeNode( d );
else
left.insert( d );
}
else if ( d.rtnid() >= data.rtnid() ){
if ( right == null )
right = new TreeNode( d );
e se
right.insert( d );
}
} // end of TreeNode
30Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
31/40
Class BST for ADTClass BST for ADT ((cont)cont)
class BSTree {
private TreeNode root;
// Construct an empty Tree of integers
public BSTree() { root = null; }
/ Insert a new node in the binary search tree.
// If the root node is null, create the root node here.
// Otherwise, call the insert method of class TreeNode.pu c vo nser o e u en
{
if ( root == null )
=
elseroot.insert( d );
31Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
32/40
Class BST for ADTClass BST for ADT ((cont)cont)
// Preorder Traversal
ublic void reorderTraversal
{ preorderHelper( root ); }
// Recursive method to perform preorder traversal
private void preorderHelper( TreeNode node )
{
if ( node == null )return;
System.out.println( "Student's ID: " + node.data.rtnid());
ys em.ou .pr n n u en s : no e. a a.r ncgpa ;
preorderHelper( node.left );
preorderHelper( node.right );
32Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
33/40
Class BST for ADTClass BST for ADT ((cont)cont)
// Inorder Traversal
public void inorderTraversal()
{ inorderHelper( root ); }
// Recursive method to perform inorder traversal
private void inorderHelper( TreeNode node )
{
if ( node == null )re urn;
inorderHelper( node.left );
" ' ". . . .
System.out.println( "Student's CGPA: " + node.data.rtncgpa());inorderHelper( node.right );
33Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
34/40
Class BST for ADTClass BST for ADT ((cont)cont)
// Postorder Traversal
public void postorderTraversal()
postor er e per root ;
// Recursive method to perform postorder traversal
{
if ( node == null )
postorderHelper( node.left );
ostorderHel er( node.ri ht );
System.out.println( "Student's ID: " + node.data.rtnid());System.out.println( "Student's CGPA: " + node.data.rtncgpa());
}
34Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
35/40
Class BST for ADTClass BST for ADT ((cont)cont)
public int countVal()
return countValAll root
// Recursive method to perform counting with condition
private int countValAll( TreeNode node )
{
if ( node == null )
return 0;if (node.data.rtncgpa() >= 3.5)
return 1 + countValAll( node.left )+ countValAll( node.right );
else
re urn coun a no e. e coun a no e.r g ;
}
35Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
36/40
Class BST for ADTClass BST for ADT ((cont)cont)
public double calcNewVal()
// Recursive method to perform calcalution with condition
{
if ( node == null )
return 0;
double lessfee = 0.50 * node.data.rtnfee();
return lessfee + calcNewAll( node.left )+ calcNewAll( node.right );
}
36Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
37/40
Class BST for ADTClass BST for ADT ((cont)cont)
public void printcat()
System.out.println("Number of students with cgpa >= 2 and < 3");int num1 = countcategory(root,2,3);
. .
System.out.println("Number of students with cgpa >= 3 and < 4");
int num2 = countcategory(root,3,4);
System.out.println("Number of students with cgpa >= 1 and < 2");
int num3 = countcategory(root,1,2);
S stem.out. rintln (num3);
}
37Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
38/40
Class BST forClass BST for ADTADT (cont)(cont)
// Recursive method to perform calcalution with condition
rivate int countcate or TreeNode node int low int hi h
{ if ( node == null )
return 0;
if (node.data.rtncgpa() >= low && node.data.rtncgpa() < high)
return 1+countcategory( node.left,low, high ) +
countcategory( node.right,low,high );
elsereturn countcategory (node.left,low,high) +
countcategory(node.right,low,high);
}
en o ree
38Intersession May 2009 UiTMT (MP, EZ, NIK)
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
39/40
BST Application for ADTBST Application for ADT
public class BSTTestObject {
Main Application for ADT class
pu c stat c vo ma n tr ng args
{ BSTree tree = new BSTree();int id; float cg; double fee;Student stud;
for ( int i = 1; i
-
8/3/2019 BINARY SEARCH TREE (BST) JAVA
40/40
BST Application for ADTBST Application for ADT ((cont)cont)
tree.calcSize();
System.out.println("\nNumber of gpa with different category");
.
System.out.println ("\nAverage of GPA : " +
(tree.calcSum() / tree.calcSize()));
" >= "
System.out.println ("\nTotal fee after 50% discount is RM"+
tree.calcNewVal());
System.out.println ( " n nPreorder traversal" );tree.preorderTraversal();
System.out.println ( "\n\nInorder traversal" );
tree.inorderTraversal();
System.out.println ( " n nPostorder traversal" );tree.postorderTraversal();
}// end main
en c ass
40Intersession May 2009 UiTMT (MP, EZ, NIK)