分享

二叉排序树的实现

 yangshiquan 2013-04-05

二叉排序树的实现

分类: 数据结构 265人阅读 评论(0) 收藏 举报
二叉排序树(Binary Sort Tree)又称二叉查找树。 
它是一棵空树,或者是具有下列性质的二叉树: 
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; 
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; 
(3)左、右子树也分别为二叉排序树;
二叉排序树是一种动态树表。

vs2008运行正确,如有误,请各位大牛指正!
实现代码如下:
  1. // MyBiSortTree.cpp : 定义控制台应用程序的入口点。   
  2. // 二叉查找树的各种操作   
  3. //实现功能:建树,查找,插入,删除,求最大值最小值,求双亲结点   
  4. //        ,获取中序遍历的前驱结点后继结点,用栈实现非递归的先序中序后序遍历算法,   
  5. #include "stdafx.h"   
  6. #include<iostream>   
  7. using namespace std;  
  8.   
  9. int a[100];//存放结点数据的数组   
  10. const int STACK_MAXSIZE = 30;  
  11. //二叉排序树的结点类型   
  12. class BSNode  
  13. {  
  14. public:  
  15.     BSNode(){};  
  16.     BSNode(int item):data(item), m_lchild(NULL), m_rchild(NULL){}  
  17.   
  18.     int data;//数据域     
  19.     BSNode *m_lchild;//左孩子   
  20.     BSNode *m_rchild;//右孩子   
  21. };  
  22.   
  23. //二叉排序树类型   
  24. class BSTree  
  25. {  
  26. public://定义接口   
  27.     BSTree();  
  28.     //建树:创建一棵结点个数为n的二叉排序树   
  29.     void CreateTree();  
  30.     //查找:在以root为根的树上查找值为item的结点,返回给Node,找到返回true,否则为false   
  31.     bool FindNode(int item);  
  32.     //插入:在以root为根的树上插入值为item的结点   
  33.     void InsertNode(int item);  
  34.     //删除:在以root为根的树上插入值为item的结点   
  35.     void DeleteNode(int item);  
  36.     //求最大值:返回以root为根的树中结点的最小值,结点返回给minNode   
  37.     int MinNode();  
  38.     //求最小值:返回以root为根的树中结点的最大值,结点返回给maxNode   
  39.     int MaxNode();  
  40.     //求双亲结点:返回值为item的结点的双亲结点   
  41.     BSNode *GetParent(int item);  
  42.     //获取中序遍历的前驱结点   
  43.     BSNode *GetInOrderPre(int item);  
  44.     //获取中序遍历的后继结点   
  45.     BSNode *GetInOrderPost(int item);  
  46.     //用栈实现非递归的先序遍历   
  47.     void PreOrderVisit();  
  48.     //用栈实现非递归的中序遍历   
  49.     void InOrderVisit();  
  50.     //用栈实现非递归的后序遍历   
  51.     void PostOrderVisit();  
  52.   
  53. private:  
  54.     BSNode *root; //树的根结点   
  55.     //建树:创建一棵结点个数为n的二叉排序树   
  56.     void Create(BSNode *&root,int *a, int n);  
  57.     //查找:在以root为根的树上查找值为item的结点,返回给Node,找到返回true,否则为false   
  58.     bool Find(BSNode *&root, BSNode *&Node, int item);  
  59.     //插入:在以root为根的树上插入值为item的结点   
  60.     void Insert(BSNode *&root, int item);  
  61.     //删除:在以root为根的树上插入值为item的结点   
  62.     void Delete(BSNode *&root, int item);  
  63.     //求最大值:返回以root为根的树中结点的最小值,结点返回给minNode   
  64.     int Min(BSNode *root, BSNode *&minNode);  
  65.     //求最小值:返回以root为根的树中结点的最大值,结点返回给maxNode   
  66.     int Max(BSNode *root, BSNode *&maxNode);  
  67.     //求双亲结点:返回值为item的结点的双亲结点   
  68.     BSNode *getParent(BSNode *root, int item);  
  69.     //获取中序遍历的前驱结点   
  70.     BSNode *getInOrderPre(BSNode *root, int item);  
  71.     //获取中序遍历的后继结点   
  72.     BSNode *getInOrderPost(BSNode *root, int item);  
  73.     //用栈实现非递归的先序遍历   
  74.     void PreOrder(BSNode *root);  
  75.     //用栈实现非递归的中序遍历   
  76.     void InOrder(BSNode *root);  
  77.     //用栈实现非递归的后序遍历   
  78.     void PostOrder(BSNode *root);  
  79. };  
  80.   
  81. BSTree::BSTree()  
  82. {  
  83.    root = NULL;  
  84. }  
  85.   
  86. //建树:创建一棵结点个数为n的二叉排序树   
  87. void BSTree::CreateTree()  
  88. {  
  89.     int n = 0;  
  90.     Create(root, a, n);  
  91. }  
  92.   
  93. //建树:创建一棵结点个数为n的二叉排序树   
  94. void BSTree::Create(BSNode *&root,int *a, int n) //待续,要利用插入算法   
  95. {  
  96.     cout << "请输入树的结点个数:" ;  
  97.     cin >> n;  
  98.     cout << endl << "请依次输入结点的数值:" << endl;       
  99.     BSNode *newNode = NULL;  
  100.     for (int i=0; i<n; i++)  
  101.     {  
  102.         cin >> a[i];  
  103.         InsertNode(a[i]);  
  104.     }  
  105. }  
  106.   
  107. //查找:在以root为根的树上查找值为item的结点,返回给Node,找到返回true,否则为false   
  108. bool BSTree::FindNode(int item)  
  109. {  
  110.     BSNode *Node = NULL;      
  111.     return Find(root, Node, item);    
  112. }  
  113.   
  114. //查找:在以root为根的树上查找值为item的结点,返回给Node,找到返回true,否则为false   
  115. bool BSTree::Find(BSNode *&root, BSNode *&findNode, int item)  
  116. {     
  117.     if (root == NULL)//树空   
  118.     {  
  119.         findNode = NULL;//没有找到结点           
  120.         return false;  
  121.     }  
  122.     else  
  123.     {  
  124.         if (root->data == item)  
  125.         {  
  126.             findNode = root;    //找到结点         
  127.             return true;  
  128.         }  
  129.         else if (root->data > item)//数值小于根结点的数值,则在左子树中递归查找   
  130.         {  
  131.             return Find(root->m_lchild, findNode, item);  
  132.         }  
  133.         else //数值大于根结点的数值,则在右子树中递归查找   
  134.         {  
  135.             return Find(root->m_rchild, findNode, item);  
  136.         }  
  137.     }     
  138. }  
  139.   
  140. //插入:在以root为根的树上插入值为item的结点   
  141. void BSTree::InsertNode(int item)  
  142. {  
  143.      Insert(root, item);  
  144. }  
  145.   
  146. //插入:在以root为根的树上插入值为item的结点   
  147. //根为空,则直接插入;根不空,则在插入之前先要查找该数值的结点是否存在   
  148. void BSTree::Insert(BSNode *&root, int item)  
  149. {     
  150.      BSNode *newNode = NULL;  
  151.      if (root == NULL)//树为空,则先建立根结点   
  152.      {        
  153.          newNode = new BSNode(item);  
  154.          root = newNode;  
  155.          cout << item << "已被插入二叉排序树中!" << endl;  
  156.      }  
  157.      else  
  158.      {  
  159.          if (FindNode(item))//如果找到该数值的结点,直接返回   
  160.          {     
  161.             cout << "数值" << item << "已经存在!" << endl;  
  162.             return;  
  163.          }  
  164.   
  165.          if (item > root->data)//当前数值比根结点大,则插入树的右子树   
  166.          {  
  167.              Insert(root->m_rchild, item);  
  168.          }  
  169.          else   
  170.          {  
  171.              Insert(root->m_lchild, item);  
  172.          }  
  173.      }  
  174. }  
  175.   
  176. //删除:在以root为根的树上删除值为item的结点   
  177. void BSTree::DeleteNode(int item)  
  178. {  
  179.      Delete(root, item);  
  180. }  
  181.   
  182. //删除:在以root为根的树上删除值为item的结点   
  183. void BSTree::Delete(BSNode *&root, int item)  
  184. {  
  185.     BSNode *pNode = NULL;  
  186.     if (root == NULL)  
  187.     {  
  188.         cout << "树空!" << endl;  
  189.         return;  
  190.     }  
  191.   
  192.     if (Find(root, pNode, item))  
  193.     {  
  194.         BSNode *pParent = NULL;  
  195.         //情况一:叶子结点         
  196.         if (pNode->m_lchild == NULL && pNode->m_rchild == NULL)//被删除结点是叶子结点   
  197.         {  
  198.             if (pNode == root)//且是根结点   
  199.             {  
  200.                 root = NULL;  
  201.             }  
  202.             else//且不是根结点   
  203.             {  
  204.                 pParent = GetParent(item);  
  205.                 if (pParent->m_lchild == pNode)//为其父亲结点的左孩子   
  206.                 {  
  207.                     pParent->m_lchild = NULL;  
  208.                 }  
  209.                 else//为其父亲结点的右孩子   
  210.                 {  
  211.                     pParent->m_rchild = NULL;  
  212.                 }  
  213.             }  
  214.         }  
  215.   
  216.         //情况二:只有一个孩子结点             
  217.         else if (pNode->m_rchild != NULL && pNode->m_lchild == NULL)//被删除结点有右孩子,无左孩子   
  218.         {  
  219.             if (pNode == root)//且是根结点   
  220.             {  
  221.                 root = pNode->m_rchild;  
  222.             }  
  223.             else//且不是根结点   
  224.             {  
  225.                  pParent = GetParent(item);//找到给结点的父亲节点   
  226.                  if (pParent->m_lchild == pNode)  
  227.                  {  
  228.                      pParent->m_lchild = pNode->m_rchild;  
  229.                  }  
  230.                  else  
  231.                  {  
  232.                      pParent->m_rchild = pNode->m_rchild;  
  233.                  }  
  234.             }             
  235.         }  
  236.         else if (pNode->m_lchild != NULL && pNode->m_rchild == NULL)//被删结点有左孩子,无右孩子   
  237.         {  
  238.             if (pNode == root)//且是根结点   
  239.             {  
  240.                 root = pNode->m_lchild;  
  241.             }  
  242.             else//且不是根结点   
  243.             {  
  244.                 pParent = GetParent(item);//找到给结点的父亲节点   
  245.                 if (pParent->m_lchild == pNode)  
  246.                 {  
  247.                     pParent->m_lchild = pNode->m_lchild;  
  248.                 }  
  249.                 else  
  250.                 {  
  251.                     pParent->m_rchild = pNode->m_lchild;  
  252.                 }                     
  253.             }             
  254.         }  
  255.         //情况三:有两个孩子结点          
  256.         else if (pNode->m_lchild != NULL && pNode->m_rchild != NULL)//被删结点有左孩子和右孩子   
  257.         {             
  258.             //解法一:找到该结点的前驱结点(右孩子必为空)               
  259.             BSNode *maxNode = NULL;  
  260.             int max = Max(pNode->m_lchild, maxNode);//找到以该结点左孩子为根的子树上最大的结点,即为其前驱结点   
  261.             BSNode *father = GetParent(max);  
  262.             pNode->data = max;//将前驱结点的值赋给该结点   
  263.             if (father != pNode)  
  264.             {  
  265.                 father->m_rchild = maxNode->m_lchild;  
  266.             }  
  267.             else  
  268.             {  
  269.                 father->m_lchild = maxNode->m_lchild;  
  270.             }  
  271.   
  272.             //解法二:找到该结点的后继结点(左子树必为空)   
  273.             //BSNode *minNode = NULL;   
  274.             //int min = Min(pNode->m_rchild, minNode);//找到以该结点右孩子为根的子树上最小的结点,即为其后继结点   
  275.             //BSNode *father = GetParent(min);   
  276.             //pNode->data = min;//将后继结点的值赋给该结点   
  277.    //         if (father != pNode)   
  278.    //         {   
  279.             //  father->m_lchild = minNode->m_rchild;   
  280.    //         }   
  281.             //else   
  282.             //{   
  283.             //  father->m_rchild = minNode->m_rchild;   
  284.             //}   
  285.   
  286.             //解法三:让该结点的左子树为其父亲结点的左子树(或右子树,取决于该结点是父亲结点的左子树还是右子树),   
  287.             //        其右子树为其前驱结点(以该结点左孩子为根的子树中最大值结点)的右子树   
  288.     //         BSNode *Father = GetParent(item);   
  289.              //if (Father == NULL)//该结点是根结点   
  290.              //{   
  291.     //             root = pNode->m_lchild;   
  292.                 // BSNode *maxNode = NULL;   
  293.                 // int max = Max(pNode->m_lchild, maxNode);   
  294.                 // maxNode->m_rchild = pNode->m_rchild;   
  295.              //}   
  296.              //else//该结点不是根结点   
  297.              //{   
  298.                 // if (Father->m_lchild == pNode)   
  299.                 // {   
  300.                 //   Father->m_lchild = pNode->m_lchild;   
  301.                 // }   
  302.                 // else   
  303.                 // {   
  304.                 //   Father->m_rchild = pNode->m_lchild;   
  305.                 // }   
  306.                 // //BSNode *maxNode = GetInOrderPre(item);   
  307.                 // BSNode *maxNode = NULL;   
  308.                 // int max = Max(pNode->m_lchild, maxNode);   
  309.                 // maxNode->m_rchild = pNode->m_rchild;   
  310.              //}               
  311.         }  
  312.         if (root == NULL)  
  313.         {  
  314.             cout << "删除结点后,二叉排序树为空!" << endl;  
  315.         }  
  316.         else  
  317.         {  
  318.             cout << "结点" << item << "删除成功!" << endl;  
  319.         }  
  320.     }  
  321.     else  
  322.     {  
  323.         cout << "数值为" << item << "的结点不存在!" << endl;  
  324.     }     
  325. }  
  326.   
  327. //求最小值:返回以root为根的树中结点的最小值,结点返回给minNode   
  328. int BSTree::MinNode()  
  329. {  
  330.     BSNode *minNode;  
  331.     return Min(root, minNode);  
  332. }  
  333.   
  334. //求最小值:返回以root为根的树中结点的最小值,结点返回给minNode   
  335. int BSTree::Min(BSNode *root, BSNode *&minNode)  
  336. {  
  337.     int min = 0;  
  338.     BSNode *p = root;  
  339.     if (p == NULL)  
  340.     {  
  341.         cout << "树空!" <<endl;  
  342.         return -1;//结果为-1表示树空,最小值不存在   
  343.     }  
  344.     else  
  345.     {  
  346.         while(p->m_lchild)  
  347.         {  
  348.            p = p->m_lchild;  
  349.         }  
  350.         minNode = p;  
  351.         return minNode->data;  
  352.     }  
  353.     return min;  
  354. }  
  355.   
  356. //求最大值:返回以root为根的树中结点的最大值,结点返回给maxNode   
  357. int BSTree::MaxNode()  
  358. {  
  359.     BSNode *maxNode = NULL;  
  360.     return Max(root, maxNode);  
  361. }  
  362.   
  363. //求最大值:返回以root为根的树中结点的最大值,结点返回给maxNode   
  364. int BSTree::Max(BSNode *root, BSNode *&maxNode)  
  365. {  
  366.     int max = 0;  
  367.     BSNode *p = root;  
  368.     if (p == NULL)  
  369.     {  
  370.         cout << "树空!" <<endl;  
  371.         return -1;//结果为-1表示树空,最大值不存在   
  372.     }  
  373.     else  
  374.     {  
  375.         while(p->m_rchild)  
  376.         {  
  377.             p = p->m_rchild;  
  378.         }  
  379.         maxNode = p;  
  380.         return maxNode->data;  
  381.     }  
  382.     return max;  
  383. }  
  384.   
  385. //求双亲结点:返回值为item的结点的双亲结点   
  386. BSNode *BSTree::GetParent(int item)  
  387. {  
  388.     return getParent(root, item);  
  389. }  
  390.   
  391. //求双亲结点:返回值为item的结点的双亲结点   
  392. BSNode *BSTree::getParent(BSNode *root, int item)  
  393. {  
  394.     BSNode *pParent = NULL;//指向双亲结点的指针   
  395.     BSNode *pNode = NULL;  
  396.     if (Find(root, pNode, item))//找到值为item的结点   
  397.     {  
  398.         if ((root == NULL) || (root == pNode))//树空,或该结点为根结点,则不存在双亲结点   
  399.         {  
  400.             pParent = NULL;           
  401.         }  
  402.         else//树不空且该结点不是根结点   
  403.         {     
  404.             if (root->m_lchild == pNode || root->m_rchild == pNode)  
  405.             {  
  406.                 pParent = root;               
  407.             }  
  408.             else if (root->data > item)  
  409.             {  
  410.                 pParent = getParent(root->m_lchild, item);                 
  411.             }  
  412.             else   
  413.             {  
  414.                 pParent = getParent(root->m_rchild, item);                 
  415.             }  
  416.         }  
  417.         return pParent;  
  418.     }  
  419.     else  
  420.     {  
  421.         cout << "不存在值为" << item << "的结点!";  
  422.         return pParent;  
  423.     }     
  424. }  
  425.   
  426. //获取中序遍历的前驱结点   
  427. BSNode *BSTree::GetInOrderPre(int item)  
  428. {  
  429.     return getInOrderPre(root, item);  
  430. }  
  431.   
  432. //获取中序遍历的前驱结点   
  433. //思想:先找到对应的结点,如果该结点的左孩子不空,找到以它的左孩子为根的子树中的最大值结点   
  434. //                        否则如果该结点的左孩子空,找到第一个有右孩子的祖先结点   
  435. BSNode *BSTree::getInOrderPre(BSNode *root, int item)  
  436. {  
  437.     BSNode *pPre = NULL;//指向该结点的前驱结点的指针   
  438.     BSNode *pNode = NULL;  
  439.     if (Find(root, pNode, item))//找到对应的结点   
  440.     {  
  441.         if (pNode->m_lchild != NULL)//该结点的左孩子不空   
  442.         {  
  443.             BSNode *maxNode = NULL;  
  444.             int max = Max(pNode->m_lchild, maxNode);//找到以它的左孩子为根的子树中的最大值结点   
  445.             pPre = maxNode;  
  446.         }  
  447.         else//该结点的左孩子为空   
  448.         {  
  449.             //找到第一个有右孩子的祖先结点   
  450.             BSNode *pParent = GetParent(item);  
  451. ;  
  452.             while(pParent->m_rchild == NULL)//右孩子为空   
  453.             {  
  454.                 pParent = GetParent(pParent->data);  
  455.             }  
  456.   
  457.             pPre = pParent;  
  458.         }  
  459.         return pPre;  
  460.     }  
  461.     else   
  462.     {  
  463.         cout << "数值为" << item << "的结点不存在!";  
  464.         return pNode;  
  465.     }     
  466. }  
  467.   
  468.   
  469. //获取中序遍历的后继结点   
  470. //思想:先找到对应的结点,如果右孩子节点非空,则找到右子树的最小值节点   
  471. //否则如果右孩子节点为空,则找到第一个有左孩子节点的其祖辈节点   
  472. BSNode *BSTree::GetInOrderPost(int item)  
  473. {  
  474.     return getInOrderPost(root, item);  
  475. }  
  476.   
  477. //获取中序遍历的后继结点   
  478. BSNode *BSTree::getInOrderPost(BSNode *root, int item)  
  479. {  
  480.     BSNode *pPost = NULL;//指向后继结点的指针   
  481.     BSNode *pNode = NULL;  
  482.     if (Find(root, pNode, item))  
  483.     {  
  484.         if (pNode->m_rchild != NULL)//该结点的右孩子不空   
  485.         {  
  486.             //找到以其右孩子为根的子树中最小值的结点   
  487.             BSNode *minNode = NULL;  
  488.             int mint = Min(pNode->m_rchild, minNode);  
  489.             pPost = minNode;  
  490.         }  
  491.         else//该结点的右孩子为空   
  492.         {  
  493.            //找到第一个有左孩子的祖先结点   
  494.             BSNode *pParent = GetParent(item);  
  495.             while(pParent->m_lchild == NULL)  
  496.             {  
  497.                 pParent = GetParent(pParent->data);  
  498.             }  
  499.   
  500.             pPost = pParent;  
  501.         }  
  502.         return pPost;                  
  503.     }  
  504.     else  
  505.     {  
  506.         cout << "数值为" << item << "的结点不存在!" << endl;  
  507.         return pPost;  
  508.     }     
  509. }  
  510.   
  511. //用栈实现非递归的先序遍历   
  512. void BSTree::PreOrderVisit()  
  513. {  
  514.     PreOrder(root);  
  515. }  
  516.   
  517. //用栈实现非递归的先序遍历   
  518. void BSTree::PreOrder(BSNode *root)  
  519. {  
  520.     //定义栈   
  521.     BSNode* stack[STACK_MAXSIZE];  
  522.     int top = 0;//top为0表示栈空,指向栈顶的下一个位置   
  523.   
  524.     BSNode *p = root;  
  525.     cout << "二叉树的先序遍历:";  
  526.     while(p || (top != 0))//指针不空或栈不空   
  527.     {  
  528.         if (p)  
  529.         {  
  530.             stack[top++] = p;//入栈   
  531.             cout << p->data << " ";//访问该结点   
  532.             p = p->m_lchild;  
  533.         }  
  534.         else  
  535.         {  
  536.             p = stack[--top];//出栈   
  537.             p = p->m_rchild;  
  538.         }  
  539.     }  
  540.     cout << endl;  
  541. }  
  542.   
  543. //用栈实现非递归的中序遍历   
  544. void BSTree::InOrderVisit()  
  545. {  
  546.    InOrder(root);  
  547. }  
  548.   
  549. //用栈实现非递归的中序遍历   
  550. void BSTree::InOrder(BSNode *root)  
  551. {  
  552.     //定义栈   
  553.     BSNode* stack[STACK_MAXSIZE];  
  554.     int top = 0;//top为0表示栈空,指向栈顶的下一个位置   
  555.   
  556.     BSNode *p = root;  
  557.     cout << "二叉树的中序遍历:";  
  558.     while(p || (top != 0))//指针不空或栈不空   
  559.     {  
  560.         if (p)  
  561.         {  
  562.             stack[top++] = p;//入栈              
  563.             p = p->m_lchild;  
  564.         }  
  565.         else  
  566.         {  
  567.             p = stack[--top];//出栈   
  568.             cout << p->data << " ";//访问该结点   
  569.             p = p->m_rchild;  
  570.         }  
  571.     }  
  572.     cout << endl;  
  573. }  
  574.   
  575. //用栈实现非递归的后序遍历,后序遍历较为复杂,需要用到两个栈   
  576. void BSTree::PostOrderVisit()  
  577. {  
  578.    PostOrder(root);  
  579. }  
  580.   
  581. //用栈实现非递归的后序遍历,后序遍历较为复杂,需要用到两个栈   
  582. void BSTree::PostOrder(BSNode *root)  
  583. {  
  584.     //定义栈   
  585.     BSNode* stack1[STACK_MAXSIZE];  
  586.     BSNode* stack2[STACK_MAXSIZE];  
  587.     int top1 = 0;//top为0表示栈空,指向栈顶的下一个位置   
  588.     int top2 = 0;  
  589.     BSNode *p = root;  
  590.   
  591.     cout << "二叉树的后序遍历:";  
  592.     while(p || (top1 != 0))//指针不空或栈不空   
  593.     {  
  594.         if (p)  
  595.         {  
  596.             stack1[top1++] = p;//入栈1   
  597.             stack2[top2++] = p;//入栈2   
  598.             p = p->m_rchild;//注意此处是指向右孩子   
  599.         }  
  600.         else  
  601.         {  
  602.             p = stack1[--top1];//栈1的栈顶元素出栈,栈2元素不出栈             
  603.             p = p->m_lchild;  
  604.         }  
  605.     }  
  606.       
  607.     while(top2 != 0)  
  608.     {  
  609.         p = stack2[--top2];//栈2栈顶元素出栈   
  610.         cout << p->data << " ";//访问该结点          
  611.     }  
  612.       
  613.     cout << endl;  
  614. }  
  615.   
  616. int _tmain(int argc, _TCHAR* argv[])  
  617. {  
  618.      BSTree tree;  
  619.      tree.CreateTree();  
  620.      tree.PreOrderVisit();  
  621.      tree.InOrderVisit();  
  622.      tree.PostOrderVisit();  
  623.      int item = 0;  
  624.      cout << "请输入要查找的结点值:";  
  625.      cin >> item;  
  626.      cout << item << "存在返回1,否则返回0.结果是:" << tree.FindNode(item) << endl;  
  627.      cout << item << "的双亲是:" ;  
  628.      if (tree.GetParent(item))  
  629.      {  
  630.          cout << tree.GetParent(item)->data << endl;  
  631.      }  
  632.      else  
  633.      {  
  634.          cout << "该结点为根结点,不存在双亲!" << endl;  
  635.      }  
  636.      cout << item << "中序遍历序列中前驱结点值是:" << tree.GetInOrderPre(item)->data << endl;  
  637.      cout << item << "中序遍历序列中后继结点值是:" << tree.GetInOrderPost(item)->data << endl;  
  638.      tree.InsertNode(100);  
  639.      tree.InsertNode(48);  
  640.      tree.DeleteNode(50);  
  641.      tree.DeleteNode(45);  
  642.      tree.DeleteNode(55);  
  643.      tree.InOrderVisit();  
  644.      cout << "二叉排序树中,最大值为:" << tree.MaxNode() << endl;  
  645.      cout << "二叉排序树中,最小值为:" << tree.MinNode() << endl;  
  646.   
  647.      system("pause");  
  648.      return 0;  
  649. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多