二叉樹的實現(c++)
阿新 • • 發佈:2018-11-17
二叉樹的實現(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;
}