-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22b8006
commit 176f60b
Showing
26 changed files
with
369 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.