标签:tree的学习
demo代码如下。欢迎指正与讨论。
#include <iostream> #include <queue> #include <stack> using namespace std; template <typename T> struct BinaryNode{ T elem; BinaryNode *left; BinaryNode * right; BinaryNode(T d, BinaryNode *l=NULL, BinaryNode *r=NULL):elem(d),left(l),right(r){}; }; BinaryNode<int> * constructTestingTree(){ BinaryNode<int> * leaf24 = new BinaryNode<int>(24); BinaryNode<int> * leaf23 = new BinaryNode<int>(23); BinaryNode<int> * leaf22 = new BinaryNode<int>(22); BinaryNode<int> * leaf21 = new BinaryNode<int>(21); BinaryNode<int> * leaf12 = new BinaryNode<int>(12, leaf23, leaf24); BinaryNode<int> * leaf11 = new BinaryNode<int>(11, leaf21, leaf22); BinaryNode<int> * root = new BinaryNode<int>(0, leaf11, leaf12); return root; } BinaryNode<int> * constructSearchTree(){ BinaryNode<int> * leaf24 = new BinaryNode<int>(8); BinaryNode<int> * leaf23 = new BinaryNode<int>(6); BinaryNode<int> * leaf22 = new BinaryNode<int>(4); BinaryNode<int> * leaf21 = new BinaryNode<int>(1); BinaryNode<int> * leaf12 = new BinaryNode<int>(7, leaf23, leaf24); BinaryNode<int> * leaf11 = new BinaryNode<int>(3, leaf21, leaf22); BinaryNode<int> * root = new BinaryNode<int>(5, leaf11, leaf12); return root; } //////////////// ///////basic operation void inorderTraversal_recursive(BinaryNode<int> * root){ if(root == NULL) return; inorderTraversal_recursive(root -> left); cout << root -> elem << endl; inorderTraversal_recursive(root -> right); } void preorderTraversal_recursive(BinaryNode<int> * root){ if(root == NULL) return; cout << root -> elem << endl; preorderTraversal_recursive(root -> left); preorderTraversal_recursive(root -> right); } void preorderTraversal_stack(BinaryNode<int> * root){ stack<BinaryNode<int> *> s; s.push(root); while(!s.empty() ){ BinaryNode<int> * temp = s.top(); s.pop(); cout << temp -> elem << endl; if(temp -> right != NULL) s.push(temp -> right); if(temp -> left != NULL) s.push(temp -> left); } } void postorderTraversal_recursive(BinaryNode<int> * root){ if(root == NULL) return; postorderTraversal_recursive(root -> left); postorderTraversal_recursive(root -> right); cout << root -> elem << endl; } void levelTraversal(BinaryNode<int> * root){ queue<BinaryNode<int> *> q; q.push(root); while(!q.empty()){ BinaryNode<int> * temp = q.front(); q.pop(); cout << temp -> elem << endl; if(temp -> left != NULL) q.push(temp -> left); if(temp -> right != NULL) q.push(temp -> right); } } ///////////////////////////// //////search tree BinaryNode<int> * search(BinaryNode<int> * root, int key){ if(root == NULL) return NULL; else if(root -> elem == key) return root; else{ if(root -> left != NULL && root -> elem > key) return search(root -> left, key); else if(root -> right != NULL && root -> elem < key) return search(root -> right, key); else return NULL; } } void insert(BinaryNode<int> * & root, int key){ if(root == NULL){ root = new BinaryNode<int>(key); return; } else{ if(key > root -> elem) insert(root->right, key); else if(key < root -> elem) insert(root->left, key); } } BinaryNode<int> * largest(BinaryNode<int> * &root){ if(root -> right == NULL) return root; else largest(root -> right); } BinaryNode<int> * largestFather(BinaryNode<int> * &root){ if(root -> right -> right == NULL) return root; else largest(root -> right); } void deleteThis(BinaryNode<int> * & root){ if(root -> left == NULL && root -> right == NULL){ //delete root; root = NULL; } else if(root -> left == NULL && root -> right != NULL ) root = root -> right; else if(root -> left != NULL && root -> right == NULL) root = root -> left; else{ BinaryNode<int> * large = largest(root -> left); BinaryNode<int> * largeFather = largestFather(root -> left); root -> elem = large -> elem; deleteThis(large); } } bool deletion(BinaryNode<int> * & root, int key){ if(root == NULL) return 0; if(root -> elem > key) return deletion(root -> left, key); else if(root -> elem < key) return deletion(root -> right, key); else if(root -> elem == key){ deleteThis(root); return 1; } } int main(){ BinaryNode<int> * root = constructSearchTree(); //////////////// /////basic operation //inorderTraversal_recursive(root); //cout << endl; //preorderTraversal_recursive(root); //cout << endl; //preorderTraversal_stack(root); //cout << endl; //postorderTraversal_recursive(root); //cout << endl; //levelTraversal(root); //cout << endl; ////////////////// ////////search tree //BinaryNode<int> * node = search(root, 7); //levelTraversal(node); insert(root, 2); //levelTraversal(root); cout << deletion(root, 2) << endl << endl; cout << deletion(root, 5) << endl << endl; levelTraversal(root); return 0; }
标签:tree的学习
原文地址:http://nocodes.blog.51cto.com/9045813/1426567