Docsity

Implementing and Serializing General Trees with More than Two Children - Prof. David J. Ga, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Implementing and Serializing General Trees with More than Two Children - Prof. David J. Ga and more Study notes Data Structures and Algorithms in PDF only on Docsity!

09-0: Trees with > 2 children

How can we implement trees with nodes that have > 2 children?

09-1: Trees with > 2 children

  • Array of Children

09-2: Trees with > 2 children

  • Linked List of Children

09-3: Left Child / Right Sibling

  • We can integrate the linked lists with the nodes themselves:

09-4: Working with General Tree

class Node { private Node leftchild_; private Node rightsib_; private Object element_; Node leftchild() { void setLeftchild(Node leftchild) { return leftchild_; leftchild_ = leftchild; } } Node rightsib() { void setRightsib(Node leftchild) { return rightsib_; rightsib_ = rightsib; } } Node element() { void setElement(Object element) { return element_; element_ = element; } } }

09-5: General Trees – NumNodes

  • Returns the number of nodes in a tree

Height = 5 Height = 6 09-9: General Trees – Height

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

09-10: General Trees

1

2 3 4

5 6 7

1

2

2 3 4

(^5 6 )

1

3

4

5

8 9

Tree 1 Tree 2

Tree 3 Write numLeaves and print 09-11: 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 C

D E

G

F

ABDEGCF

09-12: 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 C

D E

G

F

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

09-13: 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 C

D E

G

F

09-14: 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!
  • If we are searializing a full binary tree (each node contains exactly 0 or 2 children), we can store a single extra bit for each node 0 for an internal node, 1 for a leaf:

A

B

C D

E

F^ G

09-19: Serializing Binary Trees

  • If we are searializing a full binary tree (each node contains exactly 0 or 2 children), we can store a single extra bit for each node 0 for an internal node, 1 for a leaf:

A

B

C D

E

F^ G

A 0 B 0 C 1 D 1 E 0 F 1 G 1

09-20: Serializing Binary Trees

  • If we are searializing a full binary tree (each node contains exactly 0 or 2 children), we can store a single extra bit for each node 0 for an internal node, 1 for a leaf:
A 0 B 0 C 1 D 0 E 1 F 1 G 1

09-21: Serializing Binary Trees

  • If we are searializing a full binary tree (each node contains exactly 0 or 2 children), we can store a single extra bit for each node 0 for an internal node, 1 for a leaf:
A 0 B 0 C 1 D 0 E 1 F 1 G 1

A

B

D

E F

G

C

09-22: Serializing General Trees

  • Store an “end of children” marker

A

B D

E F I

C

G H J

K

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

09-23: Serializing General Trees

  • Store an “end of children” marker

A

B D E

F I

C

G H J

K

09-24: Serializing General Trees

  • Store an “end of children” marker

A

B D E

F I

C

G H J

K

ABF K)))CG)H))DI)J))E))

09-25: Serializing General Trees