Anda di halaman 1dari 40

BINARY SEARCH TREE (BST)

6.3 63

INTRODUCTION 6.3.1 6.3.2 6.3.3 6.3.4 634 6.3.5 6.3.6 What is Binary Search Tree? y Advantages Building Binary Search Tree Binary Search Tree Traversal Binary Search Tree Implementation Binary Search Tree Application 6.3.6.1 Primitive D t t 6 3 6 1 P i iti Data type 6.3.6.2 Abstract Data Type (ADT)

Intersession May 2009 UiTMT (MP, EZ, NIK)

Binary Search Tree


What is Binary Search Tree (BST)
BST with no duplicate node values (with unique key/id). key/id) Value in left sub tree are less than the value in the subsequent parent node Value in right sub tree are greater than (or equal) the value in that g g ( q ) subsequent parent node Example : Binary Search Tree with 9 values
67 55 46 68 69 70 90 85 100

Intersession May 2009 UiTMT (MP, EZ, NIK)

Binary Search Tree (cont)


Advantages of Binary Search Tree (BST) BST is mostly used as data structure to solve complex problems. BST is used to store a lot of data in sorted order. BST code is composed of the recursion algorithm algorithm. BST is efficient in accessing the data.

Intersession May 2009 UiTMT (MP, EZ, NIK)

Building BST
BST characteristics : All nodes in LEFT sub tree are < ROOT node All nodes in RIGHT 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. Any type of data can be stored in BST. y ype o da a ca s o ed S .
Intersession May 2009 UiTMT (MP, EZ, NIK) 4

Building BST (cont)


Example (a) : Data : 5 6 1 3 7 9 2 -1 0 4 -3 1 3
5 1 0 6 7 9

3 4

-1 -3

Depth: 4 Leaves: 4

Intersession May 2009 UiTMT (MP, EZ, NIK)

Building BST (cont)


Example (b) : Data : M A H A D I D
M A H A

I D

Depth: 4 Leaves: 2

Intersession May 2009 UiTMT (MP, EZ, NIK)

Building BST (cont)


Example (c) : Data Hitachi, C D t : Hit hi Compaq, Apple, Samsung, Toshiba, Dell A l S T hib D ll
Hitachi

Compaq

Samsung

Apple pp

Dell

Toshiba

Depth: 2 Leaves: 3
Intersession May 2009 UiTMT (MP, EZ, NIK) 7

BST Traversal
Example (a) : Data : 5 6 1 3 7 9 2 -1 0 4 -3
5 1 0 6 7 9

3 4

-1 -3

Inorder : -3, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9 Preorder: 5, 1, 0, -1, -3, 3, 2, 4, 6, 7, 9 Postorder: -3 -1 0 2 4 3 1 9 7 6 5 -3, -1, 0, 2, 4, 3, 1, 9, 7, 6,


Intersession May 2009 UiTMT (MP, EZ, NIK) 8

BST Traversal (cont)


Example (b) : Data : M A H A D I
M A H A

I D

Inorder: A, A, D, H, I, M Preorder: M, A, H, A, D, I Postorder: D, A, I, H, A, M os o de : , , , , ,


9

Intersession May 2009 UiTMT (MP, EZ, NIK)

BST Traversal (cont)


Example (c) : Data : Hitachi, Compaq, Apple, Samsung, Toshiba, Dell
Hitachi

Compaq

Samsung

Apple

Dell

Toshiba

Inorder: Apple, Compaq, Dell, Hitachi, Samsung, Toshiba Preorder: Hitachi, Compaq, Apple, Dell, Samsung, Toshiba Postorder: Apple, Dell, Compaq, Toshiba, Samsung, Hitachi

Intersession May 2009 UiTMT (MP, EZ, NIK)

10

BST Implementation
How to implement BST Data Structure? dynamic storage space. BST uses d i t BST ADT has to be modified to suit the type of data to be stored. ADT cant be common because data has to be compared can t to locate proper position. BST uses recursion method/technique. i th d/t h i

