C語言實(shí)現(xiàn)輸入一顆二元查找樹并將該樹轉(zhuǎn)換為它的鏡像
本文實(shí)例講述了C語言實(shí)現(xiàn)輸入一顆二元查找樹并將該樹轉(zhuǎn)換為它的鏡像的方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
采用遞歸方法實(shí)現(xiàn)代碼如下:
/*
* Copyright (c) 2011 alexingcool. All Rights Reserved.
*/
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
struct Node {
Node(int i = 0, Node *l = NULL, Node *r = NULL) : item(i), left(l), right(r) {}
int item;
Node *left;
Node *right;
};
Node *Construct()
{
Node *node6 = new Node(11);
Node *node5 = new Node(9);
Node *node4 = new Node(7);
Node *node3 = new Node(5);
Node *node2 = new Node(10, node5, node6);
Node *node1 = new Node(6, node3, node4);
Node *root = new Node(8, node1, node2);
return root;
}
void Convert(Node *root)
{
if(root == NULL)
return;
Convert(root->left);
//在這里試試swap(root->left, root->right),
//看輸出結(jié)果,有利于理解二叉樹遞歸
Convert(root->right);
swap(root->left, root->right);
}
void InOrder(Node *root)
{
if(root) {
InOrder(root->left);
cout << root->item << " ";
InOrder(root->right);
}
}
void main()
{
Node *root = Construct();
InOrder(root);
cout << endl;
Convert(root);
InOrder(root);
}
希望本文所述實(shí)例對大家C程序算法設(shè)計的學(xué)習(xí)有所幫助。
相關(guān)文章
C++ vector及實(shí)現(xiàn)自定義vector以及allocator和iterator方式
這篇文章主要介紹了C++ vector及實(shí)現(xiàn)自定義vector以及allocator和iterator方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Qt編寫地圖之實(shí)現(xiàn)覆蓋物坐標(biāo)和搜索
地圖應(yīng)用中經(jīng)常會需要有覆蓋物坐標(biāo)和搜索的功能,本文將利用Qt實(shí)現(xiàn)這一功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03
C語言實(shí)現(xiàn)倉庫物資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)倉庫物資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
C語言實(shí)現(xiàn)學(xué)生打卡系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生打卡系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12
關(guān)于C++使用std::chrono獲取當(dāng)前秒級/毫秒級/微秒級/納秒級時間戳問題
這篇文章主要介紹了C++使用std::chrono獲取當(dāng)前秒級/毫秒級/微秒級/納秒級時間戳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

