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

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

  /**

  * Internal method to make subtree empty.

  */

  void makeEmpty( AvlNode * & t )

  {

  if( t != NULL )

  {

  makeEmpty( t->left );

  makeEmpty( t->right );

  delete t;

  }

  t = NULL;

  }

  /**

  * Internal method to print a subtreerooted at t in sorted order.

  */

  void printTree( AvlNode *t ) const

  {

  if( t != NULL )

  {

  printTree( t->left );

  cout << t->element<< endl;

  printTree( t->right );

  }

  }

  /**

  * Internal method to clone subtree.

  */

  AvlNode * clone( AvlNode *t ) const

  {

  if( t == NULL )

  return NULL;

  else

  return new AvlNode( t->element,clone( t->left ), clone( t->right ), t->height );

  }

  // Avl manipulations

  /**

  *Return the height of node t or -1 if NULL.

  */

  int height( AvlNode *t ) const

  {

  return t == NULL ? -1 : t->height;

  }

  int max( int lhs, int rhs ) const

  {

  return lhs > rhs ? lhs : rhs;

  }

  /**

  * Rotate binary tree node with left child.

  * For AVL trees, this is a single rotationfor case 1.

  * Update heights, then set new root.

  */

  void rotateWithLeftChild( AvlNode * &k2 )

  {

  AvlNode *k1 = k2->left;

  k2->left = k1->right;

  k1->right = k2;

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

  k1->height = max( height(k1->left ), k2->height ) + 1;

  k2 = k1;

  }

  /**

  * Rotate binary tree node with rightchild.

  * For AVL trees, this is a single rotationfor case 4.

  * Update heights, then set new root.

  */

  void rotateWithRightChild( AvlNode * &k1 )

  {

  AvlNode *k2 = k1->right;

  k1->right = k2->left;

  k2->left = k1;

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

  k2->height = max( height(k2->right ), k1->height ) + 1;

  k1 = k2;

  }

  /**

  * Double rotate binary tree node: firstleft child.

  * with its right child; then node k3 withnew left child.

  * For AVL trees, this is a double rotationfor case 2.

  * Update heights, then set new root.

  */

  void doubleWithLeftChild( AvlNode * &k3 )

  {

  rotateWithRightChild( k3->left );

  rotateWithLeftChild( k3 );

  }

  /**

  * Double rotate binary tree node: firstright child.

  * with its left child; then node k1 withnew right child.

  * For AVL trees, this is a double rotationfor case 3.

  * Update heights, then set new root.

  */

  void doubleWithRightChild( AvlNode * &k1 )

  {

  rotateWithLeftChild( k1->right );

  rotateWithRightChild( k1 );

  }

  };

  #endif

上一页234下一页

视频学习

我考网版权与免责声明

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

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

最近更新

社区交流

考试问答