Fall, 2015 Prof. Jungkeun Park

Similar documents
Trees. (Trees) Data Structures and Programming Spring / 28

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

8. Binary Search Tree

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Algorithms. AVL Tree

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

CS 315 Data Structures mid-term 2

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

A set of nodes (or vertices) with a single starting point

CS102 Binary Search Trees

Lecture 23: Binary Search Trees

CMSC 341 Lecture 14: Priority Queues, Heaps

Self-Balancing Search Trees. Chapter 11

Analysis of Algorithms

Binary Search Trees. Analysis of Algorithms

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

CS 261 Data Structures. AVL Trees

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Cpt S 122 Data Structures. Data Structures Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Section 1: True / False (1 point each, 15 pts total)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Trees. Eric McCreath

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Binary Trees and Binary Search Trees

Programming II (CS300)

Binary Trees, Binary Search Trees

TREES. Trees - Introduction

Tree traversals and binary trees

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Recall: Properties of B-Trees

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

Fall, 2015 Prof. Jungkeun Park

Balanced Binary Search Trees. Victor Gao

Chapter 20: Binary Trees

Dictionaries. Priority Queues

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Data Structures in Java

CIS265/ Trees Red-Black Trees. Some of the following material is from:

Lecture 15 Binary Search Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Lecture Notes on Binary Search Trees

CmpSci 187: Programming with Data Structures Spring 2015

CMSC 341 Lecture 15 Leftist Heaps

CSI33 Data Structures

Dynamic Access Binary Search Trees

Lecture 15 Notes Binary Search Trees

Data Structures and Algorithms

CMSC 341 Lecture 15 Leftist Heaps

9. Heap : Priority Queue

Advanced Set Representation Methods

Transform & Conquer. Presorting

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Balanced Search Trees. CS 3110 Fall 2010

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CS Transform-and-Conquer

BBM 201 Data structures

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

CISC 235: Topic 4. Balanced Binary Search Trees

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

Data Structures and Algorithms for Engineers

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

B-Trees. CS321 Spring 2014 Steve Cutchin

void insert( Type const & ) void push_front( Type const & )

Binary Trees. Examples:

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

CHAPTER 5. Trees CHAPTER 5 1/70

LECTURE 18 AVL TREES

Data Structures and Algorithms

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Programming Languages and Techniques (CIS120)

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

AVL Trees Heaps And Complexity

AVL Trees (10.2) AVL Trees

CS200: Balanced Search Trees

Lecture 6: Analysis of Algorithms (CS )

Module 9: Binary trees

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

13.4 Deletion in red-black trees

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS350: Data Structures AVL Trees

Maps; Binary Search Trees

CS350: Data Structures Red-Black Trees

Transcription:

Data Structures and Algorithms Binary Search Trees Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea in Univ. of North Texas. http://www.cs.unt.edu/~rada/csce3110

A Taxonomy of Trees General e Trees any number of children / node Binary Trees max 2 children / node Heaps parent < (>) children Binary Search Trees 2

Binary Trees Binary ayseac search tree tee Every element has a unique key. The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree. The left and right subtrees are also binary search trees. 3

Binary Search Trees Binary Search Trees (BST) are a type of Binary Trees with a special organization of data. This data organization leads to O(log n) complexity for searches, insertions and deletions in certain types of the BST (balanced trees). O(h) in general 4

Binary Search Algorithm Binary ayseac Search ago algorithm of an array ayof sorted items reduces the search space by one half after each comparison 34 41 56 63 72 89 95 0 1 2 3 4 5 6 34 41 56 72 89 95 0 1 2 4 5 6 34 56 0 2 72 95 4 6 5

Organization Rule for BST the values in all nodes in the left subtree of a node are less than the node value the values in all nodes in the right subtree of a node are greater than the node values 63 41 89 34 56 72 95 6

Binary Tree typedef struct tnode *ptnode; typedef struct node { short int key; ptnode right, left; } ; 7

BST Operations: Search Searching in the BST method search(key) implements the binary search based on comparison of the items in the tree the items in the BST must be comparable (e.g integers, string, etc.) The search starts at the root. It probes down, comparing the values in each node with the target, till it finds the first item equal to the target. Returns this item or null if there is none. 8

Search in BST - Pseudocode if the tree is empty return NULL else if the item in the node equals the target return the node value else if the item in the node is greater than the target return the result of searching the left subtree else if the item in the node is smaller than the target return the result of searching the right subtree 9

Search in a BST: C code Ptnode search(ptnode root, int key) { /* return a pointer to the node that contains key. If there is no such node, return NULL */ } if (!root) return NULL; if (key == root->key) return root; if (key < root->key) return search(root->left,key); return search(root->right,key); 10

