You are on page 1of 2

CSci 1913 Lab 10 - Experimenting with trees

Step 1) The TreeNode A tree is just a collection of nodes, similar to the linked lists weve been working with the past couple weeks. The difference is in how we arrange them. In a tree, each node has a parent node and two children, though well only be worrying about the children for this lab. For now, lets focus on the nodes. Create a class TreeNode. Each node has a generic data of type T, much like the linked list nodes. Additionally, it will have two pointers to other nodes, one called right and one called left. These represent the left and right children: T data; TreeNode right; TreeNode left;

Step 2) The BinaryTree Secretly, our nodes are for a very particular type of tree, called a binary tree. Its binary because each node has up to two possible children. A full binary tree means every node has exactly two children. Here the tree may not be a full binary tree. Make a new class BinaryTree and write the constructors for this class. You have to make two constructors, one which takes no data and instantiates an empty tree and one which takes in data of type T and sets the root of the tree to a node with this data. TreeNode root; BinaryTree<T>(); BinaryTree<T>(T data);

Step 3) Adding a node Next we need to work out how to add nodes from the tree. Adding a new node means finding an empty child slot of a node already in the tree. Here, we do not have to worry about sorting etc. In order to do this you will have to search the free node in the tree. There are two most basic search algorithms for this purpose: depth-first and breadth-first. Depth-first search attempts to search as far down into the tree as it can, before backtracking to search other leaves. Breadth-first algorithm searches each level entirely before moving on to the next level. (Its probably easier to use breadth-first search).

Since we just learned how to use queues and queues are fun, lets use them to do a breadth-first approach to add a new node. HINT: The queue is a first in first out data structure. If we push a node s children onto the queue, then pop them off one at a time, repeating the process for all nodes, in what order are we inspecting each node? Our goal with breadth-first add is to walk through the tree level by level and find the first node with at least one missing child, adding our new node as a child of this node. print() Additionally, we need to write a print function which walks through the tree and print out the values contained within each node. This should be done in a manner similar to your add method (i.e. BFS). Start by writing the toString(). You will override this from the object class.

Step 4) findDFS(T data) Implement a findDFS() which uses the depth-first search algorithm to navigate the tree. The goal is to search the tree for the data passed into the function. Using DFS algorithm, we search the tree in the depth wise manner i.e going far deep in tree before backtracking. The function return true if the data is contained in some node of the tree already and false otherwise. HINT: recursion is your friend.

Step 5) No Duplication Lets make our tree more sophisticated. Modify your add method in such a way that it checks for duplicate entry i.e. add the new data only when that data is not present in the tree already. Step 6) Unit test Write unit tests to test the following functions: 1. Add() with duplicate. Use the toString() function to check the output of the tree. 2. findDFS(T data) 3. Add() without duplicates. Use the toString() function to check the output of the tree.

You might also like