链表栈 一> 二叉搜索树 一> 深度优先之前序遍历

来源:互联网 发布:鞑靼牛肉 知乎 编辑:程序博客网 时间:2024/06/05 08:51

1、链表栈  linkedStack

2、二叉搜索数 BST(binary search tree)

3、深度优先 DFS(depth-first search)

  前序遍历(preorder traversal)、中序(inorder)、后序(postorder)

     广度优先 BTS (breadth-first search)


代码code:

  1、stack.h

  1 /*  2  * FILE: stack.h  3  * -------------  4  * DATA: 20170813  5  */  6   7 #ifndef STACK_H  8 #define STACK_H  9  10 #include <iostream> 11  12 // 节点 13 template <typename T> 14 struct node 15 { 16         // 数据成员 17         T data; 18         node<T> *next; 19  20         // 方法 21         node(const T& value) 22         { 23                 this->data = value; 24         }; 25 }; 26  27 // 链表栈 28 template <class T> 29 class stack 30 { 31 private: 32         node<T> *head; 33  34 public: 35         // 构造函数 constructor 36         stack() 37         { 38                 head = NULL; 39         } 40  41         //析构函数 destructor 42         ~stack() 43         {} 44  45         void push(const T& value); 46  47         // 删除栈顶节点 48         void pop(); 49  50         T& top() const 51         { 52                 if(empty()) 53                         throw "Error: the stack is empty."; 54                 return head->data; 55         } 56  57         bool empty() const 58         { 59                 return head == NULL; 60         } 61  62 }; 63  64 template <class T> 65 void stack<T>::push(const T& value) 66 { 67         node<T> *p = new node<T>(value); 68         p->next = head; 69         head = p; 70 } 71  72 template <class T> 73 void stack<T>::pop() 74 { 75         if(empty()) 76                 throw "Error: the stack is empty."; 77         node<T> *p = head; 78         head = head->next; 79         delete p; 80 } 81  82 #endif


  2、BST_pre.cpp

  1 /*   2  * FILE: BST_pre.cpp  3  * -----------------  4  * DATA: 20170813  5  *  6  */  7   8 #include "stack.h"  9  10 struct treeNode 11 { 12         int data; 13         treeNode *left; 14         treeNode *right; 15         treeNode(int value) 16         { 17                 this->data = value; 18                 this->left = NULL; 19                 this->right = NULL; 20         }; 21 }; 22  23 void pre_order(treeNode *root) 24 { 25         stack<treeNode *> s; 26         treeNode *currentNode = root; 27         while(currentNode) 28         { 29                 std::cout<< currentNode->data << "  "; 30                 if(currentNode->right) 31                         s.push(currentNode->right); 32                 if(currentNode->left) 33                         currentNode = currentNode->left; 34                 else 35                 { 36                         if(s.empty()) 37                                 currentNode = NULL; 38                         else 39                         { 40                                 currentNode = s.top(); 41                                 s.pop(); 42                         } 43                 } 44 45         } 46         std::cout<< std::endl; 47 } 48  49 void insertNode(treeNode **root, treeNode *p) 50 { 51         treeNode *temp; 52         temp = *root; 53         if(temp == NULL) 54                 *root = p; 55         else 56         { 57                 if(p->data < temp->data) 58                         insertNode(&(temp->left), p); 59                 else if(p->data >= temp->data) 60                         insertNode(&(temp->right), p); 61         } 62 } 63  64 int main(int argc, char *argv[]) 65 { 66         treeNode *root = NULL; 67         int array[] = {3,6,7,2,8,5,4,4,9}; 68         for(int i = 0;i < 9; i++) 69         { 70                 insertNode(&root, new treeNode(array[i])); 71         } 72         pre_order(root); 73         return 0; 74 }

运行Run:


原创粉丝点击