Files
Data-Structure/LinearList/BinaryTree/main.cpp
2025-08-06 16:22:28 +08:00

55 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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
*/