Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Balancing a Binary Search Tree

$
0
0

Hi,

I'm trying to implement a binary search tree. I got the insert function to work, but it doesn't balance the tree as a BST should be. How do I implement a balance() function in the binary_tree class?

#pragma once
#include <iostream>
using namespace std;
template<class T>
struct TreeNode
{
	T item;         // The data in this node.
	TreeNode<T> *left;   // Pointer to the left subtree.
	TreeNode<T> *right;  // Pointer to the right subtree.
};

template<typename T>
class binary_tree
{
	TreeNode<T>* ROOT;
public:
	int countNodes(TreeNode<T>* root)
	{
		// Count the nodes in the binary tree to which
		// root points, and return the answer.
		if (root == nullptr)
			return 0;  // The tree is empty.  It contains no nodes.
		else
		{
			int count = 1;   // Start by counting the root.
			count += countNodes(root->left);  // Add the number of nodes in the left subtree.
			count += countNodes(root->right); // Add the number of nodes in the right subtree.
			return count;  // Return the total.
		}
	}
	int countNodes()
	{
		if (ROOT == nullptr)
			return 0;  // The tree is empty.  It contains no nodes.
		else
		{
			int count = 1;   // Start by counting the root.
			count += countNodes(ROOT->left);  // Add the number of nodes in the left subtree.
			count += countNodes(ROOT->right); // Add the number of nodes in the right subtree.
			return count;  // Return the total.
		}
	}
	// In a post order print, the left sub-tree is processed first, then the right sub-tree, then the base
	void postorderPrint(TreeNode<T>* root)
	{
		// Print all the items in the tree to which root points.
		// The items in the left subtree are printed first, followed 
		// by the items in the right subtree and then the item in the
		// root node.
		if (root != nullptr) // Otherwise, there's nothing to print.
		{
			postorderPrint(root->left);    // Print items in left subtree.
			postorderPrint(root->right);   // Print items in right subtree.
			cout << root->item << " ";       // Print the root item.
		}
	}
	void postorderPrint()
	{
		if (ROOT != nullptr) // Otherwise, there's nothing to print.
		{
			postorderPrint(ROOT->left);    // Print items in left subtree.
			postorderPrint(ROOT->right);   // Print items in right subtree.
			cout << ROOT->item << " ";       // Print the root item.
		}
	}
	// In a preorder print, the root is printed first, then the left subtree, then the right one.
	void preorderPrint(TreeNode<T>* root)
	{
		// Print all the items in the tree to which root points.
		// The item in the root is printed first, followed by the
		// items in the left subtree and then the items in the
		// right subtree.
		if (root != nullptr) // Otherwise, there's nothing to print.
		{
			cout << root->item << " ";      // Print the root item.
			preorderPrint(root->left);    // Print items in left subtree.
			preorderPrint(root->right);   // Print items in right subtree.
		}
	}
	void preorderPrint()
	{
		if (ROOT != nullptr) // Otherwise, there's nothing to print.
		{
			cout << ROOT->item << " ";      // Print the ROOT item.
			preorderPrint(ROOT->left);    // Print items in left subtree.
			preorderPrint(ROOT->right);   // Print items in right subtree.
		}
	}
	// In an inorder print, the left subtree is printed first, then the root, then the right subtree.
	void inorderPrint(TreeNode<T>* root)
	{
		// Print all the items in the tree to which root points.
		// The items in the left subtree are printed first, followed 
		// by the item in the root node, followed by the items in
		// the right subtree.
		if (root != nullptr) // Otherwise, there's nothing to print.
		{
			inorderPrint(root->left);    // Print items in left subtree.
			cout << root->item << " ";     // Print the root item.
			inorderPrint(root->right);   // Print items in right subtree.
		}
	}
	void inorderPrint()
	{
		if (ROOT != nullptr) // Otherwise, there's nothing to print.
		{
			inorderPrint(ROOT->left);    // Print items in left subtree.
			cout << ROOT->item << " ";     // Print the ROOT item.
			inorderPrint(ROOT->right);   // Print items in right subtree.
		}
	}
	void modify(TreeNode<T>* node, T inf)
	{
		if (node == nullptr)
			return;
		else
			node->item = inf;
	}
	void insert(T info, TreeNode<T>* node)
	{
		// If there is no ROOT, then make one
		if (ROOT == nullptr)
		{
			ROOT = new TreeNode<T>;
			ROOT->item = info;
			ROOT->left = ROOT->right = nullptr;
			return;
		}
		if (node->item == info)
			return;
		if (node->item > info)
		{
			if (node->left != nullptr)
				insert(info, node->left);
			else
			{
				node->left = new TreeNode<T>;
				node->left->item = info;
				node->left->left = nullptr;
				node->left->right = nullptr;
				return;
			}
		}
		else
		{
			if (node->right != nullptr)
				insert(info, node->right);
			else
			{
				node->right = new TreeNode<T>;
				node->right->item = info;
				node->right->left = nullptr;
				node->right->right = nullptr;
				return;
			}
		}
	}
	TreeNode<T>* GetRootNode()
	{
		return ROOT;
	}
	int maxDepth(TreeNode<T>* node)
	{
		if (node == nullptr) 
		{
			return 0;
		}
		else 
		{
			// compute the depth of each subtree 
			int lDepth = maxDepth(node->left);
			int rDepth = maxDepth(node->right);
			// use the larger one 
			if (lDepth > rDepth) 
				return (lDepth + 1);
			else 
				return (rDepth + 1);
		}
	}
	T minValue(TreeNode<T>* node)
	{
		TreeNode<T>* current = node;
		// loop down to find the leftmost leaf 
		while (current->left != nullptr) {
			current = current->left;
		}

		return current->item;
	}
	void reverse(TreeNode<T>* node)
	{
		if (node == nullptr) 
		{
			return;
		}
		else 
		{
			TreeNode<T>* temp;

			// do the subtrees 
			reverse(node->left);
			reverse(node->right);

			// swap the pointers in this node 
			temp = node->left;
			node->left = node->right;
			node->right = temp;
		}
	}
	binary_tree(T inf)
	{
		ROOT = new TreeNode<T>;
		ROOT->item = inf;
		ROOT->left = nullptr;
		ROOT->right = nullptr;
		ROOT->index = 0;
	}
	binary_tree()
	{
		ROOT = nullptr;
	}
	~binary_tree()
	{
		delete ROOT;
	}
};

Thanks,

Rahul



Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>