算法导论中十二章C++实现二叉查找树建立,插入,删除,遍历操作
2014-10-28 22:33
781 查看
#include <iostream>
#include <vector>
using namespace std;
/*二叉查找树结构*/
typedef struct BSTree
{
int node_value;
struct BSTree * left;
struct BSTree * right;
struct BSTree * parent;
}Tree;
Tree * root = NULL;
/*****构造二叉查找树**********************************************/
void CreateBSTree(Tree * root,int node_value);
Tree * CreateBSTree(int * array_list,int array_length);
void Print(Tree* root);
/***************************************************************/
int Minimum(Tree * p);
Tree * TreeMinimum(Tree * root);
int Maximum(Tree * p);
Tree * TreeMaximum(Tree * root);
Tree * FindNode(Tree * root,int node_value);
20000
Tree * Successor(Tree * root);
Tree * PredeSuccessor(Tree * p);
bool DeleteTreeNode(Tree * root,int node_value);
bool DeleteTreeNode(Tree * n);
/***************************************************************/
int main(int argc,char * argv[])
{
//int list[]={5,3,4,9,1,7,11};
int list[]={15,5,16,3,12,20,10,13,18,23,6,7};
root = CreateBSTree(list,12);
std::cout<<"Cearte BSTree."<<std::endl;
//Print(root);
//std::cout<<Successor(FindNode(root,4))->node_value;
//Print(root);
//std::cout<<std::endl;
//DeleteTreeNode(root,15);
//Print(root);
Tree * t = FindNode(root,18);
std::cout<<PredeSuccessor(t)->node_value;
getchar();
return 0;
}
/*找出树中的最小节点
p数的根节点
*/
int Minimum(Tree * p)
{
Tree * t = TreeMinimum(p);
if(t != NULL)
{
return t->node_value ;
}
else
{
return -1;
}
}
Tree * TreeMinimum(Tree * p)
{
if(p->left == NULL)
{
return p;
}
TreeMinimum(p->left);
}
/*找出树中的最大节点*/
int Maximum(Tree * p)
{
Tree * t = TreeMaximum(root);
if(t != NULL)
{
return t->node_value ;
}
else
{
return -1;
}
}
Tree * TreeMaximum(Tree * p)
{
if(p->right == NULL)
{
return p;
}
TreeMaximum(p->right);
}
/*假定所有的节点值都不相同,找到一个节点的指针
p树的根节点,
node_value要查找的节点的值
*/
Tree * FindNode(Tree * p,int node_value)
{
if(p == NULL)
{
return NULL;/*递归返回标志*/
}
if(p->node_value == node_value)
{
return p;
}
if(p->node_value < node_value)
{
FindNode(p->right, node_value);
}
else
{
FindNode(p->left, node_value);
}
}
/*找出一个节点的后继结点*/
Tree * Successor(Tree * p)
{
if(p == NULL)
{
return NULL;
}
if(p->right != NULL)
{
return TreeMinimum(p->right);
}
Tree * t = p->parent ;
while((t != NULL) && (p == t->right))
{
p = t;
t = t->parent ;
}
return t;
}
/*找到一个节点的前驱节点
p为节点的指针
*/
Tree * PredeSuccessor(Tree * p)
{
if(p == NULL)
{
return NULL;
}
else if(p->left != NULL)
{/*如果左子树不为空,则前驱为其左子树的最大值节点*/
return TreeMaximum(p->left);
}
else
{
Tree * t = p->parent ;
while((t != NULL) && (p == t->left))
{/*注意节点t的方向,这个与寻找后继节点相反*/
p = t;
t = t->parent;
}
return t;
}
}
/*删除一个节点
p为树根节点指针
node_value要删除的节点的值
*/
bool DeleteTreeNode(Tree * p,int node_value)
{
Tree * t = FindNode(p,node_value);
if(t == NULL)
{
return false;
}
if((t->left == NULL)&&(t->right == NULL))
{/*没有子节点*/
Tree* tmp = t;
if(tmp->parent->left == tmp)
{
tmp->parent->left = NULL;
}
else
{
tmp->parent->right = NULL;
}
delete tmp;
tmp = NULL;
}
else if((t->left == NULL)||(t->right == NULL))
{/*一个子节点*/
Tree* tmp = t;
if(tmp->parent->left == tmp)
{
tmp->parent->left = (tmp->left ==NULL)?tmp->right :tmp->left;
}
else
{
tmp->parent->right = (tmp->left ==NULL)?tmp->right :tmp->left;
}
delete tmp;
tmp = NULL;
}
else
{/*两个子节点*/
Tree* s = Successor(t);
if(s == NULL)
{
return false;
}
t->node_value = s->node_value ;
DeleteTreeNode(s);
}
}
/*删除一个节点
p为树根节点指针
*/
bool DeleteTreeNode(Tree * n)
{
if(n == NULL)
{
return NULL;
}
else if((n->left == NULL)&&(n->right == NULL))
{/*没有子节点*/
Tree* tmp = n;
if(tmp->parent->left == tmp)
{
tmp->parent->left = NULL;
}
else
{
tmp->parent->right = NULL;
}
delete tmp;
tmp = NULL;
}
else if((n->left == NULL)||(n->right == NULL))
{/*一个子节点*/
Tree* tmp = n;
if(tmp->parent->left == tmp)
{
tmp->parent->left = (tmp->left ==NULL)?tmp->right :tmp->left;
}
else
{
tmp->parent->right = (tmp->left ==NULL)?tmp->right :tmp->left;
}
delete tmp;
tmp = NULL;
}
else
{/*两个子节点*/
Tree* s = Successor(n);
if(s == NULL)
{
return false;
}
n->node_value = s->node_value ;
DeleteTreeNode(s);
}
}
/*生成二叉查找树*/
Tree * CreateBSTree(int * array_list,int array_length)
{
if(array_length <= 0)
{
return false;
}
Tree * root = NULL;
root = new BSTree();
root->left = NULL;
root->right = NULL;
root->parent = NULL;
root->node_value = array_list[0];
for(int i=1;i<array_length;i++)
{
CreateBSTree(root,array_list[i]);
}
return root;
}
void CreateBSTree(Tree * root,int node_value)
{
if(root == NULL)
{
return ;
}
if(root->node_value > node_value)
{
if(root->left == NULL)
{
Tree * node = new Tree();
node->left = NULL;
node->right = NULL;
node->node_value = node_value;
node->parent = root;
root->left = node;
}
else
{
CreateBSTree(root->left,node_value);
}
}
else
{
if(root->right == NULL)
{
Tree * node = new Tree();
node->left = NULL;
node->right = NULL;
node->node_value = node_value;
node->parent = root;
root->right = node;
}
else
{
CreateBSTree(root->right,node_value);
}
}
}
/*中序排序输出二叉查找树*/
void Print(Tree* root)
{
if(root == NULL)
{
return ;
}
Print(root->left);
std::cout<<root->node_value<<"\t";
Print(root->right);
}
#include <vector>
using namespace std;
/*二叉查找树结构*/
typedef struct BSTree
{
int node_value;
struct BSTree * left;
struct BSTree * right;
struct BSTree * parent;
}Tree;
Tree * root = NULL;
/*****构造二叉查找树**********************************************/
void CreateBSTree(Tree * root,int node_value);
Tree * CreateBSTree(int * array_list,int array_length);
void Print(Tree* root);
/***************************************************************/
int Minimum(Tree * p);
Tree * TreeMinimum(Tree * root);
int Maximum(Tree * p);
Tree * TreeMaximum(Tree * root);
Tree * FindNode(Tree * root,int node_value);
20000
Tree * Successor(Tree * root);
Tree * PredeSuccessor(Tree * p);
bool DeleteTreeNode(Tree * root,int node_value);
bool DeleteTreeNode(Tree * n);
/***************************************************************/
int main(int argc,char * argv[])
{
//int list[]={5,3,4,9,1,7,11};
int list[]={15,5,16,3,12,20,10,13,18,23,6,7};
root = CreateBSTree(list,12);
std::cout<<"Cearte BSTree."<<std::endl;
//Print(root);
//std::cout<<Successor(FindNode(root,4))->node_value;
//Print(root);
//std::cout<<std::endl;
//DeleteTreeNode(root,15);
//Print(root);
Tree * t = FindNode(root,18);
std::cout<<PredeSuccessor(t)->node_value;
getchar();
return 0;
}
/*找出树中的最小节点
p数的根节点
*/
int Minimum(Tree * p)
{
Tree * t = TreeMinimum(p);
if(t != NULL)
{
return t->node_value ;
}
else
{
return -1;
}
}
Tree * TreeMinimum(Tree * p)
{
if(p->left == NULL)
{
return p;
}
TreeMinimum(p->left);
}
/*找出树中的最大节点*/
int Maximum(Tree * p)
{
Tree * t = TreeMaximum(root);
if(t != NULL)
{
return t->node_value ;
}
else
{
return -1;
}
}
Tree * TreeMaximum(Tree * p)
{
if(p->right == NULL)
{
return p;
}
TreeMaximum(p->right);
}
/*假定所有的节点值都不相同,找到一个节点的指针
p树的根节点,
node_value要查找的节点的值
*/
Tree * FindNode(Tree * p,int node_value)
{
if(p == NULL)
{
return NULL;/*递归返回标志*/
}
if(p->node_value == node_value)
{
return p;
}
if(p->node_value < node_value)
{
FindNode(p->right, node_value);
}
else
{
FindNode(p->left, node_value);
}
}
/*找出一个节点的后继结点*/
Tree * Successor(Tree * p)
{
if(p == NULL)
{
return NULL;
}
if(p->right != NULL)
{
return TreeMinimum(p->right);
}
Tree * t = p->parent ;
while((t != NULL) && (p == t->right))
{
p = t;
t = t->parent ;
}
return t;
}
/*找到一个节点的前驱节点
p为节点的指针
*/
Tree * PredeSuccessor(Tree * p)
{
if(p == NULL)
{
return NULL;
}
else if(p->left != NULL)
{/*如果左子树不为空,则前驱为其左子树的最大值节点*/
return TreeMaximum(p->left);
}
else
{
Tree * t = p->parent ;
while((t != NULL) && (p == t->left))
{/*注意节点t的方向,这个与寻找后继节点相反*/
p = t;
t = t->parent;
}
return t;
}
}
/*删除一个节点
p为树根节点指针
node_value要删除的节点的值
*/
bool DeleteTreeNode(Tree * p,int node_value)
{
Tree * t = FindNode(p,node_value);
if(t == NULL)
{
return false;
}
if((t->left == NULL)&&(t->right == NULL))
{/*没有子节点*/
Tree* tmp = t;
if(tmp->parent->left == tmp)
{
tmp->parent->left = NULL;
}
else
{
tmp->parent->right = NULL;
}
delete tmp;
tmp = NULL;
}
else if((t->left == NULL)||(t->right == NULL))
{/*一个子节点*/
Tree* tmp = t;
if(tmp->parent->left == tmp)
{
tmp->parent->left = (tmp->left ==NULL)?tmp->right :tmp->left;
}
else
{
tmp->parent->right = (tmp->left ==NULL)?tmp->right :tmp->left;
}
delete tmp;
tmp = NULL;
}
else
{/*两个子节点*/
Tree* s = Successor(t);
if(s == NULL)
{
return false;
}
t->node_value = s->node_value ;
DeleteTreeNode(s);
}
}
/*删除一个节点
p为树根节点指针
*/
bool DeleteTreeNode(Tree * n)
{
if(n == NULL)
{
return NULL;
}
else if((n->left == NULL)&&(n->right == NULL))
{/*没有子节点*/
Tree* tmp = n;
if(tmp->parent->left == tmp)
{
tmp->parent->left = NULL;
}
else
{
tmp->parent->right = NULL;
}
delete tmp;
tmp = NULL;
}
else if((n->left == NULL)||(n->right == NULL))
{/*一个子节点*/
Tree* tmp = n;
if(tmp->parent->left == tmp)
{
tmp->parent->left = (tmp->left ==NULL)?tmp->right :tmp->left;
}
else
{
tmp->parent->right = (tmp->left ==NULL)?tmp->right :tmp->left;
}
delete tmp;
tmp = NULL;
}
else
{/*两个子节点*/
Tree* s = Successor(n);
if(s == NULL)
{
return false;
}
n->node_value = s->node_value ;
DeleteTreeNode(s);
}
}
/*生成二叉查找树*/
Tree * CreateBSTree(int * array_list,int array_length)
{
if(array_length <= 0)
{
return false;
}
Tree * root = NULL;
root = new BSTree();
root->left = NULL;
root->right = NULL;
root->parent = NULL;
root->node_value = array_list[0];
for(int i=1;i<array_length;i++)
{
CreateBSTree(root,array_list[i]);
}
return root;
}
void CreateBSTree(Tree * root,int node_value)
{
if(root == NULL)
{
return ;
}
if(root->node_value > node_value)
{
if(root->left == NULL)
{
Tree * node = new Tree();
node->left = NULL;
node->right = NULL;
node->node_value = node_value;
node->parent = root;
root->left = node;
}
else
{
CreateBSTree(root->left,node_value);
}
}
else
{
if(root->right == NULL)
{
Tree * node = new Tree();
node->left = NULL;
node->right = NULL;
node->node_value = node_value;
node->parent = root;
root->right = node;
}
else
{
CreateBSTree(root->right,node_value);
}
}
}
/*中序排序输出二叉查找树*/
void Print(Tree* root)
{
if(root == NULL)
{
return ;
}
Print(root->left);
std::cout<<root->node_value<<"\t";
Print(root->right);
}
相关文章推荐
- (1)顺序表的操作 ① 输入一组整型元素序列,建立线性表的顺序存储结构。 ② 实现该线性表的遍历。 ③ 在该顺序表中查找某一元素,查找成功显示查找元素,否则显示查找失败。 ④ 在该顺序表中删除或插入指
- 二叉查找树的插入、删除、遍历和查找等C++实现
- C++ 单链表的建立,插入数值,删除数值,排序,遍历,和分割操作
- 二叉排序树的建立、插入、删除、查找、比较、4种遍历方式的C++完整实现版
- (2)单链表的操作 ① 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数
- 二叉排序树的建立、插入、删除、查找、4种遍历 C++完整实现
- 二叉查找树的基本操作(建立,插入,删除,遍历)
- c++实现单链表创建,删除,遍历,插入,修改操作
- 二叉树建立与遍历递归操作c++实现
- 单链表的基本操作的实现(建立、插入、删除、逆序)
- C++实现最小堆及插入,调整顺序,删除堆顶元素的操作
- 单链表的插入删除操作(c++实现)
- C++实现红黑树建立,销毁,查找,插入和删除
- C++ 数据结构的单链表的建立,插入,删除操作
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现
- 转载:二叉查找树原理分析及查找、插入、删除、遍历实现
- C++单链表的操作(创建,删除,打印,遍历,插入)
- (C++版)链表(一)——实现单向链表创建、插入、删除等相关操作
- 单链表建立,插入,删除,查找,遍历操作
- 数据结构链表的操作集合(建立,遍历,插入,删除,排序,长度,空判断等)