【数据结构】 树

#include<iostream>
#include<exception>
using namespace std;

template<class T>
class binaryTree {
public:
    virtual void clear() = 0;
    virtual bool empty()const = 0;
    virtual int height()const = 0;
    virtual int size()const = 0;
    virtual void preOrderTraverse()const = 0;
    virtual void inOrderTraverse()const = 0;
    virtual void postOrderTraverse()const = 0;
    virtual void levelOrderTraverse()const = 0;
    virtual ~binaryTree();
};


template<class T>
class BinaryLinkList:public binaryTree<T> {
private:
    struct TreeNode {
        T val;
        TreeNode* left;
        TreeNode* right;
        TreeNode():left(nullptr), right(nullptr) {}
        TreeNode(const T& x, TreeNode* l = nullptr, TreeNode* r = nullptr) : val(x), left(l), right(r) {}   
        ~TreeNode() {}
    };
    Node* root;
    void clear(TreeNode* t)const;
    int size(TreeNode* t)const;
    int height(TreeNode* t)const;
    int leafNum(TreeNode* t)const;
    void preOrder(TreeNode* t)const;
    void inOrder(TreeNode* t)const;
    void postOrder(TreeNode* t)const;
public:
    BinaryLinkList():root(nullptr) {}
    ~BinaryLinkList(){ clear(); }
    bool empty()const { return root == nullptr; }
    void clear() {
        if (root) clear(root);
        root = nullptr;
    }
    int size()const { return size(root); }
    int leafNum()const { return leafNum(root); }
    void preOrderTraverse() const { if (root) preOrder(root); }
    void inOrderTraverse() const { if (root) preOrder(root); }
    void postOrderTraverse() const { if (root) preOrder(root); }
    void levelOrderTraverse() const;
    void preOrderWithStack() const;
    void inOrderWithStack() const;
    void postOrderWithStack() const;
};

template<class T>
int BinaryLinkList<T>::size(TreeNode* t)const {
    if (t == nullptr) return 0;
    return 1 + size(t->left) + size(t->right);
}

template<class T>
int BinaryLinkList<T>::height(TreeNode* t)const {
    if (t == 0) return 0;
    return 1 + (height(t->left) > height(t->right)) ? height(t->left) : height(t->right);
}

template<class T>
int BinaryLinkList<T>::leafNum(TreeNode* t)const {
    if (t == nullptr) {  
        return 0;
    } else if ((t->left == nullptr) && (t->right == nullptr)) { 
        return 1;
    } else {          
        return leafNum(t->left) + leaf(t->right);
    }
}


template<class T>
void BinaryLinkList<T>::preOrder(TreeNode* t)const {
    if (t == nullptr) return;

    cout << t->val << " ";
    preOrder(t->left);
    preOrder(t->right); 
}

template<class T>
void BinaryLinkList<T>::inOrder(TreeNode* t)const {
    if (t == nullptr) return;

    preOrder(t->left);
    cout << t->val << " ";
    preOrder(t->right); 
}

template<class T>
void BinaryLinkList<T>::postOrder(TreeNode* t)const {
    if (t == nullptr) return;

    preOrder(t->left);
    preOrder(t->right); 
    cout << t->val << " ";
}

template <class T>
void BinaryLinkList<T>::levelOrderTraverse() const {
	queue<Node* > que;
	Node *p = root;
	if (p) que.push(p);
	while (!que.empty()) {
		p = que.front();
		que.pop();
		cout << p -> data << ' ';
		if (p -> left != NULL)
			que.push(p -> left);
		if (p -> right != NULL)
			que.push(p -> rigth);
	}
}

template<class T>
void BinaryLinkList<T>::preOrderWithStack()const {
    if (root == nullptr) return;
    
    stack<TreeNode*>st;
    TreeNode* cur = root;
    st.push(cur);
    while (!empty()) {
        cur = st.pop();

        cout << cur->val << " ";
        if (cur->right) st.push(cur->right);
        if (cur->left)  st.push(cur->left);
    }
}

template<class T>
void BinaryLinkList<T>::inOrderWithStack()const {
     stack<Node* > s;
     Node *p = root;
     while (!s.empty() || p) {
         if (p) {
             s.push();
             p = p -> left;
         } else {
             p = s.top();
             s.pop();
             cout << p -> data << ' ';
             p = p -> right; 
         }
     } 
}

template <class T>
void BinaryLinkList<T>::postOrderWithStack()const {
	enum ChildType {Left,Right};
	struct StackElem {
		Node *pointer;
		ChildType flag;
	}; 
 	StackElem elem;
	stack<StackElem> S;
	Node *p = root;
	while (!S.empty() || p) {
		while (p != NULL) {
			elem.pointer = p;
			elem.flag = Left;
			S.push(elem);
			p = p -> left;
		}
		elem = S.top();
		S.pop();
		p = elem.pointer;	
		if (elem.flag == Left) {
			elem.flag = Right;
			S.push(elem);
			p = p -> right;
		} else { 
			cout << p -> data << ' ';
			p = NULL;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值