⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wex11_19.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 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 + -