The DSW Algorithm. Data Structures and Algorithms in Java 1

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

Analysis of Algorithms I: Binary Search Trees

Chapter 14 The Binary Search Tree

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

From Last Time: Remove (Delete) Operation

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

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

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

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

6 March Array Implementation of Binary 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

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.

Algorithms Chapter 12 Binary Search Trees

Data Structure [Question Bank]

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

Converting a Number from Decimal to Binary

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

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

Full and Complete Binary Trees

PES Institute of Technology-BSC QUESTION BANK

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

Symbol Tables. Introduction

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

Ordered Lists and Binary Trees

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

International Journal of Software and Web Sciences (IJSWS)

A Comparison of Dictionary Implementations

Binary Heaps. CSE 373 Data Structures

Binary Search Trees CMPSC 122

TREE BASIC TERMINOLOGIES

Binary Search Trees 3/20/14

Algorithms and Data Structures

Binary Search Trees (BST)

Persistent Binary Search Trees

Binary Heap Algorithms

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

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

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

Data Structures Fibonacci Heaps, Amortized Analysis

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

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

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

Big Data and Scripting. Part 4: Memory Hierarchies

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

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

OPTIMAL BINARY SEARCH TREES

Data Structures and Algorithms

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

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

Cpt S 223. School of EECS, WSU

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

Exam study sheet for CS2711. List of topics

Binary Trees and Huffman Encoding Binary Search Trees

Data Structures UNIT III. Model Question Answer

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

Why Use Binary Trees?

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

Introduction to Data Structures and Algorithms

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

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:

Fundamental Algorithms

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

Data Structures and Algorithms(5)

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

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Algorithms and Data Structures

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

CS711008Z Algorithm Design and Analysis

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

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Java Software Structures

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

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

Data Structures and Data Manipulation

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

COMPUTER SCIENCE. Paper 1 (THEORY)

Data Structure with C

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

The ADT Binary Search Tree

Data Structures. Level 6 C Module Descriptor

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Rotation Operation for Binary Search Trees Idea:

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

Social Media Mining. Graph Essentials

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Common Data Structures

Analysis of Algorithms I: Optimal Binary Search Trees

Lecture 4: Balanced Binary Search Trees

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

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Chapter 3: Restricted Structures Page 1

Sample Questions Csci 1112 A. Bellaachia

Chapter 8: Binary Trees

Binary Search Trees. Ric Glassey

Transcription:

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 are symmetrical to one another rotateright (Gr, Par, Ch) if Par is not the root of the tree // i.e., if Gr is not null grandparent Gr of child Ch becomes Ch s parent; right subtree of Ch becomes left subtree of Ch s parent Par; node Ch acquires Par as its right child; Data Structures and Algorithms in Java 1

The DSW Algorithm (continued) Figure 6-37 Right rotation of child Ch about parent Par Data Structures and Algorithms in Java 2

The DSW Algorithm (continued) Figure 6-38 Transforming a binary search tree into a backbone Data Structures and Algorithms in Java 3

The DSW Algorithm (continued) Figure 6-39 Transforming a backbone into a perfectly balanced tree Data Structures and Algorithms in Java 4

AVL Trees An AVL tree is one in which the height of the left and right subtrees of every node differ by at most one Figure 6-40 Examples of AVL trees Data Structures and Algorithms in Java 5

AVL Trees (continued) Figure 6-41 Balancing a tree after insertion of a node in the right subtree of node Q Data Structures and Algorithms in Java 6

AVL Trees (continued) Figure 6-42 Balancing a tree after insertion of a node in the left subtree of node Q Data Structures and Algorithms in Java 7

AVL Trees (continued) Figure 6-43 An example of inserting a new node (b) in an AVL tree (a), which requires one rotation (c) to restore the height balance Data Structures and Algorithms in Java 8

AVL Trees (continued) Figure 6-44 In an AVL tree (a) a new node is inserted (b) requiring no height adjustments Data Structures and Algorithms in Java 9

AVL Trees (continued) Figure 6-45 Rebalancing an AVL tree after deleting a node Data Structures and Algorithms in Java 10

AVL Trees (continued) Figure 6-45 Rebalancing an AVL tree after deleting a node (continued) Data Structures and Algorithms in Java 11

AVL Trees (continued) Figure 6-45 Rebalancing an AVL tree after deleting a node (continued) Data Structures and Algorithms in Java 12

Self-Adjusting Trees The strategy in self-adjusting trees is to restructure trees by moving up the tree with only those elements that are used more often, and creating a priority tree Data Structures and Algorithms in Java 13

Self-Restructuring Trees Single rotation Rotate a child about its parent if an element in a child is accessed, unless it is the root Figure 6-46 Restructuring a tree by using (a) a single rotation or (b) moving to the root when accessing node R Data Structures and Algorithms in Java 14

Self-Restructuring Trees (continued) Moving to the root Repeat the child parent rotation until the element being accessed is in the root Figure 6-46 Restructuring a tree by using (a) a single rotation or (b) moving to the root when accessing node R (continued) Data Structures and Algorithms in Java 15

