objectives
play

Objectives Memahami sifat dari Binary Search Tree (BST) Struktur - PowerPoint PPT Presentation

Objectives Memahami sifat dari Binary Search Tree (BST) Struktur Data & Algoritme Memahami operasi-operasi pada BST Memahami kelebihan dan kekurangan dari BST ( Data Structures & Algorithms ) Binary Search Tree Denny (


  1. Objectives � Memahami sifat dari Binary Search Tree (BST) Struktur Data & Algoritme � Memahami operasi-operasi pada BST � Memahami kelebihan dan kekurangan dari BST ( Data Structures & Algorithms ) Binary Search Tree Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0 - I nternal Use Only SDA/ TOPI C/ V2.0/ 2 Outline Properties of Binary Search Tree � Properties of Binary Search Tree (BST) � For every node X in the tree, the values of all the keys in the left subtree are smaller than the key in X and � Operation the values of all the keys in the right subtree are � Insert larger than the key in X. � find � So, the key should be comparable. � remove X <X >X SDA/ TOPI C/ V2.0/ 3 SDA/ TOPI C/ V2.0/ 4 1

  2. Binary Search Tree Binary Search Tree 3 3 1 1 7 2 9 2 1 3 2 3 2 1 2 1 5 2 3 6 1 3 SDA/ TOPI C/ V2.0/ 5 SDA/ TOPI C/ V2.0/ 6 Basic Operations Insertion � insert � Penyisipan sebuah elemen baru dalam binary search tree, elemen tersebut pasti akan menjadi leaf � findMin and findMax � remove 10 15 2 12 1 5 14 3 6 SDA/ TOPI C/ V2.0/ 7 SDA/ TOPI C/ V2.0/ 8 2

  3. Insertion: algorithm Insertion BinaryNode insert(int x, BinaryNode t) � Insert X into a binary search tree: { � start from the root. if (t == null) { � If the value of X is less than the value of the root, then t = new BinaryNode (x, null, null); X should be inserted on the left sub-tree. } else if (x < t.element) { � On the other hand, if the value of X is greater than the t.left = insert (x, t.left); value of the root, then X should be inserted on the } else if (x > t.element) { right sub-tree. t.right = insert (x, t.right); � Remember that, a sub tree is also a tree. So, the } else { problem to insert an element in the sub-tree is same throw new DuplicateItem(“exception”); as the problem to insert an element in the root. } � So? return t; } � We can attack this problem with recursive approach. SDA/ TOPI C/ V2.0/ 9 SDA/ TOPI C/ V2.0/ 10 FindMin FindMax � Mencari node yang memiliki nilai terkecil. � Mencari node yang memiliki nilai terbesar � Algorithm: � Algorithm? � ke kiri terus sampai buntu….:) � Code? � Code: BinaryNode findMin (BinaryNode t) { if (t == null) throw exception ; while (t.left != null) { t = t.left; } return t; } SDA/ TOPI C/ V2.0/ 11 SDA/ TOPI C/ V2.0/ 12 3

  4. Find Remove � Diberikan sebuah nilai yang harus dicari dalam sebuah BST. Jika ada elemen tersebut, return node 8 tersebut. Jika tidak ada, return null. 12 � Algorithm? 4 4 � Code? 7 9 6 6 2 1 1 5 3 5 5 3 6 SDA/ TOPI C/ V2.0/ 13 SDA/ TOPI C/ V2.0/ 14 Remove Removing 6 � if the node is a leaf (has no child), no problemo… delete it immediately 8 � if the node has one child: its parent adjusts a child reference to bypass the node. 12 4 � if the node has two children? � replace the item in this node with the smallest item in the right subtree and then remove that node, or 6 1 � replace the item in this node with the biggest item in the left subtree and then remove that node � introduce new sub-problems: 5 3 � removeMin, removeMax SDA/ TOPI C/ V2.0/ 15 SDA/ TOPI C/ V2.0/ 16 4

  5. After 6 removed removeMin BinaryNode removeMin(BinaryNode t) { 8 if (t == null) throw exception ; 12 4 if (t.left != null) { t.left = removeMin (t.left); } else { 6 1 t = t.right; } return t; } 3 5 SDA/ TOPI C/ V2.0/ 17 SDA/ TOPI C/ V2.0/ 18 removeMax Removing 2 � code? 7 9 2 1 5 3 4 SDA/ TOPI C/ V2.0/ 19 SDA/ TOPI C/ V2.0/ 20 5

  6. After 2 deleted Removing Root 9 7 7 2 12 9 2 2 3 10 14 1 5 1 5 X 3 9 11 3 4 4 SDA/ TOPI C/ V2.0/ 21 SDA/ TOPI C/ V2.0/ 22 Remove Find k-th element BinaryNode remove(int x, BinaryNode t) { if (t == null) throw exception; if (x < t.element) { t.left = remove(x, t.left); X X X } else if (x > t.element) { t.right = remove(x, t.right); } else if (t.left != null && t.right != null) { t.element = findMin(t.right).element; S R S R S R t.right = removeMin(t.right); S L S L S L } else { t = (t.left != null) ? t.left : t.right; } return t; k < S L + 1 k == S L + 1 k > S L + 1 } SDA/ TOPI C/ V2.0/ 23 SDA/ TOPI C/ V2.0/ 24 6

  7. Find k-th element Analysis BinaryNode findKth(int k, BinaryNode t) � Runnning time for: { � insert? if (t == null) throw exception; � Find min? int leftSize = (t.left != null) ? � remove? t.left.size : 0; � Find? � Worst case: O(n) if (k <= leftSize ) { return findKth (k, t.left); } else if (k == leftSize + 1) { return t; } else { return findKth ( k - leftSize - 1, t.right); } } SDA/ TOPI C/ V2.0/ 25 SDA/ TOPI C/ V2.0/ 26 Summary Further Reading � Binary Search Tree maintains the order of the tree. � http://telaga.cs.ui.ac.id/WebKuliah/IKI101 00/1998/handout/handout16.html � Each node should be comparable � Chapter 18 � All operations take O(log n) - average case, when the tree equally balanced. � All operations will take O(n) - worst case, when the all the height of the tree equals with the total of the nodes. SDA/ TOPI C/ V2.0/ 27 SDA/ TOPI C/ V2.0/ 28 7

  8. What’s Next � AVL tree SDA/ TOPI C/ V2.0/ 29 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend