手写数据结构:二叉树
Posted 看,未来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手写数据结构:二叉树相关的知识,希望对你有一定的参考价值。
代码部分测试,等整个系列写完再完整测试一遍
#include<iostream>
#include<vector>
using namespace std;
class TreeNode {
private:
TreeNode* left;
TreeNode* right;
int value;
public:
TreeNode(int val) {
this->value = val;
this->left = NULL;
this->right = NULL;
}
void setnode(int val) {
this->value = val;
}
void setleft(TreeNode* node) {
this->left = node;
}
void setright(TreeNode* node) {
this->right = node;
}
TreeNode* getleft(){
return this->left;
}
TreeNode* getright() {
return this->right;
}
int gevalue() {
return this->value;
}
};
class queue {
};
class BinaryTree {
private:
TreeNode* head;
public:
BinaryTree(vector<int>& node) {
this->head = NULL;
create_binary_tree(node);
}
//从一个数组中构建起二叉树,数组第一位为二叉树头
void create_binary_tree(vector<int>& node) {
for (int num : node) {
TreeNode* temp = head;
while (temp) {
if (temp->gevalue() > num) {
temp = temp->getleft();
}
else {
temp->getright();
}
}
temp = new TreeNode(num);
}
}
void insert_node(int val) {
TreeNode* temp = head;
while (temp) {
if (temp->gevalue() > val) {
temp = temp->getleft();
}
else {
temp->getright();
}
}
temp = new TreeNode(val);
}
bool search_node(int target) {
TreeNode* temp = head;
while (temp) {
if (temp->gevalue() > target) {
temp = temp->getleft();
}
else if(temp->gevalue() < target) {
temp->getright();
}
else {
return true;
}
}
return false;
}
TreeNode* Leftmost_Node(TreeNode* node) {
if (NULL == node) {
return NULL;
}
while (node->getleft()) {
node = node->getleft();
}
return node;
}
TreeNode* Rightmost_Node(TreeNode* node) {
if (NULL == node) {
return NULL;
}
while (node->getright()) {
node = node->getright();
}
return node;
}
void del_node(int target) {
TreeNode* temp = head;
TreeNode* father = NULL;
int flag = -1;
while (temp) {
if (temp->gevalue() > target) {
father = temp;
temp = temp->getleft();
flag = 0;
}
else if (temp->gevalue() < target) {
father = temp;
temp->getright();
flag = 1;
}
else {
if (0 == flag) {
if (temp->getleft() && temp->getright()) {
TreeNode* node = Rightmost_Node(temp->getleft());
father->setleft(node);
Rightmost_Node(father->getleft())->setnode(NULL);
temp->setnode(NULL);
delete Rightmost_Node(father->getleft());
delete temp;
}
else {
//pass
}
}
else if (1 == flag) {
//pass
}
}
}
}
void preorder(TreeNode* head) {
if (NULL == head)
return;
cout << head->gevalue() << endl;
preorder(head->getleft());
preorder(head->getright());
}
void midorder(TreeNode* head) {
if (NULL == head)
return;
midorder(head->getleft());
cout << head->gevalue() << endl;
midorder(head->getright());
}
void lastorder(TreeNode* head) {
if (NULL == head)
return;
lastorder(head->getleft());
lastorder(head->getright());
cout << head->gevalue() << endl;
}
void level_traversal(TreeNode* node) {
queue* q = new queue();
TreeNode* temp = NULL;
if (node) {
q.push(node);
}
while (!q.isempty()) {
temp = q.pop();
q.push(temp->getleft());
q.push(temp->getright());
cout << temp->gevalue() << endl;
}
}
};
int main() {
return 0;
}
以上是关于手写数据结构:二叉树的主要内容,如果未能解决你的问题,请参考以下文章