이진 트리 2.0

아, 겨울은 눈의 절반 깜짝 끝났다. 웨이는 학교의 지난 학기의 데이터 구조가되지 너희 (훈련), 다음 학기 두 NA (교육, 훈련), 웨이 그래서 다시 프로젝트 데이터 구조를 배울 수있는 많은있다 생각, 그것은 이진 트리에서 시작! (하지만은 시험 일주일 전에 배우고, 따라서 배울하지 않았다, 그래, 어, 이진 트리를 썼다,하지만!)

먼저, 상기의 살펴 보자 이진 트리 노드의 구조를 :

template<class T>
struct binaryTreeNode
{
    T element;
    binaryTreeNode<T> *leftChild,//左儿子
                      *rightChild;//右儿子
    //构造函数
    binaryTreeNode()
    {
        leftChild=rightChild=NULL;
    }
    binaryTreeNode(const T& theElement)
    {
        element=theElement;
        leftChild=rightChild=NULL;
    }
    binaryTreeNode(const T& theElement,binaryTreeNode *theLeftChild,binaryTreeNode *theRightChild)
    {
        element=theElement;
        leftChild=theLeftChild;
        rightChild=theRightChild;
    }
};

노드는 세 가지 경우에 대응하는 세 생성자를 갖는다.

그런 다음 볼 이진 트리 추상 클래스 :

template<class T>
class binaryTree
{
    virtual ~binaryTree(){}
    virtual bool empty() const =0;
    virtual int size() const =0;
    virtual void preOrder(void (*)(T*))=0;
    virtual void inOrder(void (*)(T*))=0;
    virtual void postOrder(void (*)(T*))=0;
    virtual void levelOrder(void (*)(T*))=0;
};

웨이도 요리 때문에, 나는 ,, 코드 책을 이해하지 못하는 네트워크 클래스에서 학교 이진 트리 탐색 너무. EMM

예약 주문
그림 삽입 설명 여기

//先序遍历
void preOrder(binaryTreeNode *BT)
{
    if(BT)
    {
        printf("%d",BT->element);
        preOrder(BT->leftChild);
        preOrder(BT->rightChild);
    }
}

예약 주문
그림 삽입 설명 여기

//中序遍历
void inOrder(binaryTreeNode *BT)
{
    if(BT)
    {
        inOrder(BT->leftChild);
        printf("%d",BT->element);
        inOrder(BT->rightChild);
    }
}

Postorder
그림 삽입 설명 여기

//后序遍历
void postOrder(binaryTreeNode *BT)
{
    if(BT)
    {
        postOrder(BT->leftChild);
        postOrder(BT->rightChild);
        printf("%d",BT->element);
    }
}

온 오늘 먼저 ~이 배운

게시 32 개 원래 기사 · 원의 찬양 (12) · 전망 1362

추천

출처blog.csdn.net/qq_18873031/article/details/104127312