BinaryTree::~BinaryTree()
{
_delAllNode(root);
}
void BinaryTree::_delAllNode(pNode root){
if (root != nullptr && root!=NULL){
_delAllNode(root->lChild);
_delAllNode(root->rChild);
DeleteNode(root->data);
}
}
类的定义(头文件)
#pragma once
#include<stdio.h>
#include<stdlib.h>
class BinaryTree
{
private:
typedef struct Node{
struct Node * parent;
struct Node * lChild;
struct Node * rChild;
int data;
}*pNode;
pNode root;
void _visitMiddle(pNode root);
pNode _searchKey(pNode root, int key);
void _delAllNode(pNode root);
public:
BinaryTree();
BinaryTree(int * datum, int len);
pNode SearchMaxNode(pNode node);
pNode SearchMinNode(pNode node);
pNode GetRoot();
pNode SearchKey(int key);
bool DeleteNode(int key);
pNode SearchPredecessor(pNode node);
pNode SearchSuccessor(pNode node);
void VisitMiddle();
bool InsertNode(pNode * cuRoot, int data, pNode self);
~BinaryTree();
};
调用示例
#include <conio.h>
#include "BinaryTree.h"
int main()
{
int arrs[] = { 23, 65, 12, 3, 8, 76, 90, 21, 75, 34,345, 61 };
int len = sizeof(arrs) / sizeof(arrs[0]);
BinaryTree bTree(arrs,len);
bTree.DeleteNode(90);
bTree.VisitMiddle();
getch();
return 0;
}
在Java中实现的二叉树结构