数据结构探险—二叉树

Tree.h

#pragma once
#ifndef _TREE_H
#define _TREE_H
class Tree
{
public:
    Tree(int size,int *pRoot);
    virtual ~Tree();
    int *SearchNode(int nodeIndex)const;
    bool EnTree(int nodeIndex, int direction, int *pNode);
    bool DeTree(int nodeIndex, int *pNode);
    void TreeTraverse()const;
private:
    int *m_pTree;
    int m_iSize;
};


#endif

Tree.cpp

#include "Tree.h"
#include 
using namespace std;

Tree::Tree(int size,int *pRoot)
{
    m_iSize = size;
    m_pTree = new int[size];
    m_pTree[0] = *pRoot;
    for (int i = 1; i < size; i++)
    {
        m_pTree[i] = 0;//假设0代表空
    }
}

Tree::~Tree()
{
    delete[]m_pTree;
    m_pTree = NULL;
}

int *Tree::SearchNode(int nodeIndex)const
{
    if (nodeIndex < 0 || nodeIndex >= m_iSize || m_pTree[nodeIndex] == 0) return NULL;
    return &m_pTree[nodeIndex];
}

bool Tree::EnTree(int nodeIndex, int direction, int *pNode)//direction = 1 是左,= 2 是右
{
    if (nodeIndex < 0 || nodeIndex >= m_iSize || m_pTree[nodeIndex] == 0) return false;
    if (nodeIndex * 2 + direction >= m_iSize || m_pTree[nodeIndex * 2 + direction] != 0) return false;
    m_pTree[nodeIndex * 2 + direction] = *pNode;
    return true;
}

bool Tree::DeTree(int nodeIndex, int *pNode)
{
    if (nodeIndex < 0 || nodeIndex >= m_iSize || m_pTree[nodeIndex] == 0) return false;
    *pNode = m_pTree[nodeIndex];
    m_pTree[nodeIndex] = 0;
    return true;
}

void Tree::TreeTraverse()const
{
    for (int i = 0; i < m_iSize; i++)
    {
        cout << m_pTree[i] << " ";
    }
}

main

#include 
#include 
#include "Tree.h"
using namespace std;
int main(void)
{
    int root = 3;
    Tree *p = new Tree(10,&root);
    int node1 = 5;
    p->EnTree(0, 1, &node1);
    int node2 = 8;
    p->EnTree(0, 2, &node2);
    int node3 = 2;
    p->EnTree(1, 1, &node3);
    int node4 = 6;
    p->EnTree(1, 2, &node4);
    p->TreeTraverse();
    delete p;
    p = NULL;//Look Out!
    system("pause");
    return 0;
}

数据结构探险—二叉树_第1张图片

链式

#include 
#include 

typedef DataType char;

typedef struct node
{
    DataType data;
    struct node *left;
    struct node *right;
}BTNode,*PBTNode,*BiTreeLink;

BiTreeLink CreatTreeLink(char *nodes,int pos,int num)
{
    if(nodes[pos] == '\0' || pos > num) return NULL;
    PBTNode m_pNode = (PBTNode)malloc(sizeof(BTNode));
    if(m_pNode == NULL) printf("Error!");
    m_pNode->data = nodes[pos];
    m_pNode->left = BiTreeLink(nodes,pos*2,num);
    m_pNode->right = BiTreeLink(nodes,pos*2+1,num);
    return = m_pNode;
}

void PrintBiTree(BiTreeLink root)
{
    if(root == NULL) return;
    PBTNode queue[30];//循环队列,30可以改变
    int front = 0;
    int rear = 1;
    queue[0] = root;
    PBTNode P = queue[0];
    while(tear != front)
    {
        p = queue[front];
        front = (front + 1) % 30;
        if(p == NULL) printf("()");//()表示空
         else printf("(%c)",p->data);
        if(p != NULL)
        {
            queue[rear] = p->left;
            rear = (rear + 1) % 30;
            queue[rear] = p->right;
            rear = (rear + 1) % 30; 
        } 
    }   
}

//遍历二叉树
void PreBiTree(BiTreeLink root)
{
    if(root != NULL){
    printf("%c ",root->data);
    PreBiTree(root->left);
    PreBiTree(root->right);
    }
}

求深度

int Depth(BiTreeLink root)
{
    int ld,rd;
    if(root == NULL) return 0;
    else
    {
        ld = Depth(root->left);
        rd = Depth(root->right);
    }
    return ld > rd ? ld +1 ? rd +1;
}
//统计二叉树中结点个数
int NodeCount(BiTreeLink root)
{
    if(root == NULL) return 0;
    else BiTreeLink(root->left) + BiTreeLink(root->right) + 1;
}

//统计叶子结点数目
int LeavesCount(BiTreeLink root)
{
    if (root == NULL) return 0;
    else if (!root->left && !root->right) return 1;
    else 
        return LeavesCount(root->left) + LeavesCount(root->right);
}

递归心得

1.问题的n和n+1要有联系;
2.假定一个函数可以满足要求function();
3.每一次递归中要干什么?回溯判断(八皇后)||改变标志||改变全局变量
4.确定结束条件。

你可能感兴趣的:(数据结构和算法)