Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.

Slides:



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

Comp 245 Data Structures Trees. Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one.
1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 4.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
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.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Search Trees Data Structures Ananda Gunawardena
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
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]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Binary Search Trees Chapter 7 Objectives
Unit 7 Trees, Tree Traversals and Binary Search Trees
AVL TREES.
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
Binary Search Tree (BST)
Section 8.1 Trees.
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 21: Binary Trees.
The DSW Algorithm The building block for tree transformations in this algorithm is the rotation There are two types of rotation, left and right, which.
Paul Tymann, Andrew Watkins,
Binary Tree Traversals
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
CS 367 – Introduction to Data Structures
Trees.
Binary Search Tree.
Presentation transcript:

Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1

Binary tree root parent child nonterminal node (terminal node) leaf Data Structures and Algorithms in Java, Third EditionCh06 – 2

Binary search tree el < el≥ el Data Structures and Algorithms in Java, Third EditionCh06 – 3

Node implementation public class BSTNode<T extends Comparable > { protected T el; protected BSTNode left, right; // constructors } el abbreviated as: Data Structures and Algorithms in Java, Third EditionCh06 – 4

Insertion in BST: algorithm insert(el ) p = root; prev = null; while p is not null // find a place for inserting new node; prev = p; if element in node p < el p = p.right; else p = p.left; if root is null // tree is empty; root becomes a new node with el; else if element in node prev < el new node with el is attached to the right of prev; else new node with el is attached to the left of prev; Data Structures and Algorithms in Java, Third EditionCh06 – 5

20 Insertion in the BST: example null Data Structures and Algorithms in Java, Third EditionCh06 – 6

Insertion in the BST: example (cont’d) Data Structures and Algorithms in Java, Third EditionCh06 – 7

Searching in the BST: algorithm search(el) p = root; while p is not null if element in node p equals el return element in node p; else if element in node p < el p = p.right; else p = p.left; return null; // el was not found; Data Structures and Algorithms in Java, Third EditionCh06 – 8

Searching in the BST: example success failure Data Structures and Algorithms in Java, Third EditionCh06 – 9

Breadth-first traversal: algorithm public void breadthFirst() { BSTNode p = root; Queue > queue = new Queue >(); if (p != null) { queue.enqueue(p); while (!queue.isEmpty()) { p = queue.dequeue(); visit(p); if (p.left != null) queue.enqueue(p.left); if (p.right != null) queue.enqueue(p.right); }

Breadth-first traversal: example 30 iteration queuep init Data Structures and Algorithms in Java, Third EditionCh06 – 11

Depth-first traversals: algorithms Three tasks: V ‑ visiting a node L ‑ traversing the left subtree R ‑ traversing the right subtree The three tasks can be executed in 3! = 6 different orders: left to rightright to left preorder inorder postorder VLR LVR LRV VRL RVL RLV Data Structures and Algorithms in Java, Third EditionCh06 – 12

public void preorder() { preorder(root); } protected void preorder(BSTNode p) { if (p != null) { visit(p); preorder(p.left); preorder(p.right); } Depth-first traversals: implementation Data Structures and Algorithms in Java, Third EditionCh06 – 13

Depth-first traversals: example preorder inorder postorder Data Structures and Algorithms in Java, Third EditionCh06 – 14

Deletion by copying 1. Deleting a leaf 2. Deleting a node with one descendant Data Structures and Algorithms in Java, Third EditionCh06 – 15

Deletion by copying (cont’d) 3. Deleting a node with two descendants A BA Data Structures and Algorithms in Java, Third EditionCh06 – 16

Deletion by copying: example delete 30 delete Data Structures and Algorithms in Java, Third EditionCh06 – 17

Rotations rotateRight(G,P,C) if P is not the root of the tree // i.e., if G is not null 1.grandparent G of child C becomes C ’s parent by replacing P; 2.right subtree of C becomes left subtree of C ’s parent P; 3.node C acquires P as its right child ; Data Structures and Algorithms in Java, Third EditionCh06 – 18

Step-by-step right rotation XY Z G P C XY Z G P C XY Z G P C XY Z G P C 2. right subtree of C becomes left subtree of C’s parent P; 3. node C acquires P as its right child; G C P YZ X = 1. grandparent G of child C becomes C’s parent by replacing P;

Left rotation YZ X G P C Y Z X G P C Data Structures and Algorithms in Java, Third EditionCh06 – 20

Self-adjusting trees  Single rotation: Rotate a child about parent if an element in the child is accessed unless it is the root.  Moving to the root: Repeat the child‑parent rotation until the element being accessed is in the root. Data Structures and Algorithms in Java, Third EditionCh06 – 21

Single rotation technique: example Data Structures and Algorithms in Java, Third EditionCh06 – 22

Expression trees (5 + 6) * 5 preorder inorder postorder +5 * 67 * * * + preorder inorder postorder * * * ** Data Structures and Algorithms in Java, Third EditionCh06 – 23

Processing an expression in postfix notation * Data Structures and Algorithms in Java, Third EditionCh06 – 24

(max) heaps 1.the value of each node is greater than or equal to the values stored in each of its children, 2.the tree is perfectly balanced and the leaves in in the last level are all in the leftmost positions. Data Structures and Algorithms in Java, Third EditionCh06 – 25

A binary tree that violates the first heap condition: A binary tree that violates the second heap condition: Heaps: counterexamples Data Structures and Algorithms in Java, Third EditionCh06 – 26

heap binary search tree el ≤ el el < el≥ el Data Structures and Algorithms in Java, Third EditionCh06 – 27

parentleft child right child …. i2∙i +12∙i +2 Data Structures and Algorithms in Java, Third EditionCh06 – 28

Heap as a priority queue: enqueuing heapEnqueue(el) put el at the end of heap ; while el is not in the root and el > parent( el ) swap el with its parent ; Data Structures and Algorithms in Java, Third EditionCh06 – 29

Series of enqueuings empty

heapDequeue() extract the element from the root ; put the element from the last leaf in its place ; remove the last leaf ; // both subtrees of the root are heaps p = the root ; while p is not a leaf and p < any of its children swap p with the larger child ; Heap as a priority queue: dequeuing Data Structures and Algorithms in Java, Third EditionCh06 – 31

Series of dequeuings Data Structures and Algorithms in Java, Third EditionCh06 – 32

Series of dequeuings (cont’d) Data Structures and Algorithms in Java, Third EditionCh06 – 33

Organizing arrays as heaps FloydAlgorithm(data[]) for i = index of the last nonleaf, down to 0 p = data[i]; while p is not a leaf and p < any of its children swap p with the larger child ; Data Structures and Algorithms in Java, Third EditionCh06 – 34

Organizing arrays as heaps: example last nonleaf Data Structures and Algorithms in Java, Third EditionCh06 – 35