Lecture 8 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson)

Similar documents
Ordered Lists and Binary Trees

Binary Search Trees (BST)

Data Structures and Algorithms

Chapter 14 The Binary Search Tree

Binary Search Trees CMPSC 122

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

TREE BASIC TERMINOLOGIES

From Last Time: Remove (Delete) Operation

Analysis of Algorithms I: Binary Search Trees

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

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

Binary Heap Algorithms

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

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

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

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:

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

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

Converting a Number from Decimal to Binary

Binary Trees and Huffman Encoding Binary Search Trees

DATA STRUCTURES USING C

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

Exam study sheet for CS2711. List of topics

GRAPH THEORY LECTURE 4: TREES

Binary Heaps. CSE 373 Data Structures

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

Binary Search Trees 3/20/14

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

Big Data and Scripting. Part 4: Memory Hierarchies

Sample Questions Csci 1112 A. Bellaachia

Lecture Notes on Binary Search Trees

Data Structure with C

The ADT Binary Search Tree

Algorithms Chapter 12 Binary Search Trees

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

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. Data in each node. Larger than the data in its left child Smaller than the data in its right child

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

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

Data Structure [Question Bank]

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

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

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

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

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

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Full and Complete Binary Trees

Persistent Binary Search Trees

Lecture Notes on Binary Search Trees

Data Structures. Level 6 C Module Descriptor

Chapter 8: Binary Trees

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

10CS35: Data Structures Using C

Symbol Tables. Introduction

CompSci-61B, Data Structures Final Exam

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

PES Institute of Technology-BSC QUESTION BANK

Algorithms and Data Structures

Binary Search Trees. Ric Glassey

Why Use Binary Trees?

Algorithms and Data Structures

Object Oriented Software Design

Graduate Assessment Test (Sample)

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

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

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

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

M-way Trees and B-Trees

Parallelization: Binary Tree Traversal

Data Structures and Algorithms Written Examination

6 March Array Implementation of Binary Trees

Java Software Structures

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Alex. Adam Agnes Allen Arthur

Data Structures Using C++

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

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

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Cpt S 223. School of EECS, WSU

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

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Section IV.1: Recursive Algorithms and Recursion Trees

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Analysis of Algorithms I: Optimal Binary Search Trees

Introduction to Data Structures and Algorithms

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

Lecture 6: Trees, Binary Trees and Binary Search Trees (BST)

International Journal of Software and Web Sciences (IJSWS)

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

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

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

A Comparison of Dictionary Implementations

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Transcription:

Lecture 8 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson)

Trees A tree is a hierarchical data structure Each node can have several children but only has one parent The root has no parents; there is only one root Example: directory hierarchy

Trees (as graphs) Also A tree is + undirected, unweighted, simple (not mulk) acyclic graph

Tree Terminology

Tree Terminology The depth of a node is the distance from the root The height of a tree is the number of levels in the tree The size of a tree is the number of nodes in it

Binary Trees A binary tree is a tree with at most two children for each node Java: class Tree<A> {! class TreeNode {!!!A contents;!!!treenode left, right; }!! TreeNode root;}! Can be null if child is missing Can be null if tree is empty Haskell: data Tree a = Node a (Tree a) (Tree a) Empty!

Height of (Binary) Trees Height: Empty trees have height - 1. Otherwise: Number of steps from root to deepest leaf. Haskell: height :: Tree a -> Integer! height Empty = -1! height (Node _ l r) = 1 + max (height l) (height r)!

Height of (Binary) Trees Height: Empty trees have height - 1. Otherwise: Number of steps from root to deepest leaf. Java: int height(treenode n) {! if (n == null) return -1;! else! return 1 + Math.max(height(n.left),height(n.right));}!

QuesKon What is the smallest and largest number of nodes for a binary tree of height n (computed with the funckon define before)? 1. n+1 and 2^n- 1 2. n and 2^n 1 3. n+1 and 2^(n+1)- 1 4. none of the above govote.at Code 882001

Balanced Binary Trees A tree can be balanced or unbalanced

Balanced Binary Trees A tree can be balanced or unbalanced If a tree of size n is balanced, its height is O(log n) unbalanced, its height could be O(n) Many tree algorithms have complexity O(height of tree), so are efficient on balanced trees and less so on unbalanced trees Normally: balanced trees - good J unbalanced trees bad L

Traversal of (Binary) Trees Traversing a tree means visikng all its nodes in some order A traversal is a parkcular order that we visit the nodes in Four common traversals: + preorder + inorder + postorder + level- order For each traversal, you can define an iterator that traverses the nodes in that order