Self-Restructuring Trees (continued) Figure 6-47 (a e) Moving element T to the root and then (e i) moving element S to the root Data Structures and Algorithms in Java 16

Splaying A modification of the move-to-the-root strategy is called splaying Splaying applies single rotations in pairs in an order depending on the links between the child, parent, and grandparent Semisplaying requires only one rotation for a homogeneous splay and continues splaying with the parent of the accessed node, not with the node itself Data Structures and Algorithms in Java 17

Splaying (continued) Figure 6-48 Examples of splaying Data Structures and Algorithms in Java 18

Splaying (continued) Figure 6-48 Examples of splaying (continued) Data Structures and Algorithms in Java 19

Splaying (continued) Figure 6-49 Restructuring a tree with splaying (a c) after accessing T and (c d) then R Data Structures and Algorithms in Java 20

Splaying (continued) Figure 6-50 (a c) Accessing T and restructuring the tree with semisplaying; (c d) accessing T again Data Structures and Algorithms in Java 21

Heaps A particular kind of binary tree, called a heap, has two properties: The value of each node is greater than or equal to the values stored in each of its children The tree is perfectly balanced, and the leaves in the last level are all in the leftmost positions These two properties define a max heap If greater in the first property is replaced with less, then the definition specifies a min heap Data Structures and Algorithms in Java 22

Heaps (continued) Figure 6-51 Examples of (a) heaps and (b c) nonheaps Data Structures and Algorithms in Java 23

Heaps (continued) Figure 6-52 The array [2 8 6 1 10 15 3 12 11] seen as a tree Data Structures and Algorithms in Java 24

Heaps (continued) Figure 6-53 Different heaps constructed with the same elements Data Structures and Algorithms in Java 25

Heaps as Priority Queues Figure 6-54 Enqueuing an element to a heap Data Structures and Algorithms in Java 26

Heaps as Priority Queues (continued) Figure 6-55 Dequeuing an element from a heap Data Structures and Algorithms in Java 27

Heaps as Priority Queues (continued) Figure 6-56 Implementation of an algorithm to move the root element down a tree Data Structures and Algorithms in Java 28

Organizing Arrays as Heaps Figure 6-57 Organizing an array as a heap with a top-down method Data Structures and Algorithms in Java 29

Organizing Arrays as Heaps Figure 6-57 Organizing an array as a heap with a top-down method (continued) Data Structures and Algorithms in Java 30

Organizing Arrays as Heaps Figure 6-57 Organizing an array as a heap with a top-down method (continued) Data Structures and Algorithms in Java 31

Organizing Arrays as Heaps (continued) Figure 6-58 Transforming the array [2 8 6 1 10 15 3 12 11] into a heap with a bottom-up method Data Structures and Algorithms in Java 32

Organizing Arrays as Heaps (continued) Figure 6-58 Transforming the array [2 8 6 1 10 15 3 12 11] into a heap with a bottom-up method (continued) Data Structures and Algorithms in Java 33

Organizing Arrays as Heaps (continued) Figure 6-58 Transforming the array [2 8 6 1 10 15 3 12 11] into a heap with a bottom-up method (continued) Data Structures and Algorithms in Java 34

Polish Notation and Expression Trees Polish notation is a special notation for propositional logic that eliminates all parentheses from formulas The compiler rejects everything that is not essential to retrieve the proper meaning of formulas rejecting it as syntactic sugar Data Structures and Algorithms in Java 35

Polish Notation and Expression Trees (continued) Figure 6-59 Examples of three expression trees and results of their traversals Data Structures and Algorithms in Java 36

Operations on Expression Trees Figure 6-60 An expression tree Data Structures and Algorithms in Java 37

Operations on Expression Trees (continued) Figure 6-61 Tree transformations for differentiation of multiplication and division Data Structures and Algorithms in Java 38

Case Study: Computing Word Frequencies Figure 6-62 Semisplay tree used for computing word frequencies Data Structures and Algorithms in Java 39

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation Data Structures and Algorithms in Java 40

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 41

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 42

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 43

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 44

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 45

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 46

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 47

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 48

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 49

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 50

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 51

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 52

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 53

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 54

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 55

Case Study: Computing Word Frequencies (continued) Figure 6-63 Implementation of word frequency computation (continued) Data Structures and Algorithms in Java 56

Summary A tree is a data type that consists of nodes and arcs. The root is a node that has no parent; it can have only child nodes. Each node has to be reachable from the root through a unique sequence of arcs, called a path. An orderly tree is where all elements are stored according to some predetermined criterion of ordering. Data Structures and Algorithms in Java 57

Summary (continued) A binary tree is a tree whose nodes have two children (possibly empty), and each child is designated as either a left child or a right child. A decision tree is a binary tree in which all nodes have either zero or two nonempty children. Tree traversal is the process of visiting each node in the tree exactly one time. Threads are references to the predecessor and successor of the node according to an inorder traversal. Data Structures and Algorithms in Java 58

Summary (continued) An AVL tree is one in which the height of the left and right subtrees of every node differ by at most one. A modification of the move-to-the-root strategy is called splaying. Polish notation is a special notation for propositional logic that eliminates all parentheses from formulas. Data Structures and Algorithms in Java 59