Sunday, March 6, 2016

CS 2420 - Assignment 5 - Part 2

So I've been working on my Binary Search tree a bit more, and I have made some changes since last time. For example, in the assignment the traverse functions need to return a string of the numbers instead of being void.

Here is what I have now:

using System;

class BinarySearchTree
{
    public TreeNode root;
    private int node_count;

    public BinarySearchTree()
    {
        root = null;
        node_count = 0;
    }

    public void Add(int item)
    {
        TreeNode new_node = new TreeNode(item);

        if (root == null)
            root = new_node;
        else
        {
            TreeNode crawler = root;

            while (crawler != null)
            {
                new_node.parent = crawler;
                if (item >= crawler.data)
                    crawler = crawler.right;
                else
                    crawler = crawler.left;
            }

            if (item >= new_node.parent.data)
                new_node.parent.right = new_node;
            else
                new_node.parent.left = new_node;
        }
        node_count++;
        CalculateHeight(root);
        Console.WriteLine("");
    }

    public TreeNode Contains(int item)
    {
        TreeNode crawler = root;

        while (crawler != null)
        {
            if (item == crawler.data)
                return crawler;
            else if (item >= crawler.data)
                crawler = crawler.right;
            else
                crawler = crawler.left;
        }
        return null;
    }

    // TODO Finish Remove function
    public void Remove(TreeNode node)
    {
        if (node == null)
            Console.WriteLine("\nCould not remove the node you were looking for!\n");
        else
        {
            //Work needed
        }
    }

    public void Clear()
    {
        root = null;
        node_count = 0;
    }

    public int NodeCount
    {
        get
        {
            return node_count;
        }
    }

    private void CalculateHeight(TreeNode node)
    {
        if (node.left != null)
            CalculateHeight(node.left);
        if (node.right != null)
            CalculateHeight(node.right);

        if (node.left != null && node.right != null)
            node.height = Math.Max(node.left.height, node.right.height) + 1;

        else if (node.left != null && node.right == null)
            node.height = node.left.height + 1;

        else if (node.left == null && node.right != null)
            node.height = node.right.height + 1;

        else if (node.left == null && node.right == null)
            node.height = 1;
        Console.WriteLine("Node value: {0} Node Height: {1}", node.data, node.height);
        CheckBalance(node);
    }

    private void CheckBalance(TreeNode node)
    {
        if (node.left == null && node.right == null)
            return;

        else if (node.left != null && node.right == null)
            if (node.left.height > 1)
                Rotate(node);

            else if (node.left == null && node.right != null)
                if (node.right.height > 1)
                    Rotate(node);

                else if (node.left != null && node.right != null)
                {
                    if (node.left.height > node.right.height + 1)
                        Rotate(node.left);
                    else if (node.right.height > node.left.height + 1)
                        Rotate(node.right);
                }
    }

    // TODO Finish Rotate function
    private void Rotate(TreeNode node)
    {
        //Work needed
    }

    public string TraversePre(MyList list, TreeNode node)
    {
        list.Add(node.data);
        if (node.left != null)
            TraversePre(list, node.left);
        if (node.right != null)
            TraversePre(list, node.right);
        return string.Join(", ", list);
    }

    public string TraverseIn(MyList list, TreeNode node)
    {
        if (node.left != null)
            TraverseIn(list, node.left);
        list.Add(node.data);
        if (node.right != null)
            TraverseIn(list, node.right);
        return string.Join(", ", list);
    }

    public string TraversePost(MyList list, TreeNode node)
    {
        if (node.left != null)
            TraversePost(list, node.left);
        if (node.right != null)
            TraversePost(list, node.right);
        list.Add(node.data);
        return string.Join(", ", list);
    }
}

There has been a few changes to the rest of the code as well. I have been trying to figure out how to make my tree self balancing. And while I have been able to calculate the depth/height of each node, I am running into issues on how I should rotate the nodes. I am also working on removing nodes, but I haven't made much progress in that department either.

Anyways, that's all for now. Thanks for reading!

No comments:

Post a Comment