Download Outline Notes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Quadtree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
CIS 55 Data Structures
Programming Assignment 2
In this assignment, you will modify the Binary Tree such that each node contains (at least) 2
additional properties as follows:
public class BinaryTreeNode
{
public int data;
public BinaryTreeNode leftChild;
public BinaryTreeNode rightChild;
public BinaryTreeNode parent;
public BinaryTreeNode rightNode;
}
The modified Binary Tree will use this Binary Tree Node to support the following:
1) The leftChild and rightChild pointers are used the same as an ordinary Binary Search Tree
2) The parent points to the parent BinaryTreeNode (if the BinaryTreeNode is root, parent is null)
3) The rightNode pointer points to the nearest Node to its right on the tree. This Node can either
be a sibling or some other node that is on the same level. The far right node of a tree at any
level should point to null.
This design modifies the standard Binary Search Tree. In the standard that we discussed in
class, the left and right child pointers are such that the data value on the left of any Node is less
than it and the data value on the right is greater. This standard remains for this modified Binary
Search Tree.
In addition, each Node can see (and thus, must maintain) the parent node. The complexity
arises when also allowing each Node to see the next Node to its right on the same level (note
that this can be the right sibling, first cousin, second cousin, ...). The rightNode pointer allows us
to basically have a Linked List at each level of the tree. For example:
6
4
2
1 3
8
7
9
10
Node 6: left = 4, right = 8, parent = null, rightNode = null
Node 4: left = 2, right = 7, parent = 6, rightNode = 8
Node 8: left = 7, right = 9, parent = 6, rightNode = null
Node 2: left = 1, right = 3, parent = 4, rightNode = 7
Node 3: left = null, right = null, parent = 2, rightNode = 10
...
Part 1) Complete the following functions for this class:
public BinaryTree()
public void insert(int n)
public boolean exists(int n)
public boolean delete(int n)
public void printInOrder()
public void printLevel(int n)
// Default constructor
// Insert number into tree
// Search function that returns true if the number exists in the tree
// If number n exists in the tree, remove it and return true
// Prints all numbers in tree in numerical order
// Print all the numbers for a given level n (n = 0 is the root level)
All boolean functions return false if the number to search for does not exist
When performing insert and delete, the parent and rightNode pointers must be modified
accordingly (i.e. all nodes in the tree must be correctly configured at all times)
Part 2) Write a main test program that validates functionality of all functions in part 1 covering all
cases. At minimum, you should be able to show the tree functions work using the earlier
example:
1) Use the correct sequence of insert functions to create the tree
2) Make calls to "exists" that return true and false for the appropriate search values
3) printInOrder should show "1 2 3 4 6 7 8 9 10"
4) printLevel should shot the following:
printLevel(0): 6
printLevel(1): 4 8
printLevel(2): 2 7 9
printLevel(3): 1 3 10
5) Perform tests for delete observing all 3 scenarios (delete leaf, node with one child and node
with 2 children) as well as deleting root and interesting node deletes that cause challenges with
maintaining the rightNode pointer
Part 3) Analysis: For each function (except the constructor and the print functions), answer the
following:
1) What is the Big-O performance category in the worst case of your function using compare
operations
2) How does this function compare to the standard Binary Search Tree version
Implementation note: There are many strategies to implementing this structure. You are
welcome to think outside the box and simulate the structure behavior in different ways (even
modifying the Node definition as well). In all cases, your analysis is key. If you're going to "fake"
the structure or functionality somehow, that is perfectly fine so long as you show the analysis.
Bonus question: Why connect the nodes on the same level? Can you find a practical application
to this structure?
Program notes:
1) The code samples above are in Java. You are welcome to use any other programming
language as long as the node and doubly linked list structure is the same and you create all
functions required
2) When complete, print your code and take screen shots of output that demonstrate all your
tests in part 2. Submit all paperwork to me by hand. Do not email me your work unless I request
it
3) Do not use exception handling such as try...catch blocks. Assume insert and append
functions will always have available memory and will successfully add a new number to the list
4) Do not worry about handling duplicate numbers
Group work (IMPORTANT): I recommend working on your own, but you are welcome to work
in pairs. If you work in pairs, let me know first. Do not work together without my approval. If
allowed, put both names on the work you submit. Working in groups of 3 or more is not allowed.
Do NOT submit work that is exactly the same or eerily similar. I've done enough teaching to spot
when work is shared between students. If I see this and the two students have not told me they
are actually working together in advance, I will question it and I reserve the right to mark BOTH
submissions as zero. This is a course that is university transferrable so the level of scrutiny is
raised a notch.
This is another tough assignment, particularly managing the rightNode pointers. If you are
unable to complete it by end of class, do the best you can and submit what you have for partial
credit. Good luck!