root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain



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:

Converting a Number from Decimal to Binary

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Trees and Huffman Encoding Binary Search Trees

Ordered Lists and Binary Trees

Binary Search Trees CMPSC 122

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Data Structures and Algorithms

Data Structure [Question Bank]

TREE BASIC TERMINOLOGIES

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Binary Search Trees (BST)

How To Create A Tree From A Tree In Runtime (For A Tree)

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Binary Search Trees 3/20/14

From Last Time: Remove (Delete) Operation

Binary Heap Algorithms

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Full and Complete Binary Trees

6 March Array Implementation of Binary Trees

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Algorithms Chapter 12 Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Any two nodes which are connected by an edge in a graph are called adjacent node.

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structure with C

Binary Heaps. CSE 373 Data Structures

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

DATA STRUCTURES USING C

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Chapter 14 The Binary Search Tree

CS711008Z Algorithm Design and Analysis

Exam study sheet for CS2711. List of topics

Analysis of Algorithms I: Optimal Binary Search Trees

Cpt S 223. School of EECS, WSU

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Classification/Decision Trees (II)

GRAPH THEORY LECTURE 4: TREES

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Symbol Tables. Introduction

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures. Level 6 C Module Descriptor

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Persistent Data Structures

Algorithms and Data Structures

Physical Data Organization

Why Use Binary Trees?

Lecture 10: Regression Trees

8.1 Min Degree Spanning Tree

The ADT Binary Search Tree

Parallelization: Binary Tree Traversal

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

Algorithms and Data Structures

Analysis of Binary Search algorithm and Selection Sort algorithm

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Persistent Binary Search Trees

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

28 Closest-Point Problems

Data Structures Fibonacci Heaps, Amortized Analysis

Algorithms and Data Structures Written Exam Proposed SOLUTION

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

OPTIMAL BINARY SEARCH TREES

Introduction to Data Structures and Algorithms

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Data storage Tree indexes

Data Structures and Data Manipulation

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Binary Search Trees. Ric Glassey

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

External Memory Geometric Data Structures

10CS35: Data Structures Using C

CSE 326: Data Structures B-Trees and B+ Trees

Alex. Adam Agnes Allen Arthur

Rotation Operation for Binary Search Trees Idea:

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Heaps & Priority Queues in the C++ STL 2-3 Trees

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Lecture 2 February 12, 2003

Laboratory Module 6 Red-Black Trees

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Lecture 4: Balanced Binary Search Trees

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

R-trees. R-Trees: A Dynamic Index Structure For Spatial Searching. R-Tree. Invariants

Lecture Notes on Binary Search Trees

Transcription:

inary 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. or example: Jargon: root node level: 0 1 2 internal node edge leaf node

inary 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 β and γ α β γ child nodes of α δ ε a descendant of α and γ φ η subtree rooted at γ

Quick Application: xpression Trees 3 A binary tree may be used to represent an algebraic expression: * x ach 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 (( + ) 5) x y

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. α preorder: visit the node, then the left subtree, then the right subtree β γ α β γ δ φ η ε δ ε postorder: visit the left subtree, then the right subtree, and then the node inorder: β φ η δ ε γ α φ visit the left subtree, then the node, then the right subtree β α φ δ η γ ε η

Postorder Traversal etails 5 onsider the postorder traversal from a recursive perspective: postorder: postorder visit the left subtree, postorder visit the right subtree, then visit the node (no recursion) α β γ If we start at the root: POV sub(β) POV sub(γ) visit α δ ε visit β POV sub(δ) POV sub(ε) visit γ φ η visit φ visit η visit δ visit ε

inary 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 ST 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.

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

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

ST eletion 9 eletion is perhaps the most complex operation on a ST, because the algorithm must result in a ST. 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. NULL

ST eletion 10 - Removing an internal node which has two subtrees is more complex Simply removing the node would disconnect the tree. ut what value should replace the one in the targeted node? To preserve the ST property, we must take the smallest value from the right subtree, which would be the closest succcessor of the value being deleted. ortunately, the smallest value will always lie in the left-most node of the subtree.

ST eletion 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 ST. Now we must delete the copied value from the right subtree. That looks straightforward here since the node in question is a leaf. owever - 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 eleterightminimum() function to clean up at this point

ST eletion 12 Suppose we want to delete the value from the ST: After swapping the with the, we must delete We must be careful to not confuse this with the other node containing an. Also, consider deleting the value. 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.

alance in a ST 13 owever, a ST with N nodes does not always provide O(log N) search times. A A well-balanced ST. This will have log(n) search times in the worst case. A What if we inserted the values in the order: A A poorly-balanced ST. This will not have log(n) search times in the worst case.

Search ost in a ST 14 rom 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 ST, the there is always an element whose search cost is at least log N. Unfortunately, it can be much worse. If the ST 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 ST, there is no reason to expect that the result will have nearly-minimum height.

ost of Insertion/eletion in a ST 15 learly, once the parent is found, the remaining cost of inserting a new node in a ST 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. or 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. ut, 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.