二叉樹的鏡像

題目:完成一個函數,輸入一棵二叉樹,該函數輸出它的鏡像。ios

爲了可以造成直觀的印象,能夠使用圖例進行分析:函數

分析第一棵與最後一棵樹的特色,可以總結出求鏡像的步驟。這兩棵樹的根節點相同,但他們的左右兩個子節點交換了位置。所以,先在樹中交換根節點的兩個子節點,如上圖第二棵樹,交換根節點的兩個子節點以後,值爲 10,6 的節點的子節點仍然保持不變,所以,還須要交換這兩個節點的左右子節點。交換以後分別爲上圖的第三棵 和第四棵樹。作完這兩次交換以後,已經遍歷完全部的非葉子節點。此時變換以後的樹恰好就是原始樹的鏡像。測試

    總結上述過程,得出求一棵樹的鏡像的過程: 先前序遍歷這棵樹的每一個節點,若是遍歷到的節點有子節點,就交換它的兩個子節點,當交換完全部非葉子結點的左右子節點以後,就獲得了樹的鏡像。spa

//二叉樹的鏡像
#include<iostream>
using namespace std;
指針

typedef int ElemType;
typedef struct TNode
{
 ElemType value;
 TNode *LeftChild;
 TNode *RightChild;
}TreeNode, *BinaryTree;
遞歸

//二叉查找樹
TreeNode* InsertBinaryTree(BinaryTree T, ElemType e)
{
 if(T == NULL)
 {
  T = new TreeNode();
  T->value = e;
  T->LeftChild = NULL;
  T->RightChild = NULL;
 }
 else if(T->value > e)
  T->LeftChild = InsertBinaryTree(T->LeftChild, e);
 else if (T->value < e)
  T->RightChild = InsertBinaryTree(T->RightChild, e);
 return T;
}
ci

TreeNode* BinaryTreeNode(ElemType e)
{
 TreeNode *T = new TNode();
 T->value = e;
 T->LeftChild = NULL;
 T->RightChild = NULL;
 return T;
}
io

void ConnectTreeNode(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight)
{
 if(pParent == NULL)
  return;
 pParent->LeftChild = pLeft;
 pParent->RightChild = pRight;
}
stream

void MirrorRecursively(BinaryTree T)
{
 if(T == NULL)
  return;
 if(T->LeftChild == NULL && T->RightChild == NULL)
  return;
 TreeNode *pNode = T->LeftChild;   //若是T 的左右孩子非空,則交換左右孩子
 T->LeftChild = T->RightChild;
 T->RightChild = pNode;
 if(T->LeftChild)     //T 的左子樹非空,遞歸交換左子樹的左右孩子
  MirrorRecursively(T->LeftChild);
 if(T->RightChild)
  MirrorRecursively(T->RightChild);
}
二叉樹

void PreOrderTree(BinaryTree T)
{
 if(T == NULL)
  return;
 cout << T->value << " ";
 if(T->LeftChild != NULL)
  PreOrderTree(T->LeftChild);
 if(T->RightChild != NULL)
  PreOrderTree(T->RightChild);
}

void DestroyTree(BinaryTree T)
{
 if(T != NULL)
 {
  TreeNode *pLeft = T->LeftChild;
  TreeNode *pRight = T->RightChild;

  delete T;
  T = NULL;

  DestroyTree(pLeft);
  DestroyTree(pRight);
 }
}

//測試用例 Test1():一棵普通的二叉樹;Test2():只有一個根節點的二叉樹;Test3():空樹(即只有一個空指針)
void Test1()
{
 TreeNode *Node1 = BinaryTreeNode(8);
 TreeNode *Node2 = BinaryTreeNode(6);
 TreeNode *Node3 = BinaryTreeNode(10);
 TreeNode *Node4 = BinaryTreeNode(5);
 TreeNode *Node5 = BinaryTreeNode(7);
 TreeNode *Node6 = BinaryTreeNode(9);
 TreeNode *Node7 = BinaryTreeNode(11);
 
 ConnectTreeNode(Node1, Node2, Node3);
 ConnectTreeNode(Node2, Node4, Node5);
 ConnectTreeNode(Node3, Node6, Node7);

 cout << "樹的前序遍歷爲:";
 PreOrderTree(Node1);
 cout << endl;

 MirrorRecursively(Node1);
 cout << "樹的鏡像的前序遍歷爲:";
 PreOrderTree(Node1);
 cout << endl;
}

void Test2()
{
 TreeNode *T = BinaryTreeNode(8);
 
 ConnectTreeNode(T, NULL, NULL);

 cout << "樹的前序遍歷爲:";
 PreOrderTree(T);
 cout << endl;

 MirrorRecursively(T);
 cout << "樹的鏡像的前序遍歷爲:";
 PreOrderTree(T);
 cout << endl;
}

void Test3()
{
 TreeNode *T = NULL;
 MirrorRecursively(T);
}

void Test4()
{
 TreeNode *T = NULL;
 int data;
 while(cin >> data)
 {
  T = InsertBinaryTree(T, data);
 }

 cout << "樹的前序遍歷爲:";
 PreOrderTree(T);
 cout << endl;

 MirrorRecursively(T);
 cout << "樹的鏡像的前序遍歷爲:";
 PreOrderTree(T);
 cout << endl;
}

int main()
{
 Test1();
 Test2();
 Test3();
 Test4();

 system("pause"); return 0;}

相關文章
相關標籤/搜索