</pre><span style="white-space:pre"></span><span style="white-space:pre"></span>最坏运行时间<span style="white-space:pre"> </span><span style="white-space:pre"></span><span style="white-space:pre"> </span>完全二叉树 O_( lgn)<span style="white-space:pre"></span><span style="white-space:pre"> </span>线性链<span style="white-space:pre"> </span> O_(n) <span style="white-space:pre"></span><span style="white-space:pre"> </span>随机构造 O( lgn) 平均时间O_(lgn)二叉搜索树性质<span style="white-space:pre"></span><span style="white-space:pre"> </span>左子树小于双亲,右子树大于双亲<span style="white-space:pre"></span><p><span style="white-space:pre"> </span>二叉搜索树的基本操作所花费的时间与这棵树的高度成正比。</p><p>遍历</p><span style="white-space:pre"></span><span style="white-space:pre"> </span>中序遍历<span style="white-space:pre"> </span>O_(n)<span style="white-space:pre"></span><span style="white-space:pre"> </span>子树根在左右之间<span style="white-space:pre"></span><span style="white-space:pre"> </span>伪代码:<span style="white-space:pre"></span><span style="white-space:pre"> </span>INORDER-TREE-WALK(x)<span style="white-space:pre"></span><span style="white-space:pre"> </span>if x!=NIL<span style="white-space:pre"></span><span style="white-space:pre"> </span>INORDER-TREE-WALK(x.left)<span style="white-space:pre"></span><span style="white-space:pre"> </span>INORDER-TREE-WALK(x.right)<span style="white-space:pre"></span><span style="white-space:pre"> </span>先序遍历<span style="white-space:pre"></span><span style="white-space:pre"> </span>子树根在左右之前<span style="white-space:pre"></span><span style="white-space:pre"> </span>后序遍历<span style="white-space:pre"></span><p><span style="white-space:pre"> </span>子树根在左右之后</p><p></p><p>查找</p><p><span style="white-space:pre"> </span>给定关键字</p><p><span style="white-space:pre"> </span>最大关键字</p><p><span style="white-space:pre"> </span>最小关键字</p><p><span style="white-space:pre"> </span>前驱</p><p><span style="white-space:pre"> </span>后驱</p><p></p><p>插入和删除</p><p><span style="white-space:pre"> </span>插入</p><p><span style="white-space:pre"> </span>删除</p><p><span style="white-space:pre"> </span>4种情况:</p><p><span style="white-space:pre"> </span>1, z没有左孩子,用右孩子替换z</p><p><span style="white-space:pre"> </span>2, z仅有左孩子,用左孩子替换z</p><p><span style="white-space:pre"> </span>3, 有两个孩子,查找后继y,y在z的右子树中,并没有左孩子。</p><p><span style="white-space:pre"> </span>1)如果y为z的右孩子,用y替换z。</p><p><span style="white-space:pre"> </span>2)否则,先用y的右孩子替换y,再用y替换z</p>1,11,2<p><span style="white-space:pre"></span></p><p><span style="white-space:pre">1.3</span></p><p><span style="white-space:pre"><span style="white-space:pre"></span></span></p><pre name="code" class="cpp">void Btree::inorderf(tree *t)
{
stack<tree*> s;
tree *p = t;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->left;
}
if (!s.empty())
{
p = s.top();
cout << p->data<<" ";
s.pop();
p = p->right;
}
}
}//中序遍历 ,栈
算法导论 12章 二叉搜索树
最新推荐文章于 2022-09-17 18:14:16 发布