程序正文
#include <iostream> #include <cstdlib> #include "stack.h" using std::cout; using std::endl; //#define TEST_STACK #define TREE_NODE_SIZE 50 //定义树结点类型 class Node { public: int data;//数据 Node *parent;//父结点 Node *left;//左子结点 Node *right;//右子结点 //构造函数 Node():data(-1),parent(NULL),left(NULL),right(NULL){}; Node(int num):data(num),parent(NULL),left(NULL),right(NULL){}; }; //二叉排序树类定义 class Tree { public: Tree(int n[], int len);//插入num数组的前len个数据 void insertNode1(int data);//插入结点,非递归方法 void insertNode(int data);//插入结点,递归方法 Node *searchNode(int data);//查找结点 void deleteNode(int data);//删除结点以及子树 void inOrderTree();//递归中序遍历 void inOrderTreeUnRec();//非递归中序遍历 private: void insertNode(Node *current, int data);//递归插入结点 Node *searchNode(Node *current, int data);//递归查找结点 void deleteNode(Node *current);//递归删除结点 void inOrderTree(Node *current);//递归中序遍历 Node *root;//二叉排序树根结点 }; Tree::Tree(int n[], int len) { root = new Node(n[0]); for(int i = 1; i < len; i++) insertNode1(n[i]); } //插入结点,非递归方法 void Tree::insertNode1(int data) { Node *p, *par; Node *newNode = new Node(data); p = par = root; while(p != NULL) { par = p;//保留结点值 if(data > p->data) p = p->right; else if(data < p->data) p = p->left; else { delete newNode; return; } } newNode->parent = par; if(newNode->data > par->data) par->right = newNode; else par->left = newNode; } //插入结点,递归方法 void Tree::insertNode(int data) { if(root != NULL) insertNode(root, data); } void Tree::insertNode(Node *current, int data) { if(data < current->data) { if(current->left == NULL) { current->left = new Node(data); current->left->parent = current; } else insertNode(current->left, data); } else if(data > current->data) { if(current->right == NULL) { current->right = new Node(data); current->right->parent = current; } else insertNode(current->right, data); } else return ; } //非递归查找结点 Node * Tree::searchNode(int data) { if(root != NULL) return searchNode(root, data); return NULL; } Node * Tree::searchNode(Node *current, int data) { if(data > current->data) { if(current->right == NULL) { current->right = new Node(data); current->right->parent = current; return current->right; } else { current = current->right; return searchNode(current, data); } } else if(data < current->data) { if(current->left == NULL) { current->left = new Node(data); current->left->parent = current; return current->left; } else { current = current->left; return searchNode(current, data); } } else return current; } //递归中序遍历 void Tree::inOrderTree() { if(root == NULL) return ; inOrderTree(root); } void Tree::inOrderTree(Node *current) { if(current->left) inOrderTree(current->left); cout << current->data << " "; if(current->right) inOrderTree(current->right); } //非递归中序遍历 void Tree::inOrderTreeUnRec() { Node * p = root; Stack<Node *> pNodeStack(TREE_NODE_SIZE); if(p == NULL) return ; pNodeStack.push(p); while(p || pNodeStack.getLength()) { while(p->left != NULL) { pNodeStack.push(p->left); p = p->left; } while(pNodeStack.getLength()) { p = pNodeStack.pop(); cout << p->data << endl; p = p->right; if(p) { pNodeStack.push(p); break; } } } } //测试用例 int main() { int iArray[] = {3,4}; Tree tree(iArray, 2); Node * pNode; tree.insertNode1(1); tree.insertNode1(2); pNode = tree.searchNode(2); if(pNode != NULL) cout << pNode->data << endl; tree.inOrderTree(); cout << endl; tree.inOrderTreeUnRec(); system("pause"); #ifdef TEST_STACK Stack<int> iStack(10); int element = 0; cout << iStack.getLength() << endl; iStack.push(1); iStack.push(2); element = iStack.pop(); cout << element << endl; #endif }
中序遍历中使用的模版栈源代码
#ifndef __STACK__H #define __STACK__H template<typename A> class Stack { public: Stack(int elementNum = 100):elementSize(elementNum),pointer(0),usedNum(0){ element = new A[elementSize]; } A pop(); void push(A ele); int getLength(){ return usedNum; } ~Stack(){ delete element; } private: A *element; int elementSize; int pointer; int usedNum; }; template<typename A> A Stack<A>::pop() { usedNum--; return element[--pointer]; } template<typename A> void Stack<A>::push(A ele) { if(usedNum == elementSize) return ; usedNum++; element[pointer++] = ele; } #endif