Hôm nay, 11 Tháng 9 2010 03:31




Tạo chủ đề mới Gửi bài trả lời  [ 1 bài viết ] 
 Cây nhị phân - Binary Tree 
Người gửi Nội dung
Thành Viên M?i
Thành Viên M?i

Ngày tham gia: 10 Tháng 6 2008 17:43
Bài viết: 28
Đã cảm ơn: lần
Được cảm ơn: lần
Giới tính: Nam
Tên thật: Ku Ti
Bài viết mới Cây nhị phân - Binary Tree
Xin chia sẻ với các bạn code mình sưu tầm, được cài đặt bằng C++


  1.  
  2.     //
  3. //  ============================================================
  4. //
  5. //          BinaryTree
  6. //
  7. //  ============================================================
  8. //
  9. //
  10. //   Copyright (C) 1992,1993,1994,1995,1996
  11. //
  12. //                Professor Kenneth I. Joy
  13. //                Computer Science Department
  14. //                University of California
  15. //                Davis, CA  95616
  16. //
  17. //   Permission is granted to use at your own risk and
  18. //   distribute this software in source and binary forms
  19. //   provided the above copyright notice and this paragraph
  20. //   are preserved on all copies.  This software is provided
  21. //   "as is" with no express or implied warranty.
  22. //
  23. //
  24. //  ============================================================
  25. //
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <stream.h>
  29. #include <strings.h>
  30. #include "BinaryTree.h"
  31. #define  TRUE  1
  32. #define  FALSE  0
  33. /*
  34.    ============================================================
  35.     CONSTRUCTOR, DESTRUCTOR
  36.    ============================================================
  37. */
  38. BinaryTree :: BinaryTree ()
  39.    {
  40.      _value = -100000.0 ;
  41.      _left_child = NULL ;
  42.      _right_child = NULL ;
  43.    }
  44. BinaryTree :: BinaryTree ( const double val,
  45.      BinaryTree *left, BinaryTree *right )
  46.    {
  47.      _value = val ;
  48.      _left_child = left ;
  49.      _right_child = right ;
  50.    }
  51. BinaryTree :: BinaryTree ( const BinaryTree& t )
  52.    {
  53.      _value = t._value ;
  54.      _left_child = _Copy ( t._left_child ) ;
  55.      _right_child = _Copy ( t._right_child ) ;
  56.    }
  57. BinaryTree :: ~BinaryTree ()
  58.    {
  59.      if ( _left_child != NULL ) delete _left_child ;
  60.      if ( _right_child != NULL ) delete _right_child ;
  61.    }
  62. /*
  63.    ============================================================
  64.     OPERATOR=
  65.    ============================================================
  66. */
  67. BinaryTree& BinaryTree :: operator= ( const BinaryTree& t )
  68.    {
  69.      if ( &t == this ) return ( *this ) ;
  70.      _value = t._value ;
  71.      _left_child = _Copy ( t._left_child ) ;
  72.      _right_child = _Copy ( t._right_child ) ;
  73.      return ( *this ) ;
  74.    }
  75. /*
  76.    =============================================================
  77.        OUTPUT
  78.    =============================================================
  79. */
  80. ostream& operator<< ( ostream& co, const BinaryTree& t )
  81.    {
  82.      co << " " << t._value ;
  83.      if ( t._left_child != NULL )
  84.   co << t._left_child ;
  85.      if ( t._right_child != NULL )
  86.   co << t._right_child ;
  87.      return co ;
  88.    }
  89. ostream& operator<< ( ostream& co, const BinaryTree *t )
  90.    {
  91.      co << *t ;
  92.      return ( co ) ;
  93.    }
  94. /*
  95.    ============================================================
  96.     COMPARISON
  97.    ============================================================
  98. */
  99. int operator== ( const BinaryTree& t1, const BinaryTree& t2 )
  100.    {
  101.      if ( t1.value() != t2.value() ) return ( FALSE ) ;
  102.      if ( ( t1.left_child() == NULL ) &&
  103.          ( t2.left_child() != NULL ) )
  104.   return ( FALSE ) ;
  105.      if ( ( t1.left_child() != NULL ) &&
  106.          ( t2.left_child() == NULL ) )
  107.   return ( FALSE ) ;
  108.      if ( ( t1.left_child() != NULL ) &&
  109.          ( t2.left_child() != NULL ) ) {
  110.   if ( *(t1.left_child()) != *(t2.left_child()) )
  111.     return ( FALSE ) ;
  112.   }
  113.      if ( ( t1.right_child() == NULL ) &&
  114.          ( t2.right_child() != NULL ) )
  115.   return ( FALSE ) ;
  116.      if ( ( t1.right_child() != NULL ) &&
  117.          ( t2.right_child() == NULL ) )
  118.   return ( FALSE ) ;
  119.      if ( ( t1.right_child() != NULL ) &&
  120.          ( t2.right_child() != NULL ) ) {
  121.   if ( *(t1.right_child()) != *(t2.right_child()) )
  122.     return ( FALSE ) ;
  123.   }
  124.      return ( TRUE ) ;
  125.    }
  126. int operator!= ( const BinaryTree& t1, const BinaryTree& t2 )
  127.    {
  128.      return ( ! ( t1 == t2 ) ) ;
  129.    }
  130. /*
  131.    ============================================================
  132.     ADD
  133.    ============================================================
  134. */
  135. void BinaryTree :: add ( const double val )
  136.    {
  137.   //  First compare against the root to see
  138.   //    If this should be placed in the left
  139.   //    subtree, or the right subtree.
  140.      if ( val < _value ) {
  141.     if ( _left_child != NULL ) {
  142.    //  Add to the left subtree
  143.     _left_child -> add ( val ) ;
  144.     }
  145.        else {
  146.    //  Create a new tree node as the left subtree
  147.     BinaryTree *t = new BinaryTree ( val, NULL, NULL ) ;
  148.     _left_child = t ;
  149.     }
  150.     }
  151.   else {
  152.     if ( _right_child != NULL ) {
  153.    //  Add to the right subtree
  154.     _right_child -> add ( val ) ;
  155.     }
  156.        else {
  157.    //  Create a new tree node as the right subtree
  158.     BinaryTree *t = new BinaryTree ( val, NULL, NULL ) ;
  159.     _right_child = t ;
  160.     }
  161.     }
  162.  
  163.    }
  164. /*
  165.    ============================================================
  166.     INORDER
  167.    ============================================================
  168. */
  169. void BinaryTree :: inorder () const
  170.    {
  171.      if ( _left_child != NULL ) _left_child -> inorder() ;
  172.      cout << " " << _value ;
  173.      if ( _right_child != NULL ) _right_child -> inorder() ;
  174.    }
  175. /*
  176.    ============================================================
  177.     PREORDER
  178.    ============================================================
  179. */
  180. void BinaryTree :: preorder () const
  181.    {
  182.      cout << " " << _value ;
  183.      if ( _left_child != NULL ) _left_child -> preorder() ;
  184.      if ( _right_child != NULL ) _right_child -> preorder() ;
  185.    }
  186. /*
  187.    ============================================================
  188.     POSTORDER
  189.    ============================================================
  190. */
  191. void BinaryTree :: postorder () const
  192.    {
  193.      if ( _left_child != NULL ) _left_child -> postorder() ;
  194.      if ( _right_child != NULL ) _right_child -> postorder() ;
  195.      cout << " " << _value ;
  196.    }
  197. /*
  198.    ============================================================
  199.     COPY
  200.    ============================================================
  201. */
  202. BinaryTree * _Copy ( BinaryTree * t )
  203.    {
  204.      if ( t == NULL ) return ( NULL ) ;
  205.      BinaryTree *root = new BinaryTree
  206.     ( t -> value(),
  207.      _Copy ( t -> left_child() ),
  208.      _Copy ( t -> right_child() ) ) ;
  209.      return ( root ) ;
  210.    }
  211.  
  212.  



14 Tháng 7 2009 18:51
Xem thông tin cá nhân
Hiển thị những bài viết cách đây:  Sắp xếp theo  
Tạo chủ đề mới Gửi bài trả lời  [ 1 bài viết ] 


Ai đang trực tuyến?

Đang xem chuyên mục này: Không có thành viên nào đang trực tuyến.1 khách.



Bạn không thể tạo chủ đề mới.
Bạn không thể trả lời bài viết.
Bạn không thể sửa những bài viết của mình.
Bạn không thể xoá những bài viết của mình.
Bạn không thể gửi tập tin đính kèm.

Tìm kiếm với từ khoá:
Chuyển đến:  
Powered by phpBB © phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums DivisionCore.
Vietnamese translation by nedka

Vui lòng ghi rõ nguồn www.thoiso.com khi phát hành lại thông tin từ website này.
web site hit counter