Boggle Game: Backtracking Algorithm Implementation

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Trees Types and Operations
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Binary Trees CS 110: Data Structures and Algorithms First Semester,
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
© 2004 Goodrich, Tamassia (2,4) Trees
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
Data Structures Using C++1 Chapter 11 Binary Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Digital Electronics Data Structures LISP
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Data Structures Week 6: Assignment #2 Problem
Boggle Game: Backtracking Algorithm Implementation
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 ),
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Trees – Part 2 CS 367 – Introduction to Data Structures.
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.
Announcements Exam Friday. More Physical Storage Lecture 10.
Tree Data Structures.
Balanced search trees: 2-3 trees. 2-3 trees allow us to process ordered lists in more efficient way than binary trees with an ordering property. Recall.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Starting at Binary Trees
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Graphs – Part II CS 367 – Introduction to Data Structures.
Binary Search Trees (BST)
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.
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. 
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Search Radix search trie (RST) R-way trie (RT) De la Briandias trie (DLB)
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Trees Binary Trees Extended Binary Trees. Tree A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree.
Generic Trees—Trie, Compressed Trie, Suffix Trie (with Analysi
Pointers and Linked Lists
Pointers and Linked Lists
Trees ---- Soujanya.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Trees (Continued)
Binary Search Tree (BST)
Problems with Linked List (as we’ve seen so far…)
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Chapter 10.1 Binary Search Trees
Radix search trie (RST) R-way trie (RT) De la Briandias trie (DLB)
Introduction to C++ Recursion
Digital Search Trees & Binary Tries
i206: Lecture 13: Recursion, continued Trees
Pointers and Linked Lists
Lesson Objectives Aims
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Data Structures Using C++ 2E
Balanced search trees: 2-3 trees.
Presentation transcript:

Boggle Game: Backtracking Algorithm Implementation CS 1501

Recursive Implementation Things to implementation for a recursive algorithm: Find the common behavior in the algorithm, and implement that as a function. Action: what to do for the current situation Recursive call: how to trigger next step Stop criteria: when to stop further recursion function Test (data) { if data satisfies stop criteria ------- (1) return; do sometime for data ------- (2) for all new_data from data Test (new_data); ------- (3) } 2017/4/12

Recursive Boggle Each move on the board can be a recursive call They accomplish similar tasks based on the string we have so far. function AdvanceStep (pos, string) { new_string = string + letter[pos]; if new_string is not prefix, not word -------- (1) return; } 2017/4/12

Recursive Boggle Each move on the board can be a recursive call They accomplish similar tasks based on the string we have so far. function AdvanceStep (pos, string) { new_string = string + letter[pos]; if new_string is not prefix, not word -------- (1) return; if new_string is a word -------- (2) output string } 2017/4/12

Recursive Boggle Each move on the board can be a recursive call They accomplish similar tasks based on the string we have so far. function AdvanceStep (pos, string) { new_string = string + letter[pos]; if new_string is not prefix, not word -------- (1) return; if new_string is a word -------- (2) output string if new_string is a prefix -------- (3) for all possible next step new_pos AdvanceStep (new_pos, new_string); return } 2017/4/12

de la Briandias Tree: Dealing with string prefix CS 1501

Note Structure DLB tree is used to organize key set (dictionary). A path from root to leaf represents a word. a Child Pointer Char Sibling b c d e $ f $ $ abe ac adf 2017/4/12

Node Structure Determine prefix? Determine word? a Char b c d e $ f $ The node has at least one child that is not a “end-of-string” sign. Determine word? Node has a child with “end-of-string” sign. a Child Pointer Char Sibling b c d e $ f $ $ abe ac adf 2017/4/12

An Example Construct a DLB tree for the following words: abc, abe, abet, abx, ace, acid hives, iodin, inval, zoo, zool, zurich 2017/4/12

Insert Operation INSERTION (tree pointer TREE, word WORD) { out = 0; letter = word [at position i]; current = TREE; DO WHILE (( i <= length of WORD) and (out=0)) { IF (letter equals (current->key)) { //if we have a match i = i +1; letter = word[ at position i]; current = current->child_pointer; //follow the child } ELSE { //we have to check siblings IF (current->sibling_pointer does not equal Null) current = current->sibling; ELSE out=1; //no sibling, we add new node } } (end of DO WHILE) …… // insertion 2017/4/12

Insert Operation INSERTION (tree pointer TREE, word WORD) { …….. // search // we're at the point of insertion of new node, // unless the word is already there: IF (out = 0) EXIT //if the word was already there, exit // otherwise add a new node with the current letter current->sibling_pointer = CREATE new NODE (letter); i = i + 1; // and move on to append the rest of the letters FOR (m=i ; m<= length of WORD; m++) { current.child_pointer = CREATE new NODE ( WORD[at position m]); current = current->child_pointer; } // now we just add the $ marker for end of word current.child_pointer = CREATE new TERMINAL $ MARKER NODE; 2017/4/12

Delete Operation DELETE (tree pointer TREE, word WORD) { current = TREE; // we start with the first node again out=0; i=0; letter = WORD [at position i]; DO WHILE (( i <= length of WORD) and (out=0)) { IF (letter is equal to current->key) current = current->child_pointer; //we move down one level i = i + 1; letter = WORD [at position i]; // we move onto next letter ELSE IF (there is a sibling node) { last_sibling = current; //store the last sibling current = current->sibling // move to the sibling node } IF (there is no sibling node) out =1; // EXIT with ERROR } DO WHILE …… // deleteion 2017/4/12

Delete Operation DELETE (tree pointer TREE, word WORD) { ……. // search PUSH last_sibling->sibling_pointer onto STACK PUSH all the children of last_sibling->sibling_pointer onto STACK one by one, until the $ marker Set all of the pointers on STACK to Null. as you're POPPING them off the STACK } 2017/4/12

Corner cases The tree is empty when insert a word The word is the last one in the tree … 2017/4/12