Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <queue>
- using namespace std;
- class TreeNode {
- public:
- TreeNode(int key) : Key(key), Left(0), Right(0) {}
- int Key;
- TreeNode* Left;
- TreeNode* Right;
- };
- class Tree {
- public:
- Tree() :Root(0) {
- }
- ~Tree() {
- DestroyTreeNode(Root);
- }
- void Insert(int n);
- void LeftStraightPrint(TreeNode* root) {
- if (root = NULL) {
- return;
- }
- cout << root->Key << endl;
- LeftStraightPrint(root->Left);
- LeftStraightPrint(root->Right);
- };
- TreeNode *get_root() {
- return Root;
- }
- private:
- static void DestroyTreeNode(TreeNode* tn) {
- if (tn) {
- DestroyTreeNode(tn->Left);
- DestroyTreeNode(tn->Right);
- delete tn;
- }
- }
- private:
- TreeNode* Root;
- };
- void Tree::Insert(int n) {
- TreeNode** cur = &Root;
- while (*cur) {
- TreeNode& tn = **cur;
- if (n < tn.Key)
- cur = &tn.Left;
- else if (n > tn.Key)
- cur = &tn.Right;
- else return;
- }
- *cur = new TreeNode(n);
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- Tree MyTree;
- int temp;
- while (!fin.eof()) {
- fin >> temp;
- MyTree.Insert(temp);
- }
- MyTree.LeftStraightPrint(MyTree.get_root());
- fin.close();
- fout.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement