📄 11_11_12.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "treelib.h"
#include "treescan.h"
#include "treeprnt.h"
#include "bstree.h"
// print the integer data in a tree node
void print(int& c)
{
cout << c << " ";
}
// RLN tree scan
template <class T>
void RLN (TreeNode<T> *t, void visit(T& item))
{
// the recursive scan terminates on a empty subtree
if (t != NULL)
{
RLN(t->Right(), visit); // descend right
RLN(t->Left(), visit); // descend left
visit(t->data); // visit the node
}
}
// RNL tree scan
template <class T>
void RNL (TreeNode<T> *t, void visit(T& item))
{
// the recursive scan terminates on a empty subtree
if (t != NULL)
{
RNL(t->Right(), visit); // descend right
visit(t->data); // visit the node
RNL(t->Left(), visit); // descend left
}
}
// NRL tree scan
template <class T>
void NRL (TreeNode<T> *t, void visit(T& item))
{
// the recursive scan terminates on a empty subtree
if (t != NULL)
{
visit(t->data); // visit the node
NRL(t->Right(), visit); // descend right
NRL(t->Left(), visit); // descend left
}
}
void main (void)
{
// data for 3 binary search trees
int s1[] = {30, 20, 10, 6, 5, 35, 56,1, 32, 40, 48};
int s2[] = {60, 25, 70, 99, 15, 3, 10, 30, 38, 59, 62, 34};
int s3[] = {30, 20, 25, 22, 24, 23};
// declare 3 binary search trees
BinSTree<int> t1, t2, t3;
int i;
// build the 3 trees
for(i=0;i < 11;i++)
t1.Insert(s1[i]);
for(i=0;i < 12;i++)
t2.Insert(s2[i]);
for(i=0;i < 6;i++)
t3.Insert(s3[i]);
// print the three trees vertically
PrintVTree(t1.GetRoot(),2, 30);
cout << endl << endl << endl << endl;
PrintVTree(t2.GetRoot(),2, 30);
cout << endl << endl << endl << endl;
PrintVTree(t3.GetRoot(),2, 30);
cout << endl << endl << endl << endl;
// scan each tree in preorder, inorder, postorder,
// RLN, RNL and NRL
cout << "Tree 1" << endl << endl;
Preorder(t1.GetRoot(),print);
cout << endl;
Inorder(t1.GetRoot(),print);
cout << endl;
Postorder(t1.GetRoot(),print);
cout << endl;
RLN(t1.GetRoot(),print);
cout << endl;
RNL(t1.GetRoot(),print);
cout << endl;
NRL(t1.GetRoot(),print);
cout << endl;
LevelScan(t1.GetRoot(),print);
cout << endl;
cout << endl << "Tree 2" << endl << endl;
Preorder(t2.GetRoot(),print);
cout << endl;
Inorder(t2.GetRoot(),print);
cout << endl;
Postorder(t2.GetRoot(),print);
cout << endl;
RLN(t2.GetRoot(),print);
cout << endl;
RNL(t2.GetRoot(),print);
cout << endl;
NRL(t2.GetRoot(),print);
cout << endl;
LevelScan(t2.GetRoot(),print);
cout << endl;
cout << endl << "Tree 3" << endl << endl;
Preorder(t3.GetRoot(),print);
cout << endl;
Inorder(t3.GetRoot(),print);
cout << endl;
Postorder(t3.GetRoot(),print);
cout << endl;
RLN(t3.GetRoot(),print);
cout << endl;
RNL(t3.GetRoot(),print);
cout << endl;
NRL(t3.GetRoot(),print);
cout << endl;
LevelScan(t3.GetRoot(),print);
cout << endl;
}
/*
<Run>
30
20 35
10 32 56
6 40
5 48
1
60
25 70
15 30 62 99
3 38
10 34 59
30
20
25
22
24
23
Tree 1
30 20 10 6 5 1 35 32 56 40 48
1 5 6 10 20 30 32 35 40 48 56
1 5 6 10 20 32 48 40 56 35 30
48 40 56 32 35 1 5 6 10 20 30
56 48 40 35 32 30 20 10 6 5 1
30 35 56 40 48 32 20 10 6 5 1
30 20 35 10 32 56 6 40 5 48 1
Tree 2
60 25 15 3 10 30 38 34 59 70 62 99
3 10 15 25 30 34 38 59 60 62 70 99
10 3 15 34 59 38 30 25 62 99 70 60
99 62 70 59 34 38 30 10 3 15 25 60
99 70 62 60 59 38 34 30 25 15 10 3
60 70 99 62 25 30 38 59 34 15 3 10
60 25 70 15 30 62 99 3 38 10 34 59
Tree 3
30 20 25 22 24 23
20 22 23 24 25 30
23 24 22 25 20 30
23 24 22 25 20 30
30 25 24 23 22 20
30 20 25 22 24 23
30 20 25 22 24 23
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -