Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Similar documents
A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key and satisfies the conditions:

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees, Binary Search Trees

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

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

Binary Trees

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

TREES. Trees - Introduction

Programming II (CS300)

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Data Structures and Algorithms for Engineers

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

COMP Analysis of Algorithms & Data Structures

INF2220: algorithms and data structures Series 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Friday Four Square! 4:15PM, Outside Gates

COMP Analysis of Algorithms & Data Structures

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

March 20/2003 Jayakanth Srinivasan,

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

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

Chapter 20: Binary Trees

Binary Trees and Binary Search Trees

Binary Search Trees Treesort

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

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

CS301 - Data Structures Glossary By

Trees. Truong Tuan Anh CSE-HCMUT

Search Trees. Undirected graph Directed graph Tree Binary search tree

Binary Search Trees. See Section 11.1 of the text.

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

Cpt S 122 Data Structures. Data Structures Trees

CS102 Binary Search Trees

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Algorithms. AVL Tree

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

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

Analysis of Algorithms

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

CSCI2100B Data Structures Trees

9. Heap : Priority Queue

Algorithms. Deleting from Red-Black Trees B-Trees

Trees. CSE 373 Data Structures

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Associate Professor Dr. Raed Ibraheem Hamed

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CS-301 Data Structure. Tariq Hanif

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Balanced Binary Search Trees. Victor Gao

Binary Trees. Height 1

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

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

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

Successor/Predecessor Rules in Binary Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

8. Binary Search Tree

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

Data Structures and Algorithms

Binary search trees (BST) Binary search trees (BST)

Trees, Binary Trees, and Binary Search Trees

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Binary Trees. Examples:

Chapter 5. Binary Trees

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

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

Why Do We Need Trees?

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Self-Balancing Search Trees. Chapter 11

CE 221 Data Structures and Algorithms

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

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

Lecture 6: Analysis of Algorithms (CS )

IX. Binary Trees (Chapter 10)

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

CISC 235: Topic 4. Balanced Binary Search Trees

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

Search Trees - 1 Venkatanatha Sarma Y

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

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

SFU CMPT Lecture: Week 9

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

Lecture Notes for Advanced Algorithms

CS350: Data Structures Binary Search Trees

Transcription:

Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each other and from the root. For example: Jargon: root node level: 0 1 2 internal node edge leaf node

Binary Tree Node Relationships 2 A binary tree node may have 0, 1 or 2 child nodes. A path is a sequence of adjacent (via the edges) nodes in the tree. A subtree of a binary tree is either empty, or consists of a node in that tree and all of its descendent nodes. parent node of b and g a b g child nodes of a d e a descendant of a and g f h subtree rooted at g

Quick Application: Expression Trees 3 A binary tree may be used to represent an algebraic expression: * x Each subtree represents a part of the entire expression + 5 If we visit the nodes of the binary tree in the correct order, we will construct the algebraic expression: x y x ( x y) 5

Traversals 4 A traversal is an algorithm for visiting some or all of the nodes of a binary tree in some defined order. A traversal that visits every node in a binary tree is called an enumeration. a preorder: visit the node, then the left subtree, then the right subtree b g a b g d f h e d e postorder: visit the left subtree, then the right subtree, and then the node b f h d e g a inorder: visit the left subtree, then the node, then the right subtree f h b a f d h g e

Postorder Traversal Details 5 Consider the postorder traversal from a recursive perspective: postorder: postorder visit the left subtree, postorder visit the right subtree, then visit the node (no recursion) a b g If we start at the root: POV sub(b) POV sub(g) visit a d e visit b POV sub(d) POV sub(e) visit g f h visit f visit h visit d visit e

Binary Search Trees 6 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will) and the general binary tree does not have good performance. Suppose that we wish to store data elements that contain a number of fields, and that one of those fields is distinguished as the key upon which searches will be performed. A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: 1. All keys in the left subtree (if there is one) are less than the key in the root node. 2. All keys in the right subtree (if there is one) are greater than (or equal to)* the key in the root node. 3. The left and right subtrees of the root are binary search trees. * In many uses, duplicate values are not allowed.

BST Insertion 7 Here, the key values are characters (and only key values are shown). Inserting the following key values in the given order yields the given BST: 50 75 80 60 30 55 70 40 50 In a BST, insertion is always at the leaf level. Traverse the BST, comparing the new value to existing ones, until you find the right spot, then add a new leaf node holding that value. 30 75 40 60 80 55 70 What is the resulting tree if the (same) key values are inserted in the order: 30 40 50 55 60 70 75 80

