Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal, Amit Kumar
Advertisements

Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
They’re not just binary anymore!
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 15 Trees and Tree Traversals Instructor: Prof. Zhigang Zhu Department of Computer Science City College of New.
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Binary Search Trees1 Part-F1 Binary Search Trees   
BST Data Structure A BST node contains: A BST contains
1 Trees III: Binary Search Trees. 2 A forest full of trees The generic toolkit of functions we have seen thus far can be applied to many types of data.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Binary Trees Chapter 6.
CSC 213 – Large Scale Programming. Today’s Goals  Review a new search tree algorithm is needed  What real-world problems occur with old tree?  Why.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
Spring 2006 Copyright (c) All rights reserved Leonard Wesley0 B-Trees CMPE126 Data Structures.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
File Organization and Processing Week Tree Tree.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CSC212 Data Structure - Section AB
BST Trees
Binary Search Tree Chapter 10.
Complete Binary Trees Chapter 10 introduces trees.
Hashing Exercises.
Complete Binary Trees Chapter 9 introduces trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
B-Trees.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Complete Binary Trees Chapter 9 introduces trees.
Presentation transcript:

Trees Main and Savitch Chapter 10

Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. But it is the connections between the nodes which characterize a binary tree.

A Binary Tree of States In this example, the data contained at each node is one of the 50 states.

A Binary Tree of States Each tree has a special node called its root, usually drawn at the top.

A Binary Tree of States Each tree has a special node called its root, usually drawn at the top. The example tree has Washington as its root. The example tree has Washington as its root.

A Binary Tree of States Each node is permitted to have two links to other nodes, called the left child and the right child.

A Binary Tree of States Children are usually drawn below a node. The right child of Washington is Colorado. The right child of Washington is Colorado. The left child of Washington is Arkansas. The left child of Washington is Arkansas.

A Binary Tree of States Some nodes have only one child. Arkansas has a left child, but no right child. Arkansas has a left child, but no right child.

A Quiz Some nodes have only one child. Which node has only a right child? Which node has only a right child?

A Quiz Some nodes have only one child. Florida has only a right child. Florida has only a right child.

A Binary Tree of States A node with no children is called a leaf.

A Binary Tree of States Each node is called the parent of its children. Washington is the parent of Arkansas and Colorado. Washington is the parent of Arkansas and Colorado.

A Binary Tree of States Two rules about parents: ¶The root has no parent. ·Every other node has exactly one parent.

A Binary Tree of States Two nodes with the same parent are called siblings. Arkansas and Colorado are siblings. Arkansas and Colorado are siblings.

Complete Binary Trees A complete binary tree is a special kind of binary tree which will be useful to us.

Complete Binary Trees A complete binary tree is a special kind of binary tree which will be useful to us. When a complete binary tree is built, its first node must be the root. When a complete binary tree is built, its first node must be the root.

Complete Binary Trees The second node of a complete binary tree is always the left child of the root...

Complete Binary Trees The second node of a complete binary tree is always the left child of the root and the third node is always the right child of the root.

Complete Binary Trees The next nodes must always fill the next level from left to right.

Complete Binary Trees The next nodes must always fill the next level from left to right.

Complete Binary Trees The next nodes must always fill the next level from left to right.

Complete Binary Trees The next nodes must always fill the next level from left to right.

Complete Binary Trees The next nodes must always fill the next level from left to right.

Complete Binary Trees The next nodes must always fill the next level from left to right.

Is This Complete?

Yes! It is called the empty tree, and it has no nodes, not even a root.

Implementing a Complete Binary Tree We will store the data from the nodes in a partially-filled array. An array of data We don't care what's in this part of the array. An integer to keep track of how many nodes are in the tree 3

Implementing a Complete Binary Tree We will store the date from the nodes in a partially-filled array. An array of data We don't care what's in this part of the array. An integer to keep track of how many nodes are in the tree 3 Read Section 10.2 to see details of how the entries are stored. Read Section 10.2 to see details of how the entries are stored.

Depth of Binary Tree This example, depth = 3

Depth of Binary Tree This example, depth = 2

Depth of Binary Tree This example, depth = 0

Depth of Complete Binary Tree Given a complete binary tree of N nodes, what is the depth? D = 0 N = 1 D = 1 N = 3 D = 2 N = 7 D = 1 N = 2 D = 2 N = 4

Depth of Complete Binary Tree Given a complete binary tree of N nodes, what is the depth? D = 0 N = 1 D = 1 N = 3 D = 2 N = 7 D = 1 N = 2 D = 2 N = 4 D = floor(log N) = O(log N)

Depth of Binary Tree Given a binary tree of N nodes, what is the maximum possible depth? D = 0 N = 1 D = 2 N = 3 D = 4 N = 5 D = O(N)

Binary trees contain nodes. Each node may have a left child and a right child. If you start from any node and move upward, you will eventually reach the root. Every node except the root has one parent. The root has no parent. Complete binary trees require the nodes to fill in each level from left-to-right before starting the next level. Summary

One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags and sets. This presentation illustrates how another data type called a dictionary is implemented with binary search trees. Binary Search Trees

The Dictionary Data Type A dictionary is a collection of items, similar to a bag. But unlike a bag, each item has a string attached to it, called the item's key.

The Dictionary Data Type A dictionary is a collection of items, similar to a bag. But unlike a bag, each item has a string attached to it, called the item's key. Example: The items I am storing are records containing data about a state.

The Dictionary Data Type A dictionary is a collection of items, similar to a bag. But unlike a bag, each item has a string attached to it, called the item's key. Example: The key for each record is the name of the state. Washington

The Dictionary Data Type The insertion procedure for a dictionary has two parameters. void Dictionary::insert(The key for the new item, The new item); Washington

The Dictionary Data Type When you want to retrieve an item, you specify the key... Item Dictionary::retrieve("Washington");

The Dictionary Data Type p When you want to retrieve an item, you specify the key and the retrieval procedure returns the item.

The Dictionary Data Type We'll look at how a binary tree can be used as the internal storage mechanism for the dictionary.

Arizona Arkansas Colorado A Binary Search Tree of States The data in the dictionary will be stored in a binary tree, with each node containing an item and a key. Washington Oklahoma Florida Mass. New Hampshire West Virginia

Colorado Arizona Arkansas A Binary Search Tree of States Washington Oklahoma Colorado Florida Mass. New Hampshire West Virginia Storage rules: 1.Every key to the left of a node is alphabetically before the key of the node.

Arizona Colorado Arkansas A Binary Search Tree of States Storage rules: 1.Every key to the left of a node is alphabetically before the key of the node. Washington Oklahoma Florida Mass. New Hampshire West Virginia Example: ' Massachusetts' and ' New Hampshire' are alphabetically before 'Oklahoma'

Arizona Arkansas A Binary Search Tree of States Storage rules: 1.Every key to the left of a node is alphabetically before the key of the node. 2.Every key to the right of a node is alphabetically after the key of the node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas A Binary Search Tree of States Storage rules: 1.Every key to the left of a node is alphabetically before the key of the node. 2.Every key to the right of a node is alphabetically after the key of the node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas Retrieving Data Start at the root. 1. If the current node has the key, then stop and retrieve the data. 2. If the current node's key is too large, move left and repeat If the current node's key is too small, move right and repeat 1-3. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas Retrieve ' New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root. 1. If the current node has the key, then stop and retrieve the data. 2. If the current node's key is too large, move left and repeat If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Retrieve 'New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root. 1. If the current node has the key, then stop and retrieve the data. 2. If the current node's key is too large, move left and repeat If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Retrieve 'New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root. 1. If the current node has the key, then stop and retrieve the data. 2. If the current node's key is too large, move left and repeat If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Retrieve 'New Hampshire' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root. 1. If the current node has the key, then stop and retrieve the data. 2. If the current node's key is too large, move left and repeat If the current node's key is too small, move right and repeat 1-3.

Retrieval: Complexity Given a complete tree of N items, the depth D = O(log N). What is the maximum number of nodes tested to retrieve an item from the binary search tree if we use a complete tree? What is the maximum number of nodes tested (worst case) to retrieve an item from a binary search tree that is not complete or balanced?

Arizona Arkansas Adding a New Item with a Given Key 1. Pretend that you are trying to find the key, but stop when there is no node to move to. 2. Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas Adding 1.Pretend that you are trying to find the key, but stop when there is no node to move to. 2.Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding 1.Pretend that you are trying to find the key, but stop when there is no node to move to. 2.Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding 1.Pretend that you are trying to find the key, but stop when there is no node to move to. 2.Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding 1.Pretend that you are trying to find the key, but stop when there is no node to move to. 2.Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding 1.Pretend that you are trying to find the key, but stop when there is no node to move to. 2.Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding 1.Pretend that you are trying to find the key, but stop when there is no node to move to. 2.Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Where would you add this state? Where would you add this state? Kazakhstan

Arizona Arkansas Adding Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan is the new right child of Iowa? Kazakhstan is the new right child of Iowa? Kazakhstan

Adding: Complexity Given a complete tree of N items, the depth D = O(log N). What is the maximum number of nodes tested to add an item to the binary search tree if we use a complete tree? What is the maximum number of nodes tested (worst case) to add an item from a binary search tree that is not complete or balanced?

Arizona Arkansas Removing an Item with a Given Key 1.Find the item. 2.If necessary, swap the item with one that is easier to remove. 3.Remove the item. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

Arizona Arkansas Removing 'Florida' 1.Find the item. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan Florida cannot be removed at the moment... Florida cannot be removed at the moment...

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... because removing Florida would break the tree into two pieces.... because removing Florida would break the tree into two pieces.

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan The problem of breaking the tree happens because Florida has 2 children. The problem of breaking the tree happens because Florida has 2 children. 1.If necessary, do some rearranging.

Arizona Arkansas Removing 'Florida' If necessary, do some rearranging. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan For the rearranging, take the smallest item in the right subtree... For the rearranging, take the smallest item in the right subtree...

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan Iowa...copy that smallest item onto the item that we're removing......copy that smallest item onto the item that we're removing... If necessary, do some rearranging.

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... and then remove the extra copy of the item we copied and then remove the extra copy of the item we copied... If necessary, do some rearranging.

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... and reconnect the tree If necessary, do some rearranging.

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Kazakhstan Why did we choose the smallest item in the right subtree? Why did we choose the smallest item in the right subtree?

Arizona Arkansas Removing 'Florida' Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan Because every key must be smaller than the keys in its right subtree

Removing an Item with a Given Key 1.Find the item. 2.If the item has a right child, rearrange the tree: 1.Find smallest item in the right subtree 2.Copy that smallest item onto the one that you want to remove 3.Remove the extra copy of the smallest item (making sure that you keep the tree connected) 3. else just remove the item.

Binary search trees are a good implementation of data types such as sets, bags, and dictionaries. Searching for an item is generally quick since you move from the root to the item, without looking at many other items. Adding and deleting items is also quick. But as you'll see later, it is possible for the quickness to fail in some cases -- can you see why? Summary