IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
1 Pertemuan 12 Binary Search Tree Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture3.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Department of Computer Science University of Maryland, College Park
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture1 introductions and Tree Data Structures 11/12/20151.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
David Stotts Computer Science Department UNC Chapel Hill.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Search Trees Chapter 7 Objectives
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Lecture No.16 Data Structures Dr. Sohail Aslam.
Binary Trees, Binary Search Trees
Lecture 9: Self Balancing Trees
CSC 143 Binary Search Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20 th Mar 2007 Binary Search Tree

2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Outline Concept of Binary Search Tree (BST) BST operations Find Insert Remove Running time analysis of BST operations

3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Binary Search Tree: Properties Elements have keys (no duplicates allowed). 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 the values of all the keys in the right subtree are larger than the key in X. The keys must be comparable. X <X>X

4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar Binary Search Tree: Examples

5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar Binary Search Tree: Examples

6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Basic Operations FindMin, FindMax, Find Insert Remove

7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 FindMin Find node with the smallest value Algorithm: Keep going left until you reach a dead end! Code: BinaryNode findMin(BinaryNode t) { if (t != null) while (t.left != null) t = t.left; return t; }

8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 FindMax Find node with the largest value Algorithm: Keep going right until you reach a dead end! Code: BinaryNode findMax(BinaryNode t) { if (t != null) while (t.right != null) t = t.right; return t; }

9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Find You are given an element to find in a BST. If it exists, return the node. If not, return null. Algorithm? Code?

10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Find: Implementation BinaryNode find(Type x, BinaryNode t) { while(t!=null) { if(x.compareTo(t.element)<0) t = t.left; else if(x.compareTo(t.element)>0) t = t.right; else return t; // Match } return null; // Not found }

11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Insertion: Principle When inserting a new element into a binary search tree, it will always become a leaf node

12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Insertion: Algorithm To insert X into a binary search tree: Start from the root If the value of X < the value of the root: X should be inserted in the left sub-tree. If the value of X > the value of the root: X should be inserted in the right sub-tree. Remember that a sub-tree is also a tree. We can implement this recursively!

13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Insertion: Implementation BinaryNode insert(Type x, BinaryNode t) { if (t == null) t = new BinaryNode (x); else if(x.compareTo(t.element)<0) t.left = insert (x, t.left); else if(x.compareTo(t.element)>0) t.right = insert (x, t.right); else throw new DuplicateItemException(x); return t; }

14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar Removing An Element 5 6 4

15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Removing An Element: Algorithm If the node is a leaf, simply delete it. If the node has one child, adjust parent’s child reference to bypass the node. If the node has two children: Replace the node’s element with the smallest element in the right subtree and then remove that node, or Replace the node’s element with the largest element in the left subtree and then remove that node Introduces new sub-problems: removeMin: Alternatively, removeMax

16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Removing Leaf

17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Removing Node With 1 Child

18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Removing Node With 1 Child

19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 removeMin BinaryNode removeMin(BinaryNode t) { if (t == null) throw new ItemNotFoundException(); else if (t.left != null) { t.left = removeMin(t.left); return t; } else return t.right; }

20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar Removing Node With 2 Children

21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar Removing Node With 2 Children 2 3

22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Removing Node With 2 Children

23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Removing Root

24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Remove BinaryNode remove(Type x, BinaryNode t) { if (t == null) throw new ItemNotFoundException(); if (x.compareTo(t.element)<0) t.left = remove(x, t.left); else if(x.compareTo(t.element)>0) t.right = remove(x, t.right); else if (t.left!=null && t.right != null) { t.element = findMin(t.right).element; t.right = removeMin(t.right); } else { if(t.left!=null) t=t.left; else t=t.right; } return t; }

25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 SLSL SRSR X k < S L + 1 SLSL SRSR X k == S L + 1 SLSL SRSR X k > S L + 1 Find k-th element

26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Find k-th element BinaryNode findKth(int k, BinaryNode t) { if (t == null) throw exception; int leftSize = (t.left != null) ? t.left.size : 0; if (k <= leftSize ) return findKth (k, t.left); else if (k == leftSize + 1) return t; else return findKth ( k - leftSize - 1, t.right); }

27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Analysis Running time for: Insert? Find min? Remove? Find? Average case: O(log n) Worst case: O(n)

28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 20 th Mar 2007 Binary Search Tree maintains the order of the tree. Each node should be comparable All operations take O(log n) - average case, when the tree is equally balanced. All operations will take O(n) - worst case, when the height of the tree equals the number of nodes. Summary