【數據結構】——搜索二叉樹的插入,查找和刪除(遞歸&非遞歸)
阿新 • • 發佈:2018-02-27
type 樹操作 iss OS 操作 amp 方法 查找 搜索樹
非遞歸代碼
一、搜索二叉樹的插入,查找,刪除
簡單說說搜索二叉樹概念:
二叉搜索樹又稱二叉排序樹,它或者是一棵空樹,或者是具有以下性質的二叉樹
若它的左子樹不為空,則左子樹上所有節點的值都小於根節點的值
若它的右子樹不為空,則右子樹上所有節點的值都大於根節點的值
它的左右子樹也分別為二叉搜索樹
例如:int a [] = {5,3,4,1,7,8,2,6,0,9};
二叉樹結構
typedef struct BSTreeNode { struct BSTreeNode *_left; struct BSTreeNode *_right; DataType _data; }BSTreeNode;
二叉樹節點創建
BSTreeNode *BuyTreeNode(DataType x) //創建節點
{
BSTreeNode *node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
assert(node);
node->_data = x;
node->_left = NULL;
node->_right = NULL;
return node;
}
二叉搜索樹操作:
1、搜索二叉樹的插入:在二叉搜索樹中插入新元素時,必須先檢測該元素是否在樹中已經存在。如果已經存在,則不進行插入;否則將新元素加入到搜索停止的地方。
非遞歸代碼
int BSTreeNodeInsert(BSTreeNode **pptree,DataType x) //搜索樹的插入 { BSTreeNode *parent = NULL; BSTreeNode *cur = *pptree; if (*pptree == NULL) { *pptree = BuyTreeNode(x); return 0; } while (cur) { parent = cur; if (cur->_data > x) cur = cur->_left; else if (cur->_data < x) cur = cur->_right; else return -1; } if (parent->_data > x) parent->_left = BuyTreeNode(x); else parent->_right = BuyTreeNode(x); return 0; }
遞歸代碼 :
int BSTreeNodeInsertR(BSTreeNode **tree,DataType x) //搜索樹的插入
{
if(*tree == NULL)
{
*tree = BuyTreeNode(x);
return 0;
}
if ((*tree)->_data > x)
return BSTreeNodeInsertR(&(*tree)->_left,x);
else if ((*tree)->_data < x)
return BSTreeNodeInsertR(&(*tree)->_right,x);
else
return -1;
}
2、搜索二叉樹的查找:
非遞歸代碼
BSTreeNode *BSTreeNodeFind(BSTreeNode *tree,DataType x) //查找
{
while (tree)
{
if (tree->_data == x)
return tree;
else if (tree->_data < x)
tree = tree->_right;
else
tree = tree->_left;
}
return NULL;
}
遞歸代碼
BSTreeNode *BSTreeNodeFindR(BSTreeNode *tree,DataType x) //查找
{
if (!tree)
return NULL;
if (tree->_data > x)
BSTreeNodeFindR(tree->_left,x);
else if (tree->_data < x)
BSTreeNodeFindR(tree->_right,x);
else
return tree;
}
3、搜索二叉樹的刪除:
首先查找元素是否在二叉搜索樹中,如果不存在,則返回, 否則要刪除的結點可能分下面四種情況:
a. 要刪除的結點無孩子結點
b. 要刪除的結點只有左孩子結點
c. 要刪除的結點只有右孩子結點
d. 要刪除的結點有左、右孩子結點
情況1可以歸類到2或者3
對於上述情況,相應的刪除方法如下:
a. 直接刪除該結點
b. 刪除該結點且使被刪除節點的雙親結點指向被刪除節點的左孩子結點
c. 刪除該結點且使被刪除節點的雙親結點指向被刪除結點的右孩子結點
d. 在它的右子樹中尋找中序下的第一個結點(關鍵碼最小),用它的值填補到被刪除節點中,在來處理該結點的刪除問題
代碼實現:
int BSTreeNodeDel(BSTreeNode **tree,DataType x) //刪除
{
BSTreeNode *cur = *tree;
BSTreeNode *parent = *tree;
BSTreeNode *del = NULL;
while (cur)
{
if (cur->_data > x)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_data < x)
{
parent = cur;
cur = cur->_right;
}
else
{
del = cur;
if (cur->_left == NULL) //1、左孩子為空
{
if (parent->_left == cur)
parent->_left = cur->_right;
else if (parent->_right == cur)
parent->_right = cur->_right;
else if (parent == cur) //沒有父親節點時
*tree = parent->_right;
}
else if (cur->_right == NULL) //2、右孩子為空
{
if (parent->_left == cur)
parent->_left = cur->_left;
else if (parent->_right == cur)
parent->_right = cur->_left;
else if (parent == cur) //沒有父親節點時
*tree = parent->_left;
}
else//3、左右孩子都不為空
{
BSTreeNode *sub = cur->_right;
while (sub->_left)
{
parent = sub;
sub = sub->_left;
}
del = sub;
cur->_data = sub->_data;
if (parent->_left == sub)
parent->_left = sub->_right;
else
parent->_right = sub->_right;
}
free(del);
del = NULL;
return 0;
}
}
return -1;
}
完整代碼請私信或者點此鏈接下載【數據結構】——搜索二叉樹的插入,查找和刪除(遞歸&非遞歸)
【數據結構】——搜索二叉樹的插入,查找和刪除(遞歸&非遞歸)