2011年软考程序员考试复习笔试知识点整理(20)

来源:软件水平考试    发布时间:2012-11-05    软件水平考试视频    评论

  private:

  struct AvlNode

  {

  Comparable element;

  AvlNode *left;

  AvlNode *right;

  int height;

  AvlNode( const Comparable &theElement, AvlNode *lt,

  AvlNode *rt, int h = 0 )

  : element( theElement ), left( lt ),right( rt ), height( h ) { }

  };

  AvlNode *root;

  /**

  * Internal method to insert into asubtree.

  * x is the item to insert.

  * t is the node that roots the subtree.

  * Set the new root of the subtree.

  */

  void insert( const Comparable & x,AvlNode * & t )

  {

  if( t == NULL )

  t = new AvlNode( x, NULL, NULL );

  else if( x < t->element )

  {

  insert( x, t->left );

  if( height( t->left ) - height(t->right ) == 2 )

  if( x left->element )

  rotateWithLeftChild( t );

  else

  doubleWithLeftChild( t );

  }

  else if( t->element < x )

  {

  insert( x, t->right );

  if( height( t->right ) - height(t->left ) == 2 )

  if( t->right->element< x )

  rotateWithRightChild( t );

  else

  doubleWithRightChild( t );

  }

  t->height = max( height( t->left), height( t->right ) ) + 1;

  }

  /**

  * Internal method to find the smallestitem in a subtree t.

  * Return node containing the smallestitem.

  */

  AvlNode * findMin( AvlNode *t ) const

  {

  if( t == NULL )

  return NULL;

  if( t->left == NULL )

  return t;

  return findMin( t->left );

  }

  /**

  * Internal method to find the largest itemin a subtree t.

  * Return node containing the largest item.

  */

  AvlNode * findMax( AvlNode *t ) const

  {

  if( t != NULL )

  while( t->right != NULL )

  t = t->right;

  return t;

  }

  /**

  * Internal method to test if an item is ina subtree.

  * x is item to search for.

  * t is the node that roots the tree.

  */

  bool contains( const Comparable & x,AvlNode *t ) const

  {

  if( t == NULL )

  return false;

  else if( x < t->element )

  return contains( x, t->left );

  else if( t->element < x )

  return contains( x, t->right );

  else

  return true; // Match

  }

  /****** NONRECURSIVEVERSION*************************

  bool contains( const Comparable & x,AvlNode *t ) const

  {

  while( t != NULL )

  if( x < t->element )

  t = t->left;

  else if( t->element < x )

  t = t->right;

  else

  return true; // Match

  return false; // No match

  }

  *****************************************************/

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答