Traversal of (Binary) Trees Traversing a tree means visikng all its nodes in some order A traversal is a parkcular order that we visit the nodes in Four common traversals: + preorder + inorder + postorder + level- order For each traversal, you can define an iterator that traverses the nodes in that order

Preorder Traversal Visit root node, then leb child, then right

Postorder Traversal Visit leb child, then right, then root node

Inorder Traversal Visit leb child, then root node, then right child

Level- Order Traversal Visit nodes leb to right, top to bocom

QuesKon Which of the tree traversal methods is equivalent to DFS? 1. Preorder 2. Postorder 3. Inorder 4. Traversal on levels govote.at Code 836306

ImplemenKng Tree Traversals Preorder: Java: void preorder(node<e> node) { if (node == null) return; System.out.println(node.value);! preorder(node.left); preorder(node.value);}!!! Haskell: preorder Empty = []! Implement the others on your own Inefficient list append preorder (Node info l r) = info : (preorder l ++ preorder r)!

QuesKon Which of the following asserkons about binary trees with disknct elements holds? 1. We can recreate a tree from its preorder and postorder 2. We can recreate a tree from its inorder and preorder 3. We can recreate a tree from only one of the traversals 4. We can t recreate a tree from any combinakon of two traversals govote.at Code 82769

Binary Search Trees (BST) In a binary search tree (BST), every node is greater than all its leb descendants, and less than all its right descendants (recall that this is an invariant)

Binary Search Trees (BST) Which of the following trees is a BST?

OperaKons on BST - Searching for a value - InserKng a new value - DeleKng a value

Searching in a BST Finding an element in a BST is easy, because by looking at the root you can tell which subtree the element is in

Searching in a BST To search for target in a BST: If the target matches the root node's data, we've found it If the target is less than the root node's data, recursively search the leb subtree If the target is greater than the root node's data, recursively search the right subtree If the tree is empty, fail A BST can be used to implement a set, or a map from keys to values

InserKng in a BST To insert a value into a BST: Start by searching for the value But when you get to null (the empty tree), make a node for the value and place it there

InserKng in a BST To insert a value into a BST: Start by searching for the value But when you get to null (the empty tree), make a node for the value and place it there

QuesKon What is the order in which we should insert the values from a sorted array a[0] a[n] into a BST in order to get a tree of minimal height? 1. a[0], a[1] a[n] 2. a[n/2], a[n- 1],,a[N/2+1], a[0],a[n/2-1] 3. a[n/2], a[n/4], a[3n/4],a[n/8], 4. None of the above govote.at Code 954797

DeleKng from a BST To delete a node with one child: DeleKng is, which has one child, in we connect in to is's parent jack

DeleKng from a BST To delete a value from a BST: Find the node and its parent If it has no children, just remove it from the tree (by disconneckng it from its parent) If it has one child, replace the node with its child (by making the node's parent point at the child) If it has two children...?

DeleKng from a BST Replace the deleted value with the biggest value from its leb subtree (or the smallest from the right subtree)

QuesKon Which of the following statements about the node from the leb subtree that we replace the deleted node with is generally true? 1. It can t have any children 2. It could have two children 3. It could have a leb child 4. It could have a right child govote.at Code?

DeleKng from a BST Find the biggest value in the leb subtree and put that value in the deleted node Using the biggest value preserves the invariant Biggest node = rightmost node Finally, delete the biggest value from the leb subtree This node can't have two children (no right child), so delekng it is much easier

DeleKng from a BST DeleKng rat, we replace it with priest; now we have to delete priest which has a child, morn

Complexity of BST OperaKons All our operakons are O(height of tree) This means O(log n) if the tree is balanced, but O(n) if it's unbalanced. Balanced BSTs add an extra invariant that makes sure the tree is balanced, then all operakons are O(log n) Coming up soon!

Summary of BSTs Can be used to implement sets and maps lookup: can easily find a value in the tree insert: perform a lookup, then put the new value at the place where the lookup would terminate delete: find the value, then several cases depending on how many children the node has Complexity: all operakons O(height of tree) that is, O(log n) if tree is balanced, O(n) if unbalanced inserkng random data tends to give balanced trees, sequenkal data gives unbalanced ones

More Trees in Computer Science Parse trees Natural Language Processing Proof tree - Logic Abstract Syntax Tress - Compilers

To Do Read from the book: + 4.1 4.3 Implement: + trees + the algorithms from today in your favourite programming language Dugga next week: hcp://www.cse.chalmers.se/edu/year/2014/course/dat037_datastrukturer/ examinakon.html#dugga Coming up: + advanced data structures - minimum spanning tree (trees and graphs) - balanced trees - more on sorkng

QuesKons about dugga