Chapter 6. Binary Trees. Data Structures and Algorithms in Java

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

Binary Search Trees (BST)

Converting a Number from Decimal to Binary

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

Binary Search Trees CMPSC 122

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

DATA STRUCTURES USING C

Analysis of Algorithms I: Binary Search Trees

Ordered Lists and Binary Trees

Data Structure [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

Full and Complete Binary Trees

Data Structure with C

Data Structures and Algorithms

Algorithms and Data Structures

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.

From Last Time: Remove (Delete) Operation

TREE BASIC TERMINOLOGIES

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

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

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

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

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

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

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

Algorithms Chapter 12 Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees

Chapter 14 The Binary Search Tree

Data Structures and Algorithms(5)

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

Binary Search Trees 3/20/14

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

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

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

PES Institute of Technology-BSC QUESTION BANK

Data Structures Fibonacci Heaps, Amortized Analysis

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

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

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

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

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

Data Structures. Level 6 C Module Descriptor

Lecture Notes on Binary Search Trees

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

Binary Heaps. CSE 373 Data Structures

The ADT Binary Search Tree

6 March Array Implementation of Binary Trees

Rotation Operation for Binary Search Trees Idea:

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

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

Persistent Binary Search Trees

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

Sample Questions Csci 1112 A. Bellaachia

Exam study sheet for CS2711. List of topics

10CS35: Data Structures Using C

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

Cpt S 223. School of EECS, WSU

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

Introduction to Data Structures and Algorithms

A Comparison of Dictionary Implementations

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

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

Structural Design Patterns Used in Data Structures Implementation

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

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

OPTIMAL BINARY SEARCH TREES

Symbol Tables. Introduction

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

Binary Heap Algorithms

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

Data Structures. September 28

Efficient Data Structures for Decision Diagrams

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:

Social Media Mining. Graph Essentials

M-way Trees and B-Trees

Chapter 8: Binary Trees

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

Unordered Linked Lists

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Parallelization: Binary Tree Traversal

Measuring the Performance of an Agent

Big Data and Scripting. Part 4: Memory Hierarchies

GRAPH THEORY LECTURE 4: TREES

Lecture Notes on Binary Search Trees

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

International Journal of Software and Web Sciences (IJSWS)

Optimal Binary Search Trees Meet Object Oriented Programming

Data Structures and Data Manipulation

Java Software Structures

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

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

Analysis of Algorithms I: 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

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

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

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

Transcription:

Chapter 6 Binary Trees Data Structures and Algorithms in Java

Objectives Discuss the following topics: Trees, Binary Trees, and Binary Search Trees Implementing Binary Trees Searching a Binary Search Tree Tree Traversal Insertion Deletion Data Structures and Algorithms in Java 2

Objectives (continued) Discuss the following topics: Balancing a Tree Self-Adjusting Trees Heaps Polish Notation and Expression Trees Case Study: Computing Word Frequencies Data Structures and Algorithms in Java 3

Trees, Binary Trees, and Binary Search Trees A tree is a data type that consists of nodes and arcs These trees are depicted upside down with the root at the top and the leaves (terminal nodes) at the bottom The root is a node that has no parent; it can have only child nodes Leaves have no children (their children are null) Data Structures and Algorithms in Java 4

Trees, Binary Trees, and Binary Search Trees (continued) Each node has to be reachable from the root through a unique sequence of arcs, called a path The number of arcs in a path is called the length of the path The level of a node is the length of the path from the root to the node plus 1, which is the number of nodes in the path The height of a nonempty tree is the maximum level of a node in the tree Data Structures and Algorithms in Java 5

Trees, Binary Trees, and Binary Search Trees (continued) Figure 6-1 Examples of trees Data Structures and Algorithms in Java 6

Trees, Binary Trees, and Binary Search Trees (continued) Figure 6-2 Hierarchical structure of a university shown as a tree Data Structures and Algorithms in Java 7

Trees, Binary Trees, and Binary Search Trees (continued) An orderly tree is where all elements are stored according to some predetermined criterion of ordering Figure 6-3 Transforming (a) a linked list into (b) a tree Data Structures and Algorithms in Java 8

Trees, Binary Trees, and Binary Search Trees (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 Figure 6-4 Examples of binary trees Data Structures and Algorithms in Java 9

Trees, Binary Trees, and Binary Search Trees (continued) In a complete binary tree, all nonterminal nodes have both their children, and all leaves are at the same level A decision tree is a binary tree in which all nodes have either zero or two nonempty children Data Structures and Algorithms in Java 10

Trees, Binary Trees, and Binary Search Trees (continued) Figure 6-5 Adding a leaf to tree (a), preserving the relation of the number of leaves to the number of nonterminal nodes (b) Data Structures and Algorithms in Java 11

Trees, Binary Trees, and Binary Search Trees (continued) Figure 6-6 Examples of binary search trees Data Structures and Algorithms in Java 12

Implementing Binary Trees Binary trees can be implemented in at least two ways: As arrays As linked structures To implement a tree as an array, a node is declared as an object with an information field and two reference fields Data Structures and Algorithms in Java 13

Implementing Binary Trees (continued) Figure 6-7 Array representation of the tree in Figure 6.6c Data Structures and Algorithms in Java 14

Implementing Binary Trees (continued) Figure 6-8 Implementation of a generic binary search tree Data Structures and Algorithms in Java 15

Implementing Binary Trees (continued) Figure 6-8 Implementation of a generic binary search tree (continued) Data Structures and Algorithms in Java 16

Implementing Binary Trees (continued) Figure 6-8 Implementation of a generic binary search tree (continued) Data Structures and Algorithms in Java 17

Searching a Binary Search Tree Figure 6-9 A function for searching a binary search tree Data Structures and Algorithms in Java 18

Searching a Binary Search Tree (continued) The internal path length (IPL) is the sum of all path lengths of all nodes It is calculated by summing Σ(i 1)l i over all levels i, where l i is the number of nodes on level I A depth of a node in the tree is determined by the path length An average depth, called an average path length, is given by the formula IPL/n, which depends on the shape of the tree Data Structures and Algorithms in Java 19

Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time Breadth-first traversal is visiting each node starting from the lowest (or highest) level and moving down (or up) level by level, visiting nodes on each level from left to right (or from right to left) Data Structures and Algorithms in Java 20

Breadth-First Traversal Figure 6-10 Top-down, left-to-right, breadth-first traversal implementation Data Structures and Algorithms in Java 21

Depth-First Traversal Depth-first traversal proceeds as far as possible to the left (or right), then backs up until the first crossroad, goes one step to the right (or left), and again as far as possible to the left (or right) V Visiting a node L Traversing the left subtree R Traversing the right subtree Data Structures and Algorithms in Java 22

Depth-First Traversal (continued) Figure 6-11 Depth-first traversal implementation Data Structures and Algorithms in Java 23

Depth-First Traversal (continued) Figure 6-12 Inorder tree traversal Data Structures and Algorithms in Java 24

Depth-First Traversal (continued) Figure 6-13 Details of several of the first steps of inorder traversal Data Structures and Algorithms in Java 25

Depth-First Traversal (continued) Figure 6-14 Changes in the run-time stack during inorder traversal Data Structures and Algorithms in Java 26

Depth-First Traversal (continued) Figure 6-15 A nonrecursive implementation of preorder tree traversal Data Structures and Algorithms in Java 27

Stackless Depth-First Traversal Threads are references to the predecessor and successor of the node according to an inorder traversal Trees whose nodes use threads are called threaded trees Data Structures and Algorithms in Java 28

Stackless Depth-First Traversal (continued) Figure 6-18 (a) A threaded tree and (b) an inorder traversal s path in a threaded tree with right successors only Data Structures and Algorithms in Java 29

Stackless Depth-First Traversal (continued) Figure 6-19 Implementation of the threaded tree and the inorder traversal of a threaded tree Data Structures and Algorithms in Java 30

Stackless Depth-First Traversal (continued) Figure 6-19 Implementation of the threaded tree and the inorder traversal of a threaded tree (continued) Data Structures and Algorithms in Java 31

Stackless Depth-First Traversal (continued) Figure 6-19 Implementation of the threaded tree and the inorder traversal of a threaded tree (continued) Data Structures and Algorithms in Java 32

Traversal Through Tree Transformation Figure 6-20 Implementation of the Morris algorithm for inorder traversal Data Structures and Algorithms in Java 33

Traversal Through Tree Transformation (continued) Figure 6-21 Tree traversal with the Morris method Data Structures and Algorithms in Java 34

Traversal Through Tree Transformation (continued) Figure 6-21 Tree traversal with the Morris method (continued) Data Structures and Algorithms in Java 35

Traversal Through Tree Transformation (continued) Figure 6-21 Tree traversal with the Morris method (continued) Data Structures and Algorithms in Java 36

Insertion Figure 6-22 Inserting nodes into binary search trees Data Structures and Algorithms in Java 37

Insertion (continued) Figure 6-23 Implementation of the insertion algorithm Data Structures and Algorithms in Java 38

Insertion (continued) Figure 6-24 Implementation of the algorithm to insert nodes into a threaded tree Data Structures and Algorithms in Java 39

Insertion (continued) Figure 6-24 Implementation of the algorithm to insert nodes into a threaded tree (continued) Data Structures and Algorithms in Java 40

Insertion (continued) Figure 6-25 Inserting nodes into a threaded tree Data Structures and Algorithms in Java 41

Deletion There are three cases of deleting a node from the binary search tree: The node is a leaf; it has no children The node has one child The node has two children Data Structures and Algorithms in Java 42

Deletion (continued) Figure 6-26 Deleting a leaf Figure 6-27 Deleting a node with one child Data Structures and Algorithms in Java 43

Deletion by Merging Making one tree out of the two subtrees of the node and then attaching it to the node s parent is called deleting by merging Figure 6-28 Summary of deleting by merging Data Structures and Algorithms in Java 44

Deletion by Merging (continued) Figure 6-29 Implementation of algorithm for deleting by merging Data Structures and Algorithms in Java 45

Deletion by Merging (continued) Figure 6-29 Implementation of algorithm for deleting by merging (continued) Data Structures and Algorithms in Java 46

Deletion by Merging (continued) Figure 6-30 Details of deleting by merging Data Structures and Algorithms in Java 47

Deletion by Merging (continued) Figure 6-31 The height of a tree can be (a) extended or (b) reduced after deleting by merging Data Structures and Algorithms in Java 48

Deletion by Merging (continued) Figure 6-31 The height of a tree can be (a) extended or (b) reduced after deleting by merging (continued) Data Structures and Algorithms in Java 49

Deletion by Copying If the node has two children, the problem can be reduced to: The node is a leaf The node has only one nonempty child Solution: replace the key being deleted with its immediate predecessor (or successor) A key s predecessor is the key in the rightmost node in the left subtree Data Structures and Algorithms in Java 50

Deletion by Copying (continued) Figure 6-32 Implementation of an algorithm for deleting by copying Data Structures and Algorithms in Java 51

Deletion by Copying Figure 6-32 Implementation of an algorithm for deleting by copying (continued) Data Structures and Algorithms in Java 52

Deletion by Copying Figure 6-32 Implementation of an algorithm for deleting by copying (continued) Data Structures and Algorithms in Java 53

Deletion by Copying (continued) Figure 6-33 Deleting by copying Data Structures and Algorithms in Java 54

Balancing a Tree A binary tree is height-balanced or balanced if the difference in height of both subtrees of any node in the tree is either zero or one A tree is considered perfectly balanced if it is balanced and all leaves are to be found on one level or two levels Data Structures and Algorithms in Java 55

Balancing a Tree (continued) Figure 6-34 Different binary search trees with the same information Data Structures and Algorithms in Java 56

Balancing a Tree (continued) Figure 6-35 Maximum number of nodes in binary trees of different heights Data Structures and Algorithms in Java 57

Balancing a Tree (continued) Figure 6-36 Creating a binary search tree from an ordered array Data Structures and Algorithms in Java 58