1/14/20161 BST Operations 15-111 Data Structures Ananda Gunawardena.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

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.
Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
BST Data Structure A BST node contains: A BST contains
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.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
CSC 143 Trees [Chapter 10].
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Binary Search Trees Data Structures Ananda Gunawardena
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
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.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
353 3/30/98 CSE 143 Trees [Sections , 13.6,13.8]
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Lecture 9 Binary Trees Trees General Definition Terminology
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Chapter 12 – Data Structures
Trees Chapter 15.
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Tree Traversals – reminder
Tree Traversals – reminder
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Search Tree.
Presentation transcript:

1/14/20161 BST Operations Data Structures Ananda Gunawardena

1/14/20162 Traversal Algorithms Inorder traversal –Left Root Right Preorder traversal –Root Left Right Postorder traversal –Left Right Root Level order traversal

1/14/20163 Expression Trees Draw the expression tree of ( ) * 3 - ( 4 ^ ( ) ) Perform preorder traversal Perform postorder traversal

1/14/20164 Level order or Breadth-first traversal Visit nodes by levels Root is at level zero At each level visit nodes from left to right Called “Breadth-First- Traversal(BFS)”

1/14/20165 Level order or Breadth-first traversal enqueue the root while (the queue is not empty) { dequeue the front element print it enqueue its left child (if present) enqueue its right child (if present) } BFS Algorithm

1/14/20166 Tree Operations Insert Operation (recursive) Insert(Node, T) = Node if T is empty = insert(Node, T.left) if Node < T = insert(Node, T.right) if Node > T Homework: Write an iterative version of insert

1/14/20167 Insert code public void insert(Comparable key, Object item) { int result = key.compareTo(this.key); if (result < 0) { // to the left if (left == null) left = new BinaryNode(key, item); else left.insert(key,item); } else { // to the right if (right == null) right = new BinaryNode(key, item); else right.insert(key,item); } Note: Assume left and right references are public

1/14/20168 Tree Operations Search Operation Search(Node, T) = false if T is empty = true if T = Node = search(Node, T.left) if Node < T = search(Node, T.right) if Node > T Homework: Write an iterative version of search

1/14/20169 Insertions Insertions in a BST are very similar to searching: find the right spot, and then put down the new element as a new leaf. We will not allow multiple insertions of the same element, so there is always exactly one place for the new entry. How do we handle duplicate elements in a tree? What is the complexity of an algorithm to determine if there are duplicate elements in a tree?

1/14/ Delete Node 3 cases –Case 1: Leaf node –Case 2: Node with one child –Case 3: Node with two children 12 3

1/14/ Delete Node Case 1 – Node is a leaf node –Just delete the leaf node –No changes to any subtree as a result Case 2 – Node has one child –If child is a left child, make the parent pointer go to left child

1/14/ Delete Node Case 3 – Node has 2 children –This is a complicated case –Best strategy is to find the Largest node in the left subtree OR Smallest node in the right subtree –Swap the data of the node to be deleted with one of the nodes as above –Delete the leaf node

1/14/ Delete code public BinaryNode delete(Comparable key) { int result = key.compareTo(this.key); if (result != 0) { // not there yet if (result < 0 && left != null) left = left.delete(key); if (result > 0 && right != null) right = right.delete(key); return this; } if (left == null && right == null) return null; // case 1 (not actually needed) if (left == null) return right; // case 2 if (right == null) return left; // case 2 BinaryNode next; // case 3 for (next = right; next.left != null; next = next.left); this.key = next.key; this.item = next.item; right = right.delete(this.key); return this; }

1/14/ Other Operations Counting nodes Height of a tree

1/14/ Other Operations Max node Min Node

1/14/ Good Tree A good tree has the minimum search depth for any node But in a "good" BST we have depth of T = O( log # nodes ) Theorem: If the tree is constructed from n inputs given in random order, then we can expect the depth of the tree to be log 2 n. But if the input is already (nearly, reverse,…) sorted we are in trouble.

1/14/ Forcing good behavior We can show that for any n inputs, there always is a BST containing these elements of logarithmic depth. But if we just insert the standard way, we may build a very unbalanced, deep tree. Can we somehow force the tree to remain shallow? At low cost? Next we will discuss balanced trees