Repository navigation
Expand file tree
/
Copy pathnode.cpp
More file actions
115 lines (104 loc) · 2.66 KB
/
Copy pathnode.cpp
File metadata and controls
115 lines (104 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "node.h"
#include "QDebug"
Node::Node(QObject *parent) : QObject(parent)
{
qDebug() << "no set value";
}
Node::Node(int value, QObject *parent):QObject(parent)
{
// qDebug() << "set value:" << value;
this->val = value;
this->left = nullptr;
this->right = nullptr;
// qDebug() << "set value:" << value;
}
void Node::addNode(int value)
{
if(this->val == value)
return ;
if(this->val < value){
if(this->right == nullptr){
this->right = new Node(value);
// qDebug() << "add value:" << value ;
}
else
this->right->addNode(value);
}else if(this->val > value){
if(this->left == nullptr){
this->left = new Node(value);
// qDebug() << "add value:" << value ;
}
else
this->left->addNode(value);
}
}
void Node::addNode(Node *node)
{
if(this->val == node->val)
return ;
if(this->val < node->val){
if(this->right == nullptr){
this->right = node;
// qDebug() << "add value:" << value ;
}
else
this->right->addNode(node);
}else if(this->val > node->val){
if(this->left == nullptr){
this->left = node;
// qDebug() << "add value:" << value ;
}
else
this->left->addNode(node);
}
}
void Node::preOrderTraversal()
{
// if(this->val) return;
qDebug() << this->val ;
if(this->left != nullptr)
this->left->preOrderTraversal();
if(this->right != nullptr)
this->right->preOrderTraversal();
}
void Node::deleteNode(Node* root,int value)
{
if(this->val == value){
if(this==root){
root = nullptr;
return ;
}else {
qDebug() << "deleteError";
return ;
}
}
if(this->val > value){
if(this->left->val == value){
Node* t1 = this->left->left;
Node* t2 = this->left->right;
this->left = nullptr;
root->addNode(t1);
root->addNode(t2);
}else {
this->left->deleteNode(root,value);
}
}else {
if(this->right->val == value){
Node* t1 = this->right->left;
Node* t2 = this->right->right;
this->right = nullptr;
root->addNode(t1);
root->addNode(t2);
}else {
this->right->deleteNode(root,value);
}
}
}
int Node::value()
{
return this->val;
}
void Node::setValue(int value)
{
this->val = value;
}