二叉树的实现(c++)

二叉树的实现(c++)

BinNode.h(节点的实现实现)

template <typename E> class BinNode
{
public:
	virtual ~BinNode() {};
	virtual E& element() = 0;
	virtual void setElement(const E&) = 0;
	virtual BinNode* left() const = 0;
	virtual void setLeft(BinNode *) = 0;
	virtual BinNode* right() const = 0;
	virtual void setRight(BinNode *) = 0;
	virtual bool isleaf() = 0;
};

BSTNode.h(二叉树节点的实现)

#include<iostream>
#include"BinNode.h"
template <typename Key, typename E>
class BSTNode :public BinNode<E>
{
private:
	Key k;
	E it;
	BSTNode* lc;
	BSTNode* rc;
public:
	BSTNode() { lc = rc = NULL; }
	BSTNode(Key K, E e, BSTNode* l = NULL, BSTNode* r = NULL)
	{
		k = K;
		it = e;
		lc = l;
		rc = r;
	}
	~BSTNode() {}
	E& element() { return it; }
	void setElement(const E& e) { it = e; }
	Key& key() { return k; }
	void setKey(const Key& K) { k = K };
	inline BSTNode* left() const { return lc; }
	void setLeft(BinNode<E>* b) { lc = (BSTNode*)b; }
	inline BSTNode* right() const { return rc; }
	void setRight(BinNode<E>* b) { rc = (BSTNode*)b; }
	bool isleaf() { return(lc == NULL) && (rc == NULL); }
};

BST.h(二叉树的实现)

#include"BSTNode.h"
using namespace std;
template<typename Key, typename E>
class BST
{
private:
	BSTNode<Key, E>* root;
	int count=0;
	int nodecount = 0;
	E findhelp(BSTNode<Key, E>* root, const Key& k) const
	{
		if (root == NULL)
			return NULL;
		if (k > root->key())
			return findhelp(root->right(), k);
		else if (k < root->key())
			return findhelp(root->left(), k);
		else return root->element();
	}

	BSTNode<Key, E>*inserthelp(BSTNode<Key, E>* root, const Key& k, const E it)
	{
		if (root == NULL)
			return new BSTNode<Key, E>(k, it, NULL, NULL);
		if (k < root->key())
			root->setLeft(inserthelp(root->left(), k, it));
		else
			root->setRight(inserthelp(root->right(), k, it));
		return root;
	}

	void clearhelp(BSTNode<Key, E>* root)//清空二叉树,释放内存
	{
		if (root == NULL)
			return;
		clearhelp(root->left());
		clearhelp(root->right());
		delete root;
	}

public:
	BST() { root = NULL; nodecount = 0; }
	~BST() { clearhelp(root); }
	void insert(const Key& k, const E& e)//用于插入新的结点
	{
		root = inserthelp(root, k, e);
		nodecount++;
	}

	E find(const Key& k)const//用于查找结点
	{
		return findhelp(root, k);
	}

	int size()//返回结点数量
	{
		return nodecount;
	}

	void preorder(BSTNode<Key, E>* root)//前序遍历
	{
		if (root != NULL)
		{
			cout << root->key() << " ";
			preorder(root->left());
			preorder(root->right());
		}
	}

	void infixorder(BSTNode<Key, E>* root) const//中序遍历
	{
		if (root != NULL)
		{
			infixorder(root->left());
			cout << root->key() << " ";
			infixorder(root->right());
		}
	}

	void postorder(BSTNode<Key, E>* root)//后序遍历
	{
		if (root != NULL)
		{
			postorder(root->left());
			postorder(root->right());
			cout << root->key() << " ";
		}
	}

	void BSTorder(BSTNode<Key, E>* root)//输出二叉树的顺序
	{
		if (root == NULL)
			cout << "/" << " ";
		else
		{
			cout << root->key() << " ";
			BSTorder(root->left());
			BSTorder(root->right());
		}
	}

	void leafcount(BSTNode<Key, E>* root)//输出叶子结点的值以及数量
	{
		if (root != NULL)
		{
			
			if (root->right() == NULL&&root->left() == NULL)
			{
				count++;
				cout << root->key()<<" ";
			}
			leafcount(root->left());
			leafcount(root->right());
		}
	}


	void print()
	{
		cout << "二叉树的顺序是:" << endl;
		BSTorder(root);
		cout << endl;
		cout << "前序遍历是:" << endl;
		preorder(root);
		cout << endl;
		cout << "中序遍历是:" << endl;
		infixorder(root);
		cout << endl;
		cout << "后序遍历是:" << endl;
		postorder(root);
		cout << endl;
		cout << "子节点的值是:" << endl;
		leafcount(root);
		cout << endl;
		cout << "子节点的数量是:" <<count<< endl;		
		cout << endl;
	}
};

BST.cpp(二叉树的函数实例化)

#include"BST.h"

int main()
{
	BST<int, int> bst;
		bst.insert(50,50);
		bst.insert(25,25);
		bst.insert(67,67);
		bst.insert(79,79);
		bst.insert(89,89);
		bst.insert(13,13);
		bst.insert(10,10);
		bst.insert(26,26);
		bst.insert(100,100);
		bst.insert(96,96);

	/*bst.insert(1,1);
	bst.insert(2,2);
	bst.insert(3,3);
	bst.insert(4,4);
	bst.insert(5,5);
	bst.insert(6,6);
	bst.insert(7,7);
	bst.insert(8,8);
	bst.insert(9,9);
	bst.insert(10,10); */

		bst.print();
		int a;
		while (cin >> a);
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_43667986/article/details/83962164