搜索二叉树的特性:左子树比根小,右子树比根大。
如下图:
算法导论这本书的伪代码其实非常清晰,这是她比很多其他书更优秀的特点。
但是实实在在的测试程序更有利于学习。
插入只会在叶子节点发生,如插入值13的图:
删除比较麻烦,有几种情况,如下图:
下面是本章节中所有伪代码的C++程序,包括测试程序:
原创地址:http://blog.csdn.net/kenden23/article/details/15335057
void inorderTreeWalk(BinaryTree *root, void (*visit)(BinaryTree*))
{
if(!root) return;
inorderTreeWalk(root->left, visit);
visit(root);
inorderTreeWalk(root->right, visit);
}
void treePrint(BinaryTree *node)
{
cout<<node->val<<" ";
}
BinaryTree* treeSearch(BinaryTree *root, int key)
{
if(root==nullptr || key == root->val) return root;
if(key<root->val) return treeSearch(root->left, key);
else return treeSearch(root->right, key);
}
BinaryTree* iterativeTreeSearch(BinaryTree *root, int key)
{
BinaryTree *cur = root;
while (cur && cur->val != key)
{
if(key<cur->val) cur=cur->left;
else cur=cur->right;
}
return cur;
}
BinaryTree* treeMinimum(BinaryTree *root)
{
BinaryTree *cur = root;
while (cur->left)
{
cur = cur->left;
}
return cur;
}
BinaryTree* treeMaximum(BinaryTree *root)
{
BinaryTree *cur = root;
while (cur->right)
{
cur = cur->right;
}
return cur;
}
BinaryTree* treeSuccesor(BinaryTree *node)
{
if(node->right) return treeMinimum(node->right);
BinaryTree *p = node->parent;
while (p && node == p->right)
{
node = p;
p = p->parent;
}
return p;
}
void treeInsert(BinaryTree *root, BinaryTree *node)
{
BinaryTree *pre = nullptr;
BinaryTree *cur = root;
while (cur)
{
pre = cur;
if(node->val < cur->val) cur = cur->left;
else cur = cur->right;
}
node->parent = pre;
if(!pre) root = node;
else if (node->val<pre->val)
{
pre->left = node;
}
else
{
pre->right = node;
}
}
//v查到u的位置,并处理了其v的新的parent和u的parent一样,但是没有处理孩子节点
void transplant(BinaryTree *root, BinaryTree *u, BinaryTree *v)
{
if(u->parent == nullptr) root = v;
else if (u == u->parent->left)
{
u->parent->left = v;
}
else
{
u->parent->right = v;
}
if(v) v->parent = u->parent;
}
void treeDelete(BinaryTree *root, BinaryTree *node)
{
if(!node) return;
if(!node->left) transplant(root, node, node->right);
else if (!node->right)
{
transplant(root, node, node->left);
}
else
{
BinaryTree *p = treeMinimum(node->right);
if(p->parent != node)
{
transplant(node->right, p, p->right);
p->right = node->right;
p->right->parent = p;
}
p->left = node->left;
p->left->parent = p;
}
}
测试主程序:
#include<iostream>
using namespace std;
void testSearch(BinaryTree *root, int key)
{
BinaryTree *st = treeSearch(root, key);
BinaryTree *sti = iterativeTreeSearch(root, key);
if(st == sti)
cout<<"treeSearch and IterativeTreeSearch produce the same result. Correct!\n";
if(st->parent)
cout<<"Search parent: "<<st->parent->val<<endl;
else cout<<"No parent."<<endl;
cout<<"Search value is: "<<st->val<<"\n";
if(st->left)
cout<<"Search left: "<<st->left->val<<"\n";
else cout<<"No left child."<<endl;
if(st->right)
cout<<"Search right: "<<st->right->val<<"\n";
else cout<<"No right child."<<endl;
cout<<endl;
}
int main()
{
BinaryTree t1(15);
BinaryTree t2(23);
BinaryTree t3(13);
BinaryTree t4(43);
BinaryTree t5(25);
BinaryTree t6(46);
BinaryTree t7(37);
BinaryTree t8(28);
BinaryTree t9(19);
BinaryTree *root = new BinaryTree(30);
treeInsert(root, &t2);
treeInsert(root, &t3);
treeInsert(root, &t4);
treeInsert(root, &t5);
treeInsert(root, &t6);
treeInsert(root, &t7);
treeInsert(root, &t8);
treeInsert(root, &t9);
cout<<"Print and In order test:"<<endl;
inorderTreeWalk(root,treePrint);
cout<<endl<<endl;
cout<<"Search test:\n";
testSearch(root, 13);
cout<<"Tree minimum test:\n";
cout<<treeMinimum(root)->val<<endl;
cout<<"Tree maximum test:\n";
cout<<treeMaximum(root)->val<<endl;
cout<<"Tree succesor test:\n";
cout<<t5.val<<"'s succesor is: ";
cout<<treeSuccesor(&t5)->val<<endl;
cout<<"Tree insert test:\n";
BinaryTree ti(18);
treeInsert(root, &ti);
inorderTreeWalk(root,treePrint);
cout<<endl;
cout<<"Tree delete test:\n";
treeDelete(root, &t5);
inorderTreeWalk(root,treePrint);
cout<<endl;
delete root;
system("pause");
return 0;
}
然后是测试结果:
可以看到所有功能都实现了。
分享到:
相关推荐
二维矩形装箱算法--二叉树--java实现.rar
算法-理论基础- 查找- 平衡二叉树(包含源程序).rar
多个车子,N个箱子,用二维矩形方式进行装车。采用二叉树实现。java
包含排序,链表,图,队列,二叉树算法c++实现,这些c++是自己写的都能运行,另外还有一些c的算法实现
数据结构与算法(C#实现)系列---二叉树.doc文档中有代码
/*选择二叉链式存储结构作为二叉树的存储结构,设计一个程序实现二叉树的基本操作(包括建立、输出、前序遍历、中序遍历、后序遍历、求树高、统计叶子总数等) 【实验内容】 必做内容 程序的菜单功能项如下: 1----...
算法-理论基础- 二叉树- 二叉树的遍历(包含源程序).rar
包括了二叉树的各种递归与非递归的遍历算法 还可对二叉树所有结点求和
#include using namespace std; template<class Type> class BinaryTree; template<class Type> class BinTreeNode { friend class BinaryTree; private: BinTreeNode<Type> *leftChild,*rightChild;...
C++ 二叉树 C++ 二叉树 C++ 二叉树 C++ 二叉树
C++二叉树源代码程序,有调试界面~~~
二叉树操作C++实现 二叉树操作C++实现 二叉树操作C++实现 二叉树操作C++实现 二叉树操作C++实现
二叉树 常用算法 遍历 插入 删除 c++
二叉树的C++算法的源代码,希望对学习编程的同学有所帮助,二叉树的实现
数据结构课程设计-平衡二叉树的操作!本人以前做的就是这个,非常不错的,不过就只有代码哦!
数据结构课程设计--平和二叉树(c++编写) 内有源文件 可执行文件 实验报告
算法-理论基础- 二叉树- 线索链表(包含源程序).rar
算法-理论基础- 二叉树- 三叉链表(包含源程序).rar
算法-理论基础- 二叉树- 二叉链表(包含源程序).rar
算法大全-面试题-链表-栈-二叉树-数据结构,面试利器