Searching in a BST 8 Because of the key ordering imposed by a BST, searching resembles the binary search algorithm on a sorted array, which is O(log N) for an array of N elements. A BST offers the advantage of purely dynamic storage, no wasted array cells and no shifting of the array tail on insertion and deletion. D B G A C E H Trace searching for the key value E.

BST Deletion 9 Deletion is perhaps the most complex operation on a BST, because the algorithm must result in a BST. The question is: what value should replace the one deleted? As with the general tree, we have cases: - Removing a leaf node is trivial, just set the relevant child pointer in the parent node to NULL. - Removing an internal node which has only one subtree is also trivial, just set the relevant child pointer in the parent node to target the root of the subtree. 50 30 75 NULL 40 60 80 55 70

BST Deletion 10 - Removing an internal node which has two subtrees is more complex Simply removing the node would disconnect the tree. But what value should replace the one in the targeted node? To preserve the BST property, we must take the smallest value from the right subtree, which would be the closest succcessor of the value being deleted OR the largest value from the left subtree, which would be the closest predecessor of the value being deleted 50 30 75 40 60 80 55 70 Fortunately, these extreme values are easy to find

BST Deletion 11 So, we first find the left-most node of the right subtree, and then swap data values between it and the targeted node. Note that at this point we don t necessarily have a BST. 55 30 75 40 60 80 Now we must delete the copied value from the right subtree. 55 70 That looks straightforward here since the node in question is a leaf. However - the node will NOT be a leaf in all cases - the occurrence of duplicate values is a complicating factor - so we might want to have a DeleteRightMinimum() function to clean up at this point

BST Deletion 12 Suppose we want to delete the value 55 from the BST: After replacing 55 with 60, we must delete 55 30 75 40 60 80 70 Also, consider deleting the value 75. In this case, the right subtree is just a leaf node, whose parent is the node originally targeted for deletion. Moral: be careful to consider ALL cases when designing.

Balance in a BST However, a BST with N nodes does not always provide O(log N) search times. 13 D G B F A A C E G F A well-balanced BST. This will have log(n) search times in the worst case. B E What if we inserted the values in the order: A B C D E F G A poorly-balanced BST. This will not have log(n) search times in the worst, or even the average, case. C D

Search Cost in a BST 14 From an earlier theorem on binary trees, we know that a binary tree that contains L nodes must contain at least 1 + log L levels. If the tree is full, we can improve the result to imply that a full binary tree that contains N nodes must contain at least log N levels. So, for any BST, the there is always an element whose search cost is at least log N. Unfortunately, it can be much worse. If the BST is a "stalk" then the search cost for the last element would be N. It all comes down to one simple issue: how close is the tree to having minimum height? Unfortunately, if we perform lots of random insertions and deletions in a BST, there is no reason to expect that the result will have nearly-minimum height.

Traversal Cost in a BST 15 Let s suppose that we have a binary tree such that every level of the tree is completely full. For example: Obviously, we could extend any given binary tree to have this property, and a tree ike this lets us get a bound on the worst case for a traversal. Each of the three standard traversals travels to every node in the tree, and the cost of the traversal really isn t influenced by the order in which the current node is processed. For the sake of argument, let s say that the cost of whatever we do with a node is 1. And, let s let W(N) represent the cost of traversing a binary tree with N nodes.

Traversal Cost in a BST Whatever node the traversal is currently at, we must do three things: 16 Process node Process left subtree Process right subtree Therefore, we can write a recursive definition for the cost of the traversal: N if N 0,1 W( N) N 1 1 2C if N 1 2 We will see later how to express this without using recursion

Cost of Insertion/Deletion in a BST 17 Clearly, once the parent is found, the remaining cost of inserting a new node in a BST is constant, simply allocating a new node object and setting a pointer in the parent node. So, insertion cost is essentially the same as search cost. For deletion, the argument is slightly more complex. Suppose the parent of the targeted node has been found. If the parent has only one subtree, then the remaining cost is resetting a pointer in the parent and deallocating the node; that's constant. But, if the parent has two subtrees, then an additional search must be carried out to find the minimum value in the right subtree, and then an element copy must be performed, and then that node must be removed from the right subtree (which is again a constant cost). In either case, we have no more than the cost of a worst-case search to the leaf level, plus some constant manipulations. So, deletion cost is also essentially the same as search cost.