分类: 数据结构 2012-08-12 20:42 265人阅读 收藏 举报
二叉排序树(Binary Sort Tree)又称二叉查找树。
它是一棵空树,或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
二叉排序树是一种动态树表。
vs2008运行正确,如有误,请各位大牛指正! 实现代码如下:
-
-
-
-
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
-
- int a[100];
- const int STACK_MAXSIZE = 30;
-
- class BSNode
- {
- public:
- BSNode(){};
- BSNode(int item):data(item), m_lchild(NULL), m_rchild(NULL){}
-
- int data;
- BSNode *m_lchild;
- BSNode *m_rchild;
- };
-
-
- class BSTree
- {
- public:
- BSTree();
-
- void CreateTree();
-
- bool FindNode(int item);
-
- void InsertNode(int item);
-
- void DeleteNode(int item);
-
- int MinNode();
-
- int MaxNode();
-
- BSNode *GetParent(int item);
-
- BSNode *GetInOrderPre(int item);
-
- BSNode *GetInOrderPost(int item);
-
- void PreOrderVisit();
-
- void InOrderVisit();
-
- void PostOrderVisit();
-
- private:
- BSNode *root;
-
- void Create(BSNode *&root,int *a, int n);
-
- bool Find(BSNode *&root, BSNode *&Node, int item);
-
- void Insert(BSNode *&root, int item);
-
- void Delete(BSNode *&root, int item);
-
- int Min(BSNode *root, BSNode *&minNode);
-
- int Max(BSNode *root, BSNode *&maxNode);
-
- BSNode *getParent(BSNode *root, int item);
-
- BSNode *getInOrderPre(BSNode *root, int item);
-
- BSNode *getInOrderPost(BSNode *root, int item);
-
- void PreOrder(BSNode *root);
-
- void InOrder(BSNode *root);
-
- void PostOrder(BSNode *root);
- };
-
- BSTree::BSTree()
- {
- root = NULL;
- }
-
-
- void BSTree::CreateTree()
- {
- int n = 0;
- Create(root, a, n);
- }
-
-
- void BSTree::Create(BSNode *&root,int *a, int n)
- {
- cout << "请输入树的结点个数:" ;
- cin >> n;
- cout << endl << "请依次输入结点的数值:" << endl;
- BSNode *newNode = NULL;
- for (int i=0; i<n; i++)
- {
- cin >> a[i];
- InsertNode(a[i]);
- }
- }
-
-
- bool BSTree::FindNode(int item)
- {
- BSNode *Node = NULL;
- return Find(root, Node, item);
- }
-
-
- bool BSTree::Find(BSNode *&root, BSNode *&findNode, int item)
- {
- if (root == NULL)
- {
- findNode = NULL;
- return false;
- }
- else
- {
- if (root->data == item)
- {
- findNode = root;
- return true;
- }
- else if (root->data > item)
- {
- return Find(root->m_lchild, findNode, item);
- }
- else
- {
- return Find(root->m_rchild, findNode, item);
- }
- }
- }
-
-
- void BSTree::InsertNode(int item)
- {
- Insert(root, item);
- }
-
-
-
- void BSTree::Insert(BSNode *&root, int item)
- {
- BSNode *newNode = NULL;
- if (root == NULL)
- {
- newNode = new BSNode(item);
- root = newNode;
- cout << item << "已被插入二叉排序树中!" << endl;
- }
- else
- {
- if (FindNode(item))
- {
- cout << "数值" << item << "已经存在!" << endl;
- return;
- }
-
- if (item > root->data)
- {
- Insert(root->m_rchild, item);
- }
- else
- {
- Insert(root->m_lchild, item);
- }
- }
- }
-
-
- void BSTree::DeleteNode(int item)
- {
- Delete(root, item);
- }
-
-
- void BSTree::Delete(BSNode *&root, int item)
- {
- BSNode *pNode = NULL;
- if (root == NULL)
- {
- cout << "树空!" << endl;
- return;
- }
-
- if (Find(root, pNode, item))
- {
- BSNode *pParent = NULL;
-
- if (pNode->m_lchild == NULL && pNode->m_rchild == NULL)
- {
- if (pNode == root)
- {
- root = NULL;
- }
- else
- {
- pParent = GetParent(item);
- if (pParent->m_lchild == pNode)
- {
- pParent->m_lchild = NULL;
- }
- else
- {
- pParent->m_rchild = NULL;
- }
- }
- }
-
-
- else if (pNode->m_rchild != NULL && pNode->m_lchild == NULL)
- {
- if (pNode == root)
- {
- root = pNode->m_rchild;
- }
- else
- {
- pParent = GetParent(item);
- if (pParent->m_lchild == pNode)
- {
- pParent->m_lchild = pNode->m_rchild;
- }
- else
- {
- pParent->m_rchild = pNode->m_rchild;
- }
- }
- }
- else if (pNode->m_lchild != NULL && pNode->m_rchild == NULL)
- {
- if (pNode == root)
- {
- root = pNode->m_lchild;
- }
- else
- {
- pParent = GetParent(item);
- if (pParent->m_lchild == pNode)
- {
- pParent->m_lchild = pNode->m_lchild;
- }
- else
- {
- pParent->m_rchild = pNode->m_lchild;
- }
- }
- }
-
- else if (pNode->m_lchild != NULL && pNode->m_rchild != NULL)
- {
-
- BSNode *maxNode = NULL;
- int max = Max(pNode->m_lchild, maxNode);
- BSNode *father = GetParent(max);
- pNode->data = max;
- if (father != pNode)
- {
- father->m_rchild = maxNode->m_lchild;
- }
- else
- {
- father->m_lchild = maxNode->m_lchild;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- if (root == NULL)
- {
- cout << "删除结点后,二叉排序树为空!" << endl;
- }
- else
- {
- cout << "结点" << item << "删除成功!" << endl;
- }
- }
- else
- {
- cout << "数值为" << item << "的结点不存在!" << endl;
- }
- }
-
-
- int BSTree::MinNode()
- {
- BSNode *minNode;
- return Min(root, minNode);
- }
-
-
- int BSTree::Min(BSNode *root, BSNode *&minNode)
- {
- int min = 0;
- BSNode *p = root;
- if (p == NULL)
- {
- cout << "树空!" <<endl;
- return -1;
- }
- else
- {
- while(p->m_lchild)
- {
- p = p->m_lchild;
- }
- minNode = p;
- return minNode->data;
- }
- return min;
- }
-
-
- int BSTree::MaxNode()
- {
- BSNode *maxNode = NULL;
- return Max(root, maxNode);
- }
-
-
- int BSTree::Max(BSNode *root, BSNode *&maxNode)
- {
- int max = 0;
- BSNode *p = root;
- if (p == NULL)
- {
- cout << "树空!" <<endl;
- return -1;
- }
- else
- {
- while(p->m_rchild)
- {
- p = p->m_rchild;
- }
- maxNode = p;
- return maxNode->data;
- }
- return max;
- }
-
-
- BSNode *BSTree::GetParent(int item)
- {
- return getParent(root, item);
- }
-
-
- BSNode *BSTree::getParent(BSNode *root, int item)
- {
- BSNode *pParent = NULL;
- BSNode *pNode = NULL;
- if (Find(root, pNode, item))
- {
- if ((root == NULL) || (root == pNode))
- {
- pParent = NULL;
- }
- else
- {
- if (root->m_lchild == pNode || root->m_rchild == pNode)
- {
- pParent = root;
- }
- else if (root->data > item)
- {
- pParent = getParent(root->m_lchild, item);
- }
- else
- {
- pParent = getParent(root->m_rchild, item);
- }
- }
- return pParent;
- }
- else
- {
- cout << "不存在值为" << item << "的结点!";
- return pParent;
- }
- }
-
-
- BSNode *BSTree::GetInOrderPre(int item)
- {
- return getInOrderPre(root, item);
- }
-
-
-
-
- BSNode *BSTree::getInOrderPre(BSNode *root, int item)
- {
- BSNode *pPre = NULL;
- BSNode *pNode = NULL;
- if (Find(root, pNode, item))
- {
- if (pNode->m_lchild != NULL)
- {
- BSNode *maxNode = NULL;
- int max = Max(pNode->m_lchild, maxNode);
- pPre = maxNode;
- }
- else
- {
-
- BSNode *pParent = GetParent(item);
- ;
- while(pParent->m_rchild == NULL)
- {
- pParent = GetParent(pParent->data);
- }
-
- pPre = pParent;
- }
- return pPre;
- }
- else
- {
- cout << "数值为" << item << "的结点不存在!";
- return pNode;
- }
- }
-
-
-
-
-
- BSNode *BSTree::GetInOrderPost(int item)
- {
- return getInOrderPost(root, item);
- }
-
-
- BSNode *BSTree::getInOrderPost(BSNode *root, int item)
- {
- BSNode *pPost = NULL;
- BSNode *pNode = NULL;
- if (Find(root, pNode, item))
- {
- if (pNode->m_rchild != NULL)
- {
-
- BSNode *minNode = NULL;
- int mint = Min(pNode->m_rchild, minNode);
- pPost = minNode;
- }
- else
- {
-
- BSNode *pParent = GetParent(item);
- while(pParent->m_lchild == NULL)
- {
- pParent = GetParent(pParent->data);
- }
-
- pPost = pParent;
- }
- return pPost;
- }
- else
- {
- cout << "数值为" << item << "的结点不存在!" << endl;
- return pPost;
- }
- }
-
-
- void BSTree::PreOrderVisit()
- {
- PreOrder(root);
- }
-
-
- void BSTree::PreOrder(BSNode *root)
- {
-
- BSNode* stack[STACK_MAXSIZE];
- int top = 0;
-
- BSNode *p = root;
- cout << "二叉树的先序遍历:";
- while(p || (top != 0))
- {
- if (p)
- {
- stack[top++] = p;
- cout << p->data << " ";
- p = p->m_lchild;
- }
- else
- {
- p = stack[--top];
- p = p->m_rchild;
- }
- }
- cout << endl;
- }
-
-
- void BSTree::InOrderVisit()
- {
- InOrder(root);
- }
-
-
- void BSTree::InOrder(BSNode *root)
- {
-
- BSNode* stack[STACK_MAXSIZE];
- int top = 0;
-
- BSNode *p = root;
- cout << "二叉树的中序遍历:";
- while(p || (top != 0))
- {
- if (p)
- {
- stack[top++] = p;
- p = p->m_lchild;
- }
- else
- {
- p = stack[--top];
- cout << p->data << " ";
- p = p->m_rchild;
- }
- }
- cout << endl;
- }
-
-
- void BSTree::PostOrderVisit()
- {
- PostOrder(root);
- }
-
-
- void BSTree::PostOrder(BSNode *root)
- {
-
- BSNode* stack1[STACK_MAXSIZE];
- BSNode* stack2[STACK_MAXSIZE];
- int top1 = 0;
- int top2 = 0;
- BSNode *p = root;
-
- cout << "二叉树的后序遍历:";
- while(p || (top1 != 0))
- {
- if (p)
- {
- stack1[top1++] = p;
- stack2[top2++] = p;
- p = p->m_rchild;
- }
- else
- {
- p = stack1[--top1];
- p = p->m_lchild;
- }
- }
-
- while(top2 != 0)
- {
- p = stack2[--top2];
- cout << p->data << " ";
- }
-
- cout << endl;
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- BSTree tree;
- tree.CreateTree();
- tree.PreOrderVisit();
- tree.InOrderVisit();
- tree.PostOrderVisit();
- int item = 0;
- cout << "请输入要查找的结点值:";
- cin >> item;
- cout << item << "存在返回1,否则返回0.结果是:" << tree.FindNode(item) << endl;
- cout << item << "的双亲是:" ;
- if (tree.GetParent(item))
- {
- cout << tree.GetParent(item)->data << endl;
- }
- else
- {
- cout << "该结点为根结点,不存在双亲!" << endl;
- }
- cout << item << "中序遍历序列中前驱结点值是:" << tree.GetInOrderPre(item)->data << endl;
- cout << item << "中序遍历序列中后继结点值是:" << tree.GetInOrderPost(item)->data << endl;
- tree.InsertNode(100);
- tree.InsertNode(48);
- tree.DeleteNode(50);
- tree.DeleteNode(45);
- tree.DeleteNode(55);
- tree.InOrderVisit();
- cout << "二叉排序树中,最大值为:" << tree.MaxNode() << endl;
- cout << "二叉排序树中,最小值为:" << tree.MinNode() << endl;
-
- system("pause");
- return 0;
- }
|