Serializing Trees, General Trees

Data Structures and Algorithms Serializing Trees, General Trees Chris Brooks Department of Computer Science University of San Francisco Department o...
Author: Arabella James
6 downloads 2 Views 101KB Size
Data Structures and Algorithms

Serializing Trees, General Trees Chris Brooks Department of Computer Science University of San Francisco

Department of Computer Science — University of San Francisco – p.1/20

11-0:

Dealing with Project 2

Basic steps to compress: Read in a text file Count the frequency of each character Build Huffman tree Build lookup table from tree Write tree out to file Write compressed characters out to file.

Department of Computer Science — University of San Francisco – p.2/20

11-1:

Dealing with Project 2

Basic steps to uncompress: Open binary file Read in and rebuild tree Use tree to decode remaining characters

Department of Computer Science — University of San Francisco – p.3/20

11-2:

Reading from files

You can use the provided TextFile and BinaryFile classes for this. Start by writing a program that opens a text file or binary file, reads each character/byte, and writes to standard out. Step 2: Add in frequency counting.

Department of Computer Science — University of San Francisco – p.4/20

11-3:

Dealing with the Huffman tree

Step 3: Build a huffman tree Build one tree for each character Combine two lowest-frequency characters. See last week’s notes. Once tree is finished, do a preorder traversal to get a lookup table.

Department of Computer Science — University of San Francisco – p.5/20

11-4:

Serializing Binary Trees

Print a tree to a file, saving structure information First Try: Print out nodes, in order that they would appear in a PREORDER traversal. Why doesn’t this work?

A B D

C E

F

G ABDEGCF

Department of Computer Science — University of San Francisco – p.6/20

11-5:

Serializing Binary Trees

Printing out nodes, in order that they would appear in a PREORDER traversal does not work, because we don’t know when we’ve hit a null pointer Store null pointers, too!

A B D

C E

F

G ABD//EG///C/F//

Department of Computer Science — University of San Francisco – p.7/20

11-6:

Serializing Binary Trees

In most trees, more null pointers than internal nodes Instead of marking null pointers, mark internal nodes Still need to mark some nulls, for nodes with 1 child

A B D

C E

F

G A’B’DE’G/C’/F

Department of Computer Science — University of San Francisco – p.8/20

11-7:

Trees with > 2 children

We might want to have a tree with more than two children How can we implement this?

Department of Computer Science — University of San Francisco – p.9/20

11-8:

Trees with > 2 children

Array of Children

Department of Computer Science — University of San Francisco – p.10/20

11-9:

Trees with > 2 children

Linked List of Children

Department of Computer Science — University of San Francisco – p.11/20

11-10:

Left Child / Right Sibling

We can integrate the linked lists with the nodes themselves:

Department of Computer Science — University of San Francisco – p.12/20

11-11:

Working with General Tree

class Node { private Node leftchild; private Node rightsib; private Object element; Node leftchild() { return leftchild; }

void setLeftchild(Node lc) { leftchild= lc; }

Node rightsib() { return rightsib; }

void setRightsib(Node rs) { rightsib = rs; }

Node element() { return element; }

void setElement(Object e) { e = e; }

}

Department of Computer Science — University of San Francisco – p.13/20

11-12:

General Trees – NumNodes

Returns the number of nodes in a tree

Number of Nodes = 8

Number of Nodes = 6

Department of Computer Science — University of San Francisco – p.14/20

11-13:

General Trees – NumNodes

int numnodes(Node tree) { int descendants = 0; Node tmp; if (tree == null) return 0; for (tmp = tree.leftchild(); tmp != null; tmp = tmp.rightsib()) descendants = descendants + numnodes(tmp); return descendants + 1; }

Department of Computer Science — University of San Francisco – p.15/20

11-14:

General Trees – NumNodes II

int numnodes(Node tree) { if (tree == null) return 0; return 1 + numnodes(tree.leftchild()) + numnodes(tree.rightsib()); }

Department of Computer Science — University of San Francisco – p.16/20

11-15:

Tree Operations – Height

Returns the height of the tree (Length of the path to the deepest leaf) + 1

Height = 5

Height = 6

Department of Computer Science — University of San Francisco – p.17/20

11-16:

General Trees – Height

int height(Node tree) { if (tree == null) return 0; return MAX((1 + height(tree.leftchild())), height(tree.rightsib())); }

Department of Computer Science — University of San Francisco – p.18/20

11-17: Tree 1

Tree 2 1

2

1

3

5

4

6

2

3

7

1

2

5

General Trees

4

3

6

4

7

8

5

9

Tree 3

Department of Computer Science — University of San Francisco – p.19/20

11-18:

Serializing General Trees

Store an “end of children” marker

A C

B E

F

D G

H I J

K ABE)FK)))C)DG)H)I)J)))

Department of Computer Science — University of San Francisco – p.20/20