📄 wex11_19.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "treelib.h"
#include "treeprnt.h"
// copy tree t by exchanging all left and right pointers
template <class T>
TreeNode<T> *ReverseCopy(TreeNode<T> *t)
{
// newptr1 is address of left subtree of t in the copy
// newptr2 is address of right subtree of t in the copy
TreeNode<T> *newptr1, *newptr2, *newnode;
// if tree t is empty, return
if (t == NULL)
return NULL;
// use ReverseCopy to copy the right subtree of t
if (t->Right() != NULL)
newptr1 = ReverseCopy(t->Right());
else
newptr1 = NULL;
// use ReverseCopy to copy the left subtree of t
if (t->Left() != NULL)
newptr2 = ReverseCopy(t->Left());
else
newptr2 = NULL;
// copy node t with left subtree newptr1 and right
// subtree newptr2. this exchanges the left and right
// subtrees of tree t
newnode = GetTreeNode(t->data, newptr1, newptr2);
// return the address of the new node
return newnode;
}
void main(void)
{
// pointers for original and copied tree
TreeNode<char> *root1, *root2;
// create Tree_0 and print it
MakeCharTree(root1, 0);
PrintVTree (root1, 2,18);
// copy the tree in reverse
cout << endl << endl << endl;
root2 = ReverseCopy(root1);
// print the new tree vertically
PrintVTree (root2, 2,18);
}
/*
<Run>
A
B C
D E
A
C B
E D
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -