Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

Trees Types and Operations
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
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.
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.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
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.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Week 15 – Friday.  What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Trees Chapter 15.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
i206: Lecture 14: Heaps, Graphs intro.
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.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
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.
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.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
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.
Binary Trees, Binary Search Trees
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps.
Presentation transcript:

Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3

Menu Taxonomy tree (decision tree) Tree traversal Heaps B-trees Other trees: –binary space partitioning trees –game trees

yesno dogcat yesno Talks?pig yesno Barks?mouse yesnoyesno Binary Taxonomy Tree Are you a mammal? yesno Are you bigger than a cat?Do you live underwater? Are you a primate?Are you a house pet?fishlegs? yesno toadslug yesno humanslug

Implementation: Tree Nodes Using a typedef statement: struct BinaryTreeNode { typedef string Item; Item data; BinaryTreeNode *left; BinaryTreeNode *right; }; Using a template parameter: template struct BinaryTreeNode { Item data; BinaryTreeNode *left; BinaryTreeNode *right; };

Binary Tree Represented with BinaryTreeNode Structs ‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ Using a template parameter: template struct BinaryTreeNode { Item data; BinaryTreeNode *left; BinaryTreeNode *right; };

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr

Tree Traversal Pre order – process parent before children Post order – process parent after children In order – process left subtree, parent, right subtree ‘T’ ‘C’ ‘A’ Pre order: C A T Post order: A T C In order: A C T

In-Order Traversal Process the nodes in the left subtree with a recursive call Process the root Process the nodes in the right subtree with a recursive call

Implementation template void inorder_print(BinaryTreeNode * node_ptr) { if(node_ptr != NULL) { inorder_print(node_ptr->left); cout data << endl; inorder_print(node_ptr->right); }

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr O

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr O L

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr O L R

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr O L R A

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr O L R A G

‘G’ ‘A’ ‘L’ ‘T’ ‘O’ ‘R’ data ‘A’ leftright data ‘L’ leftright data ‘O’ leftright data ‘G’ leftright data ‘T’ leftright data ‘R’ leftright root_ptr O L R A G T

In-order Traversal of Binary Search Trees In order traversal: 2, 3, 4, 5, 6, 10, In order traversal: 9, 55, 62, 453, 600

Representing Equations As Binary Trees * In order traversal: (22 – 2) + (22*5) 6 * - 5 /2 22 In order traversal: ((22/5) – 2)*6 Each internal node stores a binary operator Each leaf stores a value (or variable)

Pre-Order Traversal Process the nodes in the left subtree with a recursive call Process the nodes in the right subtree with a recursive call Process the root

Implementation template void preorder_print(BinaryTreeNode * node_ptr) { if(node_ptr != NULL) { preorder_print(node_ptr->left); preorder_print(node_ptr->right); cout data << endl; }

Post-Order Traversal Process the root Process the nodes in the left subtree with a recursive call Process the nodes in the right subtree with a recursive call

Implementation template void postorder_print(BinaryTreeNode * node_ptr) { if(node_ptr != NULL) { cout data << endl; postorder_print(node_ptr->left); postorder_print(node_ptr->right); }

Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

Heaps Complete binary tree.

Heaps A heap is a certain kind of complete binary tree. Each node in a heap contains a key that can be compared to other nodes' keys. Each node in a heap contains a key that can be compared to other nodes' keys

Heaps A heap is a certain kind of complete binary tree. Can be used to store priority queue The "heap property" requires that each node's key is >= the keys of its children The "heap property" requires that each node's key is >= the keys of its children

Adding a Node to a Heap ¶Put the new node in the next available spot. ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location

Adding a Node to a Heap ¶Put the new node in the next available spot. ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location

Adding a Node to a Heap ¶Put the new node in the next available spot. ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location

Adding a Node to a Heap 4The parent has a key that is >= new node, or 4The node reaches the root. ÚThe process of pushing the new node upward is called reheapification upward

Removing the Top of a Heap ¶Move the last node onto the root

Removing the Top of a Heap ¶Move the last node onto the root

Removing the Top of a Heap ¶Move the last node onto the root. ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location

Removing the Top of a Heap ¶Move the last node onto the root. ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location

Removing the Top of a Heap ¶Move the last node onto the root. ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location

Removing the Top of a Heap 4The children all have keys <= the out-of-place node, or 4The node reaches the leaf. ØThe process of pushing the new node downward is called reheapification downward

Implementing a Heap We will store the data from the nodes in a partially-filled array. An array of data

Implementing a Heap Data from the root goes in the first location of the array. An array of data

Implementing a Heap Data from the next row goes in the next two array locations. An array of data

Implementing a Heap Data from the next row goes in the next two array locations. An array of data

Implementing a Heap Data from the next row goes in the next two array locations. An array of data We don't care what's in this part of the array.

Important Points about the Implementation The links between the tree's nodes are not actually stored as pointers, or in any other way. The only way we "know" that "the array is a tree" is from the way we manipulate the data. An array of data

B-Trees B-tree is a special kind of tree, similar to a binary search tree where each node holds entries of some type. But a B-tree is not a binary tree: –nodes can have more than two children –Nodes can contain more than just a single entry Rules of B-tree make it easy to search for an item. Also ensures that the leaves do not become too deep.

B-tree: Example 93 and 107 subtree number 0 subtree number 1 subtree number 2 Each entry in subtree 0 is less than 93 Each entry in subtree 2 is greater than 107 Each entry in subtree 1 is between 93 and 107

B-tree Rules 1. Every node has at least MINIMUM number of entries. MINIMUM can be as small as 1, or as big as 100’s or 1000’s. 2. Maximum number of entries in a node is twice the value of MINIMUM. 3. The entries in a B-tree are stored in a partially- filled array, sorted from the smallest entry to the largest entry. 4. The number of subtrees below a node depends on how many entries are in the node.

B-tree: Rule 4 93 and 107 subtree number 0 subtree number 1 subtree number 2 Each entry in subtree 0 is less than 93 Each entry in subtree 2 is greater than 107 Each entry in subtree 1 is between 93 and 107 The number of subtrees below a node depends on how many entries are in the node.

B-tree: Rule 5 93 and 107 subtree number 0 subtree number 1 subtree number 2 Each entry in subtree 0 is less than 93 Each entry in subtree 2 is greater than 107 Each entry in subtree 1 is between 93 and 107 For any non-leaf node: (a) an entry at index i is greater than all the entries in subtree number i of the node, and (b) an entry at index i is less than all the entries in subtree number i+1 of the node.

B-tree: Rule 6 Every leaf in a B-tree has the same depth. 6 2 and 4 7 and

Operations on B-trees Searching for an item: easy Inserting item: see text Removing item: see text 6 2 and 4 7 and

Binary Space Partitioning Tree 2D Example: We are given a set of N points in the plane. Each point has an associated (x,y) position.

Binary Space Partitioning Tree Problem: How to efficiently determine if a query point (x,y) is contained in the set of points? ??

Possible Solutions Go through N points comparing to see if their (x,y) value matches query point (x,y). Drawback: O(N) complexity. Use some sort of tree structure to organize the points, and formulate query in terms of searching tree. Assuming uniform distribution of points, O(logN) complexity. How to structure tree??? Partition space using Binary Space Partitioning (BSP).

Building BSP Tree Approach: Recursively split space in half, in such a way that number of points in each half is roughly equal.

Building BSP Tree Approach: Recursively split space in half, in such a way that number of points in each half is roughly equal.

Building BSP Tree Approach: Recursively split space in half, in such a way that number of points in each half is roughly equal.

Building BSP Tree Approach: Recursively split space in half, in such a way that number of points in each half is roughly equal.

Building BSP Tree Recursively divide set of points using planes positioned that split the point set roughly in half. Stop recursively dividing when set is empty, or set is smaller than threshold M, or maximum tree depth is reached.

BSP Tree Result:

BSP Tree Search Is there a point in set that matches query point (x,y)?

BSP Tree Search Is there a point in set that matches query point (x,y)?

BSP Tree Search Is there a point in set that matches query point (x,y)?

BSP Tree Search Is there a point in set that matches query point (x,y)?

BSP Tree Search Compare query point only with those points in this leaf node.

BSP Tree Search: Complexity Assume that points are randomly, uniformly distributed in space (within some range). We recursively split point set in half at each internal node. For analysis, assume that we allow trees of unlimited depth, and that each leaf can contain only one point. Average case complexity, given N points?

Game Trees Consider writing a computer program that plays Tic-Tac-Toe against a human... X human makes first move... where should the computer move??? There are 8 possible choices.

Game Trees The computer can make 8 possible moves X Each possible choice represents one node at that level in the game tree. Which one should be chosen??

Game Trees The computer can make 8 possible moves X Each possible choice represents one node at that level in the game tree. Which one should be chosen??

Game Tree: Look Ahead For each possible move, see what possible moves are for opponent. For instance: X O Each node represents a possible move that the opponent could take.

Game Tree: Look Ahead Proceed recursively, testing all possible moves until opponent or computer wins. X O X Choose a move that leads to the most possible winning moves!!

Game Trees For each turn in the game, build game tree that tests possible move scenarios. Choose move that maximizes number of possible wins. Parts of the tree are reusable (depending on the opponents choice of next move). Problem: tree is quite large. Assume we have N=9 possible positions, what is the potential size O(f(N)) number of nodes?