Tree Tutorial

From Progzoo
Revision as of 09:44, 9 June 2011 by Spoon! (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

We will use a number of mechanisms to represent trees.

Immutable Trees

An immutable tree never changes. Once a node is created it may be used as part of another tree. The methods used are static because we do not take advantage of objects.

Immutable Binary Trees

This example creates a tree and prints it out in in-order.

Binary Tree

Change the file to that the node England has two children "North" and "South".



[Font] [Default] [Show] [Resize] [History] [Profile]

Pre-Order

Binary Tree

Change the function walk-over so that it prints in pre-order.

Europe
UK
Scotland
England
France



[Font] [Default] [Show] [Resize] [History] [Profile]

Count Nodes

Complete the function to count the nodes.



[Font] [Default] [Show] [Resize] [History] [Profile]


Tree Depth

Complete the function to count the depth of the tree.



[Font] [Default] [Show] [Resize] [History] [Profile]

Flip a Tree

You flip a tree by putting the left branches on the right and visa versa.

A Tree and Flip Tree



[Font] [Default] [Show] [Resize] [History] [Profile]

Insert in Order

You must return a tree with the same root node as before. You add the new item to the left or the right depending on the compareTo result.



[Font] [Default] [Show] [Resize] [History] [Profile]

Map Tree

You must return a tree with all the values doubled.



[Font] [Default] [Show] [Resize] [History] [Profile]