Intersession May 2009 UiTMT (MP, EZ, NIK)

11

BST Implementation (cont.)


How to represent BST? Each node will contain 3 elements : 1. Left node reference/link 2. 2 Data 3. Right node reference/link

node

left

data

right

Intersession May 2009 UiTMT (MP, EZ, NIK)

12

BST Implementation (cont.)


Tree node Structure
Class : Attributes: TreeNode leftNode data rightNode

// left node reference/link // represent data // right node reference/link

Methods : Constructor() insert()

// normal constructor // insert a TreeNode into a tree

Intersession May 2009 UiTMT (MP, EZ, NIK)

13

BST Implementation (cont.)


Binary Search Tree (BST) Structure
Class : BSTree Attributes: A ib root// left node reference Methods : public BSTree() // default constructor public void insertNode(element) public void preorderTraversal() private void preorderHelper(node) public void inorderTraversal() p private void inorderHelper(node) p ( ) public void postorderTraversal() private void postorderHelper(node)

public int calcSize() private int calcSizeAll(node) p public double calcSum() () private double calcSumAll(node) public int countVal(value) private int countValAll(node, value) public void printCat() private void printCatAll(node, category) etc

Intersession May 2009 UiTMT (MP, EZ, NIK)

14

Class BST for PDT


BST for Primitive Data Type (PDT)
// TreeNode definition public class TreeNode { TreeNode left; int data; TreeNode right;

// left node // data item d t it // right node

// constructor : initialize data to d and make this as a leaf node public TreeNode(int d) { data = d; ; left=right = null; }

Intersession May 2009 UiTMT (MP, EZ, NIK)

15

Class BST for PDT (cont)


public insert (int d) { if (d < data) { if ( left == null) l f ll) left = new TreeNode(d); else left.insert(d); left insert(d); } else if (d >= data) { if (right == null) right = new TreeNode(d); else right.insert(d); g ( ); } } } // end TreeNode

Intersession May 2009 UiTMT (MP, EZ, NIK)

16

Class BST for PDT (cont)


public 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. null here // Otherwise, call the insert method of class TreeNode. public void insertNode( int d ) { if ( root == null ) root = new TreeNode( d ); else oot. se t( root.insert( d ); }
Intersession May 2009 UiTMT (MP, EZ, NIK) 17

Class BST for PDT (cont.)


// Preorder Traversal public void preorderTraversal() { preorderHelper( root ); } d H l ( ) // Recursive method to perform preorder traversal private void preorderHelper( TreeNode node ) { if ( node == null ) return; System.out.print( " " + node.data ); p preorderHelper( node.left ); p ( preorderHelper( node.right ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

18

BST Implementation (cont.)


// Inorder Traversal public void inorderTraversal() {i inorderHelper( root ); } d H l ( ) // Recursive method to perform inorder traversal private void inorderHelper( TreeNode node ) { if ( node == null ) return; inorderHelper( node.left ); System.out.print( node.data + " " ); y p ( inorderHelper( node.right ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

19

Class BST for PDT (cont)


// Postorder Traversal public void postorderTraversal() { postorderHelper( root ); } // Recursive method to perform postorder traversal private void postorderHelper( TreeNode node ) { if ( node == null ) return; postorderHelper( node.left ); postorderHelper( node right ); node.right System.out.print( node.data + " " ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

20

Class BST for PDT (cont)


public int calcSize() { return calcSizeAll( root ) } l Si All( ); // Recursive method to perform counting without condition private int calcSizeAll( TreeNode node ) { if ( node == null ) return 0; return 1 + calcSizeAll( node.left )+calcSizeAll( node.right ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

21

Class BST for PDT (cont)


