二叉树重建
根据二叉树的先序遍历和中序遍历重建二叉树。
1 #include <iostream> 2 3 #include <iomanip> 4 5 using namespace std; 6 7 template<typename T> 8 struct TreeNode 9 { 10 T m_nData; 11 12 TreeNode<T>* m_pLeft; 13 14 TreeNode<T>* m_pRight; 15 16 }; 17 18 19 template<typename T> 20 TreeNode<T>* ConstructTree(const T* pPreTraval, const T* pInTraval, int vTreeNodeNum) 21 { 22 TreeNode<T> *pTreeNode =NULL; 23 24 if (vTreeNodeNum > 0) 25 { 26 pTreeNode = new TreeNode<T>; 27 28 pTreeNode->m_nData = pPreTraval[0]; 29 30 pTreeNode->m_pLeft = pTreeNode->m_pRight = NULL; 31 32 const T * pRoot = pInTraval; 33 34 const T * pLeftInTraval = NULL; 35 36 const T * pRightInTraval =NULL; 37 38 for (; pRoot < pInTraval + vTreeNodeNum; pRoot++) 39 { 40 if (*pRoot == pPreTraval[0]) 41 { 42 pLeftInTraval = pRoot - 1; 43 44 pRightInTraval = pRoot + 1; 45 46 break; 47 } 48 } 49 50 if (pRoot < pInTraval + vTreeNodeNum) 51 { 52 int vLeftTreeNodeNum = pLeftInTraval - pInTraval + 1; 53 54 pTreeNode->m_pLeft = ConstructTree(pPreTraval + 1, pInTraval, vLeftTreeNodeNum); 55 56 pTreeNode->m_pRight = ConstructTree(pPreTraval + vLeftTreeNodeNum + 1, pRightInTraval, vTreeNodeNum - vLeftTreeNodeNum -1); 57 } 58 else 59 { 60 cout << "Invalid input" <<endl; 61 62 delete pTreeNode; 63 64 pTreeNode = NULL; 65 } 66 } 67 68 return pTreeNode; 69 } 70 71 template<typename T> 72 void PreTraval(TreeNode<T>* Tree) 73 { 74 if (Tree) 75 { 76 cout << setw(5) << Tree->m_nData; 77 78 if (Tree->m_pLeft) 79 { 80 PreTraval(Tree->m_pLeft); 81 } 82 83 if (Tree->m_pRight) 84 { 85 PreTraval(Tree->m_pRight); 86 } 87 } 88 } 89 90 template<typename T> 91 void InTraval(TreeNode<T>* Tree) 92 { 93 if (Tree) 94 { 95 if (Tree->m_pLeft) 96 { 97 InTraval(Tree->m_pLeft); 98 } 99 100 cout << setw(5) << Tree->m_nData; 101 102 if (Tree->m_pRight) 103 { 104 InTraval(Tree->m_pRight); 105 } 106 } 107 } 108 template<typename T> 109 void DestroyTree(TreeNode<T>* Tree) 110 { 111 if (Tree) 112 { 113 if (Tree->m_pLeft) 114 { 115 DestroyTree(Tree->m_pLeft); 116 } 117 if (Tree->m_pRight) 118 { 119 DestroyTree(Tree->m_pRight); 120 } 121 122 delete Tree; 123 124 Tree = NULL; 125 } 126 } 127 void main() 128 { 129 const int treeNodeNum = 8; 130 131 /*int preTraval[] = {1, 2, 4, 7, 3, 5, 6, 8}; 132 133 int inTraval[] = {4, 7, 2, 1, 5, 3, 8, 6}; 134 135 TreeNode<int> * pTree = NULL; 136 137 pTree = ConstructTree(preTraval, inTraval, treeNodeNum); 138 139 PreTraval(pTree); 140 141 cout << endl; 142 143 InTraval(pTree); 144 145 cout << endl; 146 147 DestroyTree(pTree); 148 149 pTree = NULL;*/ 150 151 152 153 char preTraval[] = {'a', 'b', 'd', 'h', 'c', 'f', 'g', 'i'}; 154 155 char inTraval[] = {'d', 'h', 'b', 'a', 'f', 'c', 'i', 'g'}; 156 157 TreeNode<char> * pTree = NULL; 158 159 pTree = ConstructTree(preTraval, inTraval, treeNodeNum); 160 161 PreTraval(pTree); 162 163 cout << endl; 164 165 InTraval(pTree); 166 167 cout << endl; 168 169 DestroyTree(pTree); 170 171 pTree = NULL; 172 173 174 175 system("pause"); 176 177 return; 178 179 }
模板类版本
1 #ifndef _TREE_ 2 3 #define _TREE_ 4 5 #include <iostream> 6 7 #include <iomanip> 8 9 using namespace std; 10 11 template<typename T> 12 struct TreeNode 13 { 14 T m_nData; 15 16 TreeNode<T>* m_pLeft; 17 18 TreeNode<T>* m_pRight; 19 }; 20 21 template<typename T> 22 class Tree 23 { 24 25 public: 26 Tree() 27 { 28 m_pTree = NULL; 29 } 30 31 ~Tree() 32 { 33 DestroyTree(m_pTree); 34 } 35 36 void PreTraval(TreeNode<T> *m_pTree); 37 38 void InTraval(TreeNode<T> *m_pTree); 39 40 void DestroyTree(TreeNode<T> *m_pTree); 41 42 TreeNode<T>* GetTreeRoot(); 43 44 void CreateBinaryTree(const T* pPreTraval, const T* pInTraval, int vTreeNodeNum); 45 46 private: 47 48 TreeNode<T>* ConstructTree(const T* pPreTraval, const T* pInTraval, int vTreeNodeNum); 49 50 private: 51 52 TreeNode<T> *m_pTree; 53 54 }; 55 56 template<typename T> 57 void Tree<T>::CreateBinaryTree(const T* pPreTraval, const T* pInTraval, int vTreeNodeNum) 58 { 59 m_pTree = ConstructTree(pPreTraval, pInTraval, vTreeNodeNum); 60 } 61 62 template<typename T> 63 TreeNode<T>* Tree<T>::ConstructTree(const T* pPreTraval, const T* pInTraval, int vTreeNodeNum) 64 { 65 TreeNode<T> *pTreeNode =NULL; 66 67 if (vTreeNodeNum > 0) 68 { 69 pTreeNode = new TreeNode<T>; 70 71 pTreeNode->m_nData = pPreTraval[0]; 72 73 pTreeNode->m_pLeft = pTreeNode->m_pRight = NULL; 74 75 const T * pRoot = pInTraval; 76 77 const T * pLeftInTraval = NULL; 78 79 const T * pRightInTraval =NULL; 80 81 for (; pRoot < pInTraval + vTreeNodeNum; pRoot++) 82 { 83 if (*pRoot == pPreTraval[0]) 84 { 85 pLeftInTraval = pRoot - 1; 86 87 pRightInTraval = pRoot + 1; 88 89 break; 90 } 91 } 92 93 if (pRoot < pInTraval + vTreeNodeNum) 94 { 95 int vLeftTreeNodeNum = pLeftInTraval - pInTraval + 1; 96 97 pTreeNode->m_pLeft = ConstructTree(pPreTraval + 1, pInTraval, vLeftTreeNodeNum); 98 99 pTreeNode->m_pRight = ConstructTree(pPreTraval + vLeftTreeNodeNum + 1, pRightInTraval, vTreeNodeNum - vLeftTreeNodeNum -1); 100 } 101 else 102 { 103 cout << "Invalid input" <<endl; 104 105 delete pTreeNode; 106 107 pTreeNode = NULL; 108 } 109 } 110 111 return pTreeNode; 112 } 113 114 template<typename T> 115 void Tree<T>::PreTraval(TreeNode<T> *m_pTree) 116 { 117 if (m_pTree) 118 { 119 cout << setw(5) << m_pTree->m_nData; 120 121 if (m_pTree->m_pLeft) 122 { 123 PreTraval(m_pTree->m_pLeft); 124 } 125 126 if (m_pTree->m_pRight) 127 { 128 PreTraval(m_pTree->m_pRight); 129 } 130 } 131 } 132 133 template<typename T> 134 void Tree<T>::InTraval(TreeNode<T> *m_pTree) 135 { 136 if (m_pTree) 137 { 138 if (m_pTree->m_pLeft) 139 { 140 InTraval(m_pTree->m_pLeft); 141 } 142 143 cout << setw(5) << m_pTree->m_nData; 144 145 if (m_pTree->m_pRight) 146 { 147 InTraval(m_pTree->m_pRight); 148 } 149 } 150 } 151 152 template<typename T> 153 void Tree<T>::DestroyTree(TreeNode<T> *m_pTree) 154 { 155 if (m_pTree) 156 { 157 if (m_pTree->m_pLeft) 158 { 159 DestroyTree(m_pTree->m_pLeft); 160 } 161 if (m_pTree->m_pRight) 162 { 163 DestroyTree(m_pTree->m_pRight); 164 } 165 166 delete m_pTree; 167 168 m_pTree = NULL; 169 } 170 } 171 172 template<typename T> 173 TreeNode<T>* Tree<T>::GetTreeRoot() 174 { 175 return m_pTree; 176 } 177 178 #endif
主函数
1 #include "Tree.h" 2 3 int main() 4 { 5 const int treeNodeNum = 8; 6 7 int preTraval[] = {1, 2, 4, 7, 3, 5, 6, 8}; 8 9 int inTraval[] = {4, 7, 2, 1, 5, 3, 8, 6}; 10 11 Tree<int> binaryTree; 12 13 TreeNode<int> *binaryTreeRoot = NULL; 14 15 binaryTree.CreateBinaryTree(preTraval, inTraval,treeNodeNum); 16 17 binaryTreeRoot = binaryTree.GetTreeRoot(); 18 19 binaryTree.PreTraval(binaryTreeRoot); 20 21 cout << endl; 22 23 binaryTree.InTraval(binaryTreeRoot); 24 25 cout << endl; 26 27 system("pause"); 28 29 return 0; 30 31 }