BST Operations: Insertion method insert(key) ey) places a new item near the frontier of the BST while retaining its organization of data: starting at the root it probes down the tree till it finds a node whose left or right pointer is empty and is a logical place for the new value uses a binary search to locate the insertion point is based on comparisons of the new item and values of nodes in the BST Elements in nodes must be comparable! 11

Case 1: The Tree is Empty Set the root to a new node containing the item Case 2: The Tree is Not Empty py Call a recursive helper method to insert the item 10 10 > 7 7 5 9 10 > 9 4 6 8 10 12

Insertion in BST - Pseudocode if tree is empty create a root node with the new key else compare key with the top node if key = node key replace the node with the new value else if key > node key compare key with the right subtree: if subtree is empty create a leaf node else add key in right subtree else key < node key compare key with the left subtree: if the subtree is empty create a leaf node else add key to the left subtree 13

Insertion into a BST: C code void insert (ptnode *node, int key) { ptnode ptr, temp = search(*node, key); if (temp!(*node)) { ptr = (ptnode) malloc(sizeof(tnode)); if (IS_FULL(ptr)) { fprintf(stderr, The memory is full\n ); exit(1); } ptr->key = key; ptr->left = ptr->right = NULL; if (*node) if (key<temp->key) temp->left=ptr; ptr; else temp->right = ptr; else *node = ptr; } } 14

BST Shapes The order of supplying the data determines es where e it is placed in the BST, which determines the shape of the BST Create BSTs from the same set of data presented each time in a different order: a) 17 4 14 19 15 7 9 3 16 10 b) 9 10 17 4 3 7 14 16 15 19 c)191716151410974 17 16 15 14 10 9 7 4 3can you guess this shape? 15

BST Operations: Removal removes es a specified ed item from the BST and adjusts the tree uses a binary search to locate the target item: starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree) removal of a node must not leave a gap in the tree, 16

Removal in BST - Pseudocode method remove (key) I if the tree is empty return false II Attempt to locate the node containing the target using the binary search algorithm if the target is not found return false else the target is found, so remove its node: Case 1: if the node has 2 empty subtrees replace the link in the parent with null Case 2: if the node has a left and a right subtree - replace the node's value with the max value in the left subtree - delete the max node in the left subtree 17

Removal in BST - Pseudocode Case 3: if the node has no left child - link the parent of the node - to the right (non-empty) subtree Case 4: if the node has no right child - link the parent of the target - to the left (non-empty) subtree 18

Removal in BST: Example Case 1: removing a node with 2 EMPTY SUBTREES S parent 7 cursor 5 9 Removing 4 replace the link in the parent with null 4 6 8 10 5 7 9 6 8 10 19

Removal in BST: Example Case 2: removing a node with 2 SUBTREESS replace the node's value with the max value in the left subtree delete the max node in the left subtree Removing 7 cursor cursor What other element can be used as replacement? 7 6 5 9 5 9 4 6 8 10 4 8 10 20

Removal in BST: Example Case 3: removing a node with 1 EMPTY SUBTREE the node has no left child: link the parent of the node to the right (non-empty) subtree parent cursor parent 7 7 5 9 cursor 5 9 6 8 10 6 8 10 21

Removal in BST: Example Case 4: removing a node with 1 EMPTY SUBTREE the node has no right child: link the parent of the node to the left (non-empty) subtree Removing 5 parent parent cursor 7 cursor 7 5 9 5 9 4 8 10 4 8 10 22

Analysis of BST Operations The complexity of operations o get, insert and remove e in BST is O(h), where h is the height. O(log n) when the tree is balanced. The updating operations cause the tree to become unbalanced. The tree can degenerate to a linear shape and the operations will become O (n) 23

Best Case BST tree = new BST(); tree.insert ("E"); tree.insert ("C"); tree.insert ("D"); tree.insert ("A"); tree.insert ("H"); tree.insert ("F"); tree.insert ("K"); Output: >>>> Items in advantageous order: K H F E D C A 24

Worst Case BST tree = new BST(); for (int i = 1; i <= 8; i++) tree.insert (i); Output: >>>> Items in worst order: 8 7 6 5 4 3 2 1 25

Random Case tree = new BST (); for (int i = 1; i <= 8; i++) tree.insert(random()); Output: >>>> Items in random order: X U P O H F B 26

Applications for BST Sorting Sot with binary ayseac search tees trees Input: unsorted array Output: sorted array Algorithm? Running time? 27

Better Search Trees Prevent the degeneration of the BST : A BST can be set up to maintain balance during updating operations (insertions and removals) Types of ST which maintain the optimal performance: splay trees AVL trees 2-4 Trees Red-Black trees B-trees 28