先上結果——vs2019親測有效

ps:代碼思路來源于王道考研及網上資料補充,在看王道代碼時,發現其代碼有一定誤解性,后查閱網上思路,獲得代碼靈感——創建二叉排序樹時用插入演算法時,傳入引數使用二級指標可有效使形參改變根節點(root)
網上有的思路為插入演算法里面使用查找演算法遞回來代替插入遞回
廢話不多說,直接上代碼——(代碼絕對簡練不啰嗦,不包括洗掉,可自行動手)

以下為資料結構類的定義(能實作c++代碼,c代碼不也就觸類旁通了嗎?)
//Node.h 結點類
#pragma once
class Node
{
private:
int m_data;
public:
Node *m_left_child, *m_right_child;
//Node(int data);
Node(int data, Node* left_child = nullptr, Node* right_child = nullptr);
~Node();
void set_data(int data);
int get_data() const;
};
//Node.cpp
#include "Node.h"
Node::Node(int data, Node* left_child, Node* right_child)
:m_data(data), m_left_child(left_child), m_right_child(right_child){}
Node::~Node()
{
delete m_left_child;
delete m_right_child;
m_left_child = nullptr;
m_right_child = nullptr;
}
void Node::set_data(int data)
{
m_data = data;
}
int Node::get_data() const
{
return m_data;
}
//Sort_tree.h 二叉排序樹類
#pragma once
#include "Node.h"
class Sort_tree
{
private:
Node* m_tree;
public:
Sort_tree();
Sort_tree(int *arr,int size);
~Sort_tree();
Node* get_tree() const;
Node* find_data(Node* temp, int data);
void insert_data(Node **temp,int data);
void inorder_traversal(Node* temp);
};
最后不要說復制粘貼報錯,那你倒是復制到vs2019試試啊,本來那個once是vs的宏,mian.cpp在測驗圖上,最后的最后,再次強調,vs2019親測有效,代碼為本人所寫,轉載時請標明原出處,嚴禁投自制
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268941.html
標籤:C++ 語言
上一篇:GDB怎么在成員函式里列印類(class)的成員變數?
下一篇:求四舍五入函式
