二叉排序树(c++)

本文介绍了一个简单的二叉树类的实现,包括插入、查找、删除节点等基本操作,并通过示例展示了这些功能的应用。

***********************btree.h********************

#ifndef _BTREE_H_
#define _BTREE_H_

#include <iostream>
using namespace std;

class Node
{
 public:
  Node(); 
  int iKey;
  Node *pLChild;
  Node *pRChild;

};

class BTree
{

 public:
  BTree();
  ~BTree();
  Node *InsertTree(int iKey);
  Node * SearchTree(int iKey);
  Node * DeleteTree(int iKey);
  void DestroyTree();
  void ShowTree();

 private:
  Node *Search(Node *pNode, int iKey);
  Node *Insert(Node* pHead,  int iKey);
  Node * Delete(Node *pHead, int iKey);
  void Destroy(Node *pHead);
  void Show(Node *pHead);
 public:
  Node *m_pRoot;
};

***********************btree.cpp********************

#include "btree.h"

#define _MAIN_

Node::Node()
{
 iKey =0;
 pLChild = NULL;
 pRChild = NULL;
}

BTree::BTree()
{
 m_pRoot = NULL;
}

BTree::~BTree()
{
 DestroyTree();
}


Node *BTree::Insert(Node* pHead,  int iKey)
{

 if (pHead == NULL)
 {
  Node * pNow = new Node();
  pNow->iKey = iKey;
  pNow->pLChild = pNow->pRChild = NULL;
  return pNow;
 }

 if (iKey <pHead->iKey)
 {
  pHead->pLChild = Insert(pHead->pLChild, iKey);
 }
 else
 {
  pHead->pRChild = Insert(pHead->pRChild,iKey);
 }
 
 return pHead;
}


Node *BTree::Search(Node *pNode, int iKey)
{
 if (pNode == NULL) return NULL;

 if (iKey == pNode->iKey)
 {
  return pNode;
 }
 else if (iKey < pNode->iKey)
 {
  return Search(pNode->pLChild, iKey);
 }
 else
 {
  return Search(pNode->pRChild, iKey);
 }
}

void BTree::Show(Node *pHead)
{
 //cout << "debug ............begin show pHead=" << pHead << endl;
 if (pHead != NULL)
 {
  Show(pHead->pLChild);
  cout << pHead->iKey << " ";
  Show(pHead->pRChild);
 }
}

Node * BTree::Delete(Node *pHead, int iKey)
{
 Node *p, *q;

 if (pHead->iKey == iKey)
 {
  if (pHead->pLChild ==NULL &&pHead->pRChild ==NULL)
  {
   delete pHead;
   return NULL;
  }
  else if (pHead->pLChild == NULL)
  {
   p = pHead->pRChild;
   delete pHead;
   return p;
  }
  else if (pHead->pRChild == NULL)
  {
   p = pHead->pLChild;
   delete pHead;
   return p;
  }
  else
  {
   p = q = pHead->pRChild;
   while (p->pLChild != NULL)
   {
    //找到左子树最左孩子结点
    p = p->pLChild;
   }
   //原左子树作为左孩子
   p->pLChild = pHead->pLChild;
   delete pHead;
   return q;
  }
 }

 if(pHead->iKey >iKey &&pHead->pLChild !=NULL)
 {
  pHead->pLChild = Delete(pHead->pLChild, iKey);
 }
 if(pHead->iKey <iKey &&pHead->pRChild !=NULL)
 {
  pHead->pRChild = Delete(pHead->pRChild, iKey);
 }
 return pHead;
}

void BTree::Destroy(Node * pHead)
{
 if (pHead!=NULL)
 {
  Destroy(pHead->pLChild);
  Destroy(pHead->pRChild);
  delete pHead;
 }

}

Node * BTree::SearchTree(int iKey)
{
 return Search(m_pRoot, iKey);

}


Node *BTree::InsertTree(int iKey)
{
 m_pRoot = Insert(m_pRoot, iKey);
}


Node * BTree::DeleteTree(int iKey)
{
 return Delete(m_pRoot,iKey);
}

void BTree::DestroyTree()
{
 Destroy(m_pRoot);
}
void BTree::ShowTree()
{
 Show(m_pRoot);

}

#ifdef _MAIN_

int main()
{
 BTree *pBTree = new BTree();
 
 
 pBTree->InsertTree(6);
 pBTree->InsertTree(4);
 pBTree->InsertTree(9);
 pBTree->InsertTree(8);
 pBTree->InsertTree(7);
 pBTree->InsertTree(3);
 pBTree->InsertTree(5);
 pBTree->InsertTree(10);
 
 cout << "root:" << pBTree->m_pRoot << endl;
 pBTree->ShowTree();
 cout << endl;
 Node *pNode = pBTree->SearchTree(3);
 if (pNode != NULL)
 {
  cout << "search key = " << pNode->iKey << "ok" << endl;
 }
 
 pBTree->DeleteTree(4);
 cout << "after delete 4 "<< endl;
 pBTree->ShowTree();
 pBTree->DeleteTree(7);
 cout << "after delete 7 "<< endl;
 pBTree->ShowTree();
 
 delete pBTree;
 return 0;

}


#endif

下面是一个简单的二叉排序树C++ 实现: ``` #include <iostream> using namespace std; struct TreeNode { int data; TreeNode* left; TreeNode* right; }; void insert(TreeNode*& node, int data) { if (!node) { node = new TreeNode{ data, nullptr, nullptr }; } else if (data < node->data) { insert(node->left, data); } else if (data > node->data) { insert(node->right, data); } } void inorder(TreeNode* node) { if (node) { inorder(node->left); cout << node->data << " "; inorder(node->right); } } void preorder(TreeNode* node) { if (node) { cout << node->data << " "; preorder(node->left); preorder(node->right); } } void postorder(TreeNode* node) { if (node) { postorder(node->left); postorder(node->right); cout << node->data << " "; } } int main() { TreeNode* root = nullptr; int arr[] = { 5, 3, 7, 1, 9, 4, 6 }; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) { insert(root, arr[i]); } cout << "Inorder traversal: "; inorder(root); cout << endl; cout << "Preorder traversal: "; preorder(root); cout << endl; cout << "Postorder traversal: "; postorder(root); cout << endl; return 0; } ``` 上面的代码实现了二叉排序树的插入和三种遍历方式(中序遍历、先序遍历和后序遍历)。在 `insert` 函数中,如果当前节点为空,就新建一个节点并把数据插入该节点;否则,根据数据大小递归向左或向右子树插入数据。 在 `main` 函数中,创建了一个二叉排序树并插入了一些数据。然后分别对该树进行了三种遍历,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值