0% found this document useful (0 votes)
11 views

Term_2_IP_problem_sheet_6

The document contains solutions to problem sheets related to data structures, specifically focusing on hash tables and binary trees. It includes code snippets for adding, counting, and deleting elements in a hash table, as well as methods for printing and flipping binary trees. Additionally, it discusses the longest pairs of words and sets of anagrams found using a specific dictionary.

Uploaded by

oysterman0123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Term_2_IP_problem_sheet_6

The document contains solutions to problem sheets related to data structures, specifically focusing on hash tables and binary trees. It includes code snippets for adding, counting, and deleting elements in a hash table, as well as methods for printing and flipping binary trees. Additionally, it discusses the longest pairs of words and sets of anagrams found using a specific dictionary.

Uploaded by

oysterman0123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6 Barnaby Foyster, Problem sheet Solutions

5
4
6.1
3
2
1
Well if N is small, which it should be, this shouldn’t matter.

6.2

6.3

class HashTable {
private class Node(var word : String, var count : Int)
private val table = new Array[Node](MAX)

def add(x : String) : Unit = {


var h = hash(x)
while (0==0) {
if (table(h).count == 0) {
table(h).word = x
table(h).count = 1
return
} else if (table(h).word == x) {
table(h).count += 1
return
} else {h += 1; h = h % MAX}
}
}

1
def count(x : String) : Int = {
var h = hash(x)
while (0==0) {
if (table(h).word == x) {return table(h).count}
else if (table(h).word == null) {return 0}
else {h += 1; h = h % MAX}
}
}

def delete(x : String) : Unit = {


var h = hash(x)
while (0==0) {
if (table(h).word == x) {
if (table(h).count == 1) {
var swap = h
while (table(swap).word != null) {
swap += 1; swap = swap % MAX
}
table(h).word = table(swap-1).word
table(h).count = table(swap-1).count
return
} else {table(h).count -= 1}
}
else {h += 1; h = h % MAX}
}
}
}

2
6.4

(a) def printTree(t : Tree, depth : Int) : Unit = {

if (t == null) {
for (i <- 1 to depth) {print(".")}
println("null")
} else {
for (i <- 1 to depth) {print(".")}
println(t.word)
printTree(t.left, depth +1)
printTree(t.right, depth+1)
}
}

(b) def printTree(t : Tree) : Unit = {

var s = Stack[(Tree, Int)]()


s.push(tree)
while (!s.isEmpty) {
var t = s.pop
if (t._1 == null) {
for (i <- 1 to t._2) {print(".")}
println("null")
} else {
for (i <- 1 to t._2) {print(".")}
println(t._1.word)
s.push((t._1.left, t._2 +1))
s.push((t._1.right, t._2+1))
}

3
}
}

6.5

def flip(t: Tree) : Unit = {


if (t != null) {
val temp = flip(t.right)
t.right = flip(t.left)
t.left = temp
}
}

6.6

def total(t: Tree) : Int = {


if (t == null) {return 0}
else {return t.count + total(t.left) + total(t.right)}
}

def total(t: Tree) : Int = {


var r = 0
var s = new Stack[Tree]()
s.push(t)
while (!s.isEmpty) { //Inv: total = r + T(t) forall t in s
val t = stack.pop
if (t != null) {
r += t.count

4
s.push(t.left)
s.push(t.right)
} //var: T(t) forall t in s
}
return r
}

6.7

(a) I’ve just sent in the scala file to make this easier

This is slow because we have to create every single permutation, many of which are

unlikely to be real words

(b) The longest pair of words I could find using this dictionary1 was electromagnetic and

magnetoelectric.

The longest sets of anagrams I could find were:

• (tesla, teals, tales, steal, stale, slate, least)

• (spear, spare, reaps, rapes, pears, parse, pares)

• (timers, smiter, remits, mitres, miters, mister, merits)

1
https://people.sc.fsu.edu/~jburkardt/datasets/words/anagram_dictionary.txt

You might also like