55 lines
1.1 KiB
C++
55 lines
1.1 KiB
C++
#include <iostream>
|
||
#include <string>
|
||
#include <vector>
|
||
#include "linkedBinaryTree.h"
|
||
using namespace std;
|
||
vector<string> m;
|
||
void func(binaryTreeNode<string>* node) {
|
||
cout << node->element;
|
||
}
|
||
|
||
int main() {
|
||
/*
|
||
* x
|
||
* + -
|
||
* a b c d
|
||
*/
|
||
//构建要从下往上构建,注意如果是 new了一个[],必须delete []而不能直接delete t[i]
|
||
auto t0 = new binaryTreeNode<string>("a");
|
||
auto t1 = new binaryTreeNode<string>("b");
|
||
auto t2 = new binaryTreeNode<string>("c");
|
||
auto t3 = new binaryTreeNode<string>("d");
|
||
auto t4 = new binaryTreeNode<string>("+", t0, t1);
|
||
auto t5 = new binaryTreeNode<string>("-", t2, t3);
|
||
auto t6 = new binaryTreeNode<string>("x", t4, t5);
|
||
linkedBinaryTree<string> tree(t6);
|
||
|
||
cout << endl;
|
||
tree.inOrder(func);
|
||
cout << endl;
|
||
|
||
tree.preOrder(func);
|
||
cout << endl;
|
||
|
||
tree.postOrder(func);
|
||
cout << endl;
|
||
|
||
tree.levelOrder(func);
|
||
cout << endl;
|
||
return 0;
|
||
}
|
||
|
||
/*
|
||
Output:
|
||
((a+b)x(c-d))
|
||
(x(+ab)(-cd))
|
||
((ab+)(cd-)x)
|
||
x+-abcd
|
||
disposing a
|
||
disposing b
|
||
disposing +
|
||
disposing c
|
||
disposing d
|
||
disposing -
|
||
disposing x
|
||
*/ |