public double calcSum() { return calcSumAll( root ); } // Recursive method to perform calcalution without condition private double calcSumAll( T N d node ) i t d bl l S All( TreeNode d { if ( node == null ) return 0; return node.data + calcSumAll( node.left ) + calcSumAll( node.right ); ( g }

Intersession May 2009 UiTMT (MP, EZ, NIK)

22

Class BST for PDT (cont)


public int countVal(double val) { return countValAll( root val ); } root,val // Recursive method to perform counting with condition private int countValAll( TreeNode node double v ) node, { if ( node == null ) return 0; ; if (node.data > v) return 1 + countValAll( node.left )+ countValAll( node.right ); else return countValAll (node.left) +countValAll(node.right); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

23

Class BST for PDT (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); System.out.println System out println (num2); System.out.println(Category = 10"); int num3 = countcategory(root,10); System.out.println System out println (num3); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

24

Class BST for PDT (cont)


// Recursive method to perform calcalution with condition private void printCatAll( TreeNode node int num) node, { if ( node == null ) return 0; if (node.data == num) return 1+countcategory( node.left,num )+ g y( , ) countcategory( node.right,num ); else return countcategory (node.left,num)+ countcategory(node.right,num); } } // end of BSTree

Intersession May 2009 UiTMT (MP, EZ, NIK)

25

BST Application for PDT


Main Application for BST (Primitive Data Type)
public class BSTTest { public static void main( String args[] ) { BSTree tree = new BSTree(); int intVal; System.out.println( "Inserting the following values: " ); y p ( g g for ( int i = 1; i <= 10; i++ ) { intVal = ( int ) ( Math.random() * 100 ); System.out.print( intVal + " " ); tree.insertNode( intVal ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

26

BST Application for PDT (cont)


System.out.println ("\n Calculation Size Of Tree + tree.calcSize); System.out.println ("\ S S i l ("\nSum Of Tree "+tree.calcSum()); T " l S ()) System.out.println ("\nNumber of value > 50 is "+ tree.countVal(50)); System.out.println System out println ( "\n\nPreorder traversal" ); tree.preorderTraversal(); System.out.println ( "\n\nInorder traversal" ); tree.inorderTraversal(); System.out.println ( "\n\nPostorder traversal" ); tree.postorderTraversal(); System.out.println("\nNumber of values with diff category"); y p ( g y ); tree.printcat(); } // end main } // end BSTTest

Intersession May 2009 UiTMT (MP, EZ, NIK)

27

Class BST for ADT


BST for Abstract Data Type (ADT) - Student class
class Student { 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 int rtnid() { return id; } public float rtncgpa() { return cgpa; } public double rtnfee() { return fee; } }
Intersession May 2009 UiTMT (MP, EZ, NIK) 28

Class BST for ADT (cont.)


class TreeNode { // package access members TreeNode left; //left node Student data; // data item TreeNode right; // right node // Constructor: initialize data to d and make this a leaf node p public TreeNode( Student d ) ( { data = d; left = right = null; // this node has no children }

Intersession May 2009 UiTMT (MP, EZ, NIK)

29

Class BST for ADT (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 ); else l right.insert( d ); } } } // end of TreeNode
Intersession May 2009 UiTMT (MP, EZ, NIK) 30

Class BST for ADT (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. d h b h // If the root node is null, create the root node here. // Otherwise, call the insert method of class TreeNode. public void insertNode( Student d ) { if ( root == null ) root = new TreeNode( d ); else root.insert( d ); }
Intersession May 2009 UiTMT (MP, EZ, NIK) 31

Class BST for ADT (cont)


// Preorder Traversal public void preorderTraversal() { 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()); System.out.println( "Student's CGPA " + node.data.rtncgpa()); S t t i tl ( "St d t' CGPA: d d t t ()) preorderHelper( node.left ); preorderHelper( node.right ); }
Intersession May 2009 UiTMT (MP, EZ, NIK) 32

Class BST for ADT (cont)


// Inorder Traversal public void inorderTraversal() { inorderHelper( root ); } // Recursive method to perform inorder traversal private void inorderHelper( TreeNode node ) { if ( node == null ) return; inorderHelper( node.left ); System.out.println( Student s System out println( "Student's ID: " + node data rtnid()); node.data.rtnid()); System.out.println( "Student's CGPA: " + node.data.rtncgpa()); inorderHelper( node.right ); }
Intersession May 2009 UiTMT (MP, EZ, NIK) 33

Class BST for ADT (cont)


// Postorder Traversal public void postorderTraversal() { postorderHelper( root ); } d H l ( ) // Recursive method to perform postorder traversal private void postorderHelper( TreeNode node ) { if ( node == null ) return; postorderHelper( node.left ); p postorderHelper( node.right ); p ( g System.out.println( "Student's ID: " + node.data.rtnid()); System.out.println( "Student's CGPA: " + node.data.rtncgpa()); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

34

Class BST for ADT (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 return countValAll ( d l ft) + countValAll(node.right); t tV lAll (node.left) tV lAll( d i ht) }

Intersession May 2009 UiTMT (MP, EZ, NIK)

35

Class BST for ADT (cont)


public double calcNewVal() { return calcNewAll( root ); } // Recursive method to perform calcalution with condition private double calcNewAll( TreeNode node ) { if ( node == null ) return 0; ; double lessfee = 0.50 * node.data.rtnfee(); return lessfee + calcNewAll( node.left )+ calcNewAll( node.right ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

36

Class BST for ADT (cont)


public void printcat() { System.out.println("Number of students with cgpa >= 2 and < 3"); int num1 = countcategory(root,2,3); System.out.println System out println (num1); System.out.println("Number of students with cgpa >= 3 and < 4"); int num2 = countcategory(root,3,4); System.out.println (num2); System.out.println("Number of students with cgpa >= 1 and < 2"); int num3 = countcategory(root,1,2); System.out.println (num3); y p ( ); }

Intersession May 2009 UiTMT (MP, EZ, NIK)

37

Class BST for ADT (cont)


// Recursive method to perform calcalution with condition private int countcategory( TreeNode node, int low, int high) { 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 ); else return countcategory (node.left,low,high) + countcategory(node.right,low,high); } } // end of BST d f BSTree

Intersession May 2009 UiTMT (MP, EZ, NIK)

38

BST Application for ADT


Main Application for ADT class
public class BSTTestObject { public static void main( String args[] ) bli i id i (S i [] { BSTree tree = new BSTree(); int id; float cg; double fee;Student stud; for ( int i = 1; i <= 3; i++ ) { id = Integer.parseInt (JOptionPane.showInputDialog("Enter ID: ")); cg = Float.parseFloat (JOptionPane.showInputDialog ("Enter CGPA: ")); fee = Double.parseDouble (JOptionPane.showInputDialog ( ("Enter FEE: ")); )); stud = new Student(); stud.setData ( id, cg, fee ); tree.insertNode( stud ); }
Intersession May 2009 UiTMT (MP, EZ, NIK) 39

BST Application for ADT (cont)


tree.calcSize(); System.out.println("\nNumber of gpa with different category"); tree.printcat(); tree printcat(); System.out.println ("\nAverage of GPA : " + (tree.calcSum() / tree.calcSize())); System.out.println ("\nNumber of cgpa >= 3.5 is: "+ tree.countVal()); ( \nNumber + System.out.println ("\nTotal fee after 50% discount is RM"+ tree.calcNewVal()); System.out.println ( "\n\nPreorder traversal" ); y p tree.preorderTraversal(); System.out.println ( "\n\nInorder traversal" ); tree.inorderTraversal(); System.out.println ( "\n\nPostorder traversal" ); tree.postorderTraversal(); } // end main } // end class d l
Intersession May 2009 UiTMT (MP, EZ, NIK) 40

Anda mungkin juga menyukai