Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangao2018 committed Nov 20, 2024
1 parent 22b8006 commit 176f60b
Show file tree
Hide file tree
Showing 26 changed files with 369 additions and 1 deletion.
55 changes: 54 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,59 @@
"iostream": "cpp",
"vector": "cpp",
"list": "cpp",
"__config": "cpp"
"__config": "cpp",
"__bit_reference": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"algorithm": "cpp",
"queue": "cpp",
"deque": "cpp",
"__memory": "cpp"
}
}
70 changes: 70 additions & 0 deletions Data Structure/数/delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>

using namespace std;


struct Node
{
Node(int value): value(value), left(nullptr), right(nullptr) {}
int value;
Node *left;
Node *right;
};


// 删除的节点:
// 1. 该节点是叶子节点 ===> 直接 parent 删除即可
// 2. 该节点有一个子节点(左节点或右节点) ===> 直接加该节点挂在 Parent 节点下
// 3. 该节点有两个节点(左节点和右节点) ==> 找左节点最大的 或者 右节点最小的
void deleteNode(Node *root, int data)
{
// 先找到这个节点以及父节点
Node *node = root;
while( node != nullptr)
{
if ( node->value < data ) {
node = node->right;
} else {
node = node->left;
}
}

// 找 Node 节点
if( node == nullptr ) return;

if( node->left != nullptr && node->right != nullptr ) // 两个节点
{
// 找左边最大的
Node *leftMaxNode = node->left;
Node *pLeftMaxNode = node;
while ( leftMaxNode->right != nullptr)
{
leftMaxNode = leftMaxNode->right;
pLeftMaxNode = leftMaxNode;
}

// 现在要将左边最大的跟 Node 替换
// 1. 先将 leftMaxNode 从 pLeftMaxNode 移除
if( pLeftMaxNode->left == leftMaxNode ) {
pLeftMaxNode->left = nullptr;
} else if( pLeftMaxNode->right == leftMaxNode ) {
pLeftMaxNode->right = nullptr;
}

}
else if( node->left != nullptr ) // 只有一个左节点
{
if( )
}




}


int main(int argc, char const *argv[])
{

return 0;
}
File renamed without changes.
130 changes: 130 additions & 0 deletions Data Structure/数/insert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <iostream>
#include <string>
#include <queue>

using namespace std;

// 二叉树的插入基本都是放在叶子节点上

struct Node
{
Node(int value): value(value), left(nullptr), right(nullptr) {}
int value;
Node *left;
Node *right;
};

void preOrderPrint(Node *node)
{
if( node == NULL )
return;

std::cout << node->value << " ";

preOrderPrint(node->left);
preOrderPrint(node->right);
}


void insertIgnoreSame1(Node *node, Node *insert)
{
if( node->value == insert->value )
return; // Tree already has the value
else if( node->value < insert->value ) // 当前 Node 小于 insert, 需要放在右边
{
if( node->right ==nullptr )
node->right = insert;
else
insertIgnoreSame1(node->right, insert);

return;
}
else
{
if( node->left ==nullptr )
node->left = insert;
else
insertIgnoreSame1(node->left, insert);

return;
}
}

void insertIgnoreSame2(Node *node, Node *insert)
{
Node *current = node;
while( current != nullptr )
{
if( current->value < insert->value )
{
if( current->right == nullptr )
{
current->right = insert;
return;
}

current = current->right;
}
else if ( current->value > insert->value )
{
if( current->left == nullptr )
{
current->left = insert;
return;
}

current = current->left;
}
else // equal, ignore
{

}
}
}


int main(int argc, char const *argv[])
{
Node n33(33);
Node n17(17);
Node n50(50);
Node n13(13);
Node n18(18);
Node n34(34);
Node n58(58);
Node n16(16);
Node n25(25);
Node n51(51);
Node n66(66);
Node n19(19);
Node n27(27);

n33.left = &n17;
n33.right = &n50;

n17.left = &n13;
n17.right = &n18;

n50.left = &n34;
n50.right = &n58;

n13.right = &n16;

n18.right = &n25;

n58.left = &n51;
n58.right = &n66;

n25.left = &n19;
n25.right = &n27;

// preOrderPrint(&n33);

// Node *result = search(&n33, 19);
// if( result != nullptr )
// {
// std::cout << (*(result)).value << std::endl;
// }

return 0;
}
115 changes: 115 additions & 0 deletions Data Structure/数/search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <iostream>
#include <string>
#include <queue>

using namespace std;

struct Node
{
Node(int value): value(value), left(nullptr), right(nullptr) {}
int value;
Node *left;
Node *right;
};

void preOrderPrint(Node *node)
{
if( node == NULL )
return;

std::cout << node->value << " ";

preOrderPrint(node->left);
preOrderPrint(node->right);
}

Node *search(Node *from, int value)
{
std::cout << "current value is " << from->value << std::endl;
if( from->value == value)
{
std::cout <<"Equal, return" << std::endl;
return( from );
}

if( from->value < value )
{
std::cout << "right " << std::endl;
return( search(from->right, value ));
}

std::cout << "left " << std::endl;
return( search(from->left, value) );
}

Node *search2(Node *root, int value)
{
Node *current = root;
while( current != nullptr )
{
if( current->value == value )
return current;
else if( current->value < value)
current = current->right;
else if( current->value > value)
current = current->left;
}

return( nullptr );
}


int main(int argc, char const *argv[])
{
Node n33(33);
Node n17(17);
Node n50(50);
Node n13(13);
Node n18(18);
Node n34(34);
Node n58(58);
Node n16(16);
Node n25(25);
Node n51(51);
Node n66(66);
Node n19(19);
Node n27(27);

n33.left = &n17;
n33.right = &n50;

n17.left = &n13;
n17.right = &n18;

n50.left = &n34;
n50.right = &n58;

n13.right = &n16;

n18.right = &n25;

n58.left = &n51;
n58.right = &n66;

n25.left = &n19;
n25.right = &n27;

// preOrderPrint(&n33);

// Node *result = search(&n33, 19);
// if( result != nullptr )
// {
// std::cout << (*(result)).value << std::endl;
// }


Node *result = search2(&n33, 19);
if( result != nullptr )
{
std::cout << (*(result)).value << std::endl;
}



return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 176f60b

Please sign in to comment.