码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树

时间:2015-08-28 17:11:31      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;

/*
void insertTree(BSTNode* &root, keyType val);    //插入值 val
void deleteTree(BSTNode* &root, double val);    //删除值 val

void printByLevel(BSTNode *root);        //树的层序遍历(非递归)
void printByPreorder(BSTNode* root);    //树的前序遍历(非递归)
void printByInorder(BSTNode* root);        //树的中序遍历(非递归)
void printByPostorder(BSTNode *root);    //树的后序遍历(非递归)反序输出

void preorder(BSTNode* root);            //树的前序遍历(递归)
void inorder(BSTNode* root);            //树的中序遍历(递归)
void postorder(BSTNode* root);            //树的后序遍历(递归)

void printPaths(BSTNode* root, keyType sum, keyType *path, int top);    //打印出树中所有路径和为sum的路径,top=0,path[]应该足够存放树的最长路径

void mirrorTree(BSTNode* root);                                            //镜像搜索二叉树

void treeToList(BSTNode* &head, BSTNode* &tail, BSTNode* root);            //将搜素二叉树转化为双向链表

int isPostorderResult(int *a, int len);                                    //判断某序列是否为二叉树后序遍历的结果
*/

typedef double keyType;
struct BSTNode{
    keyType key;
    BSTNode *left, *right;
};
void insertTree(BSTNode* &root, keyType val)
{
    BSTNode* tmp = (BSTNode*)malloc(sizeof(BSTNode));
    tmp->key = val;
    tmp->left = tmp->right = NULL;
    BSTNode *q = NULL, *p = root;
    while (p){
        q = p;
        if (p->key > val) p = p->left;
        else if (p->key < val) p = p->right;
        else return;
    }
    if (q == NULL) root = tmp;
    else if (q->key > val) q->left = tmp;
    else q->right = tmp;
}
void deleteTree(BSTNode* &root, double val)
{
    BSTNode *q = NULL, *p = root, *y;
    while (p){
        if (p->key > val) {
            q = p;
            p = p->left;
        }
        else if (p->key < val){
            q = p;
            p = p->right;
        }
        else break;
    }
    if (p == NULL) return;      //不存在

    if (p->left&&p->right){       // 双分支的特殊处理
        q = p;
        y = p->right;
        while (y->left) {
            q = y;
            y = y->left;
        }
        p->key = y->key;
        p = y;
    }

    if (p->left == NULL) y = p->right;        //单分支,获取分支指针 y
    else y = p->left;

    if (q == NULL) root = y;                //删除根节点 root=y;
    else{                                   //非根节点
        if (p == q->left) q->left = y;
        else q->right = y;
    }
    free(p);
}
//非递归遍历
//层序遍历
void printByLevel(BSTNode *root)
{
    BSTNode *p;
    queue<BSTNode*> q;
    q.push(root);
    while (!q.empty()){
        p = q.front(); q.pop();
        if (p){
            cout << p->key <<  ;
            q.push(p->left);
            q.push(p->right);
        }
    }
    cout << endl;
}
//前序遍历
void printByPreorder(BSTNode* root)
{
    stack<BSTNode*> s;
    BSTNode* p = root;
    while (p || !s.empty()){
        while (p){
            cout << p->key <<  ;
            s.push(p);
            p = p->left;
        }
        if (!s.empty()){
            p = s.top(); s.pop();
            p = p->right;
        }
    }
    cout << endl;
}
//中序遍历
void printByInorder(BSTNode* root)
{
    stack<BSTNode*> s;
    BSTNode *p = root;
    while (p || !s.empty()){
        while (p){
            s.push(p);
            p = p->left;
        }
        if (!s.empty()){
            p = s.top(); s.pop();
            cout << p->key <<  ;
            p = p->right;
        }
    }
    cout << endl;
}
//后序遍历
void printByPostorder(BSTNode *root)
{
    stack<BSTNode*> s;
    BSTNode* p = root;
    while (p || !s.empty()){
        while (p){
            cout << p->key <<  ;
            s.push(p);
            p = p->right;
        }
        if (!s.empty()){
            p = s.top(); s.pop();
            p = p->left;
        }
    }
    cout << endl;
}

//递归遍历
void preorder(BSTNode* root){
    if (root){
        cout << root->key <<  ;
        preorder(root->left);
        preorder(root->right);
    }
}
void inorder(BSTNode* root)
{
    if (root){
        inorder(root->left);
        cout << root->key <<  ;
        inorder(root->right);
    }
}
void postorder(BSTNode* root)
{
    if (root){
        postorder(root->left);
        postorder(root->right);
        cout << root->key <<  ;
    }
}

/* 打印出树中所有路径和为sum的路径 */
void printPaths(BSTNode* root, keyType sum, keyType *path, int top)
{
    path[top++] = root->key;
    sum -= root->key;
    if (root->left == NULL&&root->right == NULL&&sum == 0){
        for (int i = 0; i < top - 1; ++i)
            cout << path[i] <<  ;
        cout << path[top - 1] << endl;
    }
    if (root->left) printPaths(root->left, sum, path, top);
    if (root->right) printPaths(root->right, sum, path, top);
    --top;
    sum += root->key;
}

/* 镜像搜索二叉树 */
void swapTreeNode(BSTNode* &l, BSTNode* &r)
{
    BSTNode* tmp = l; l = r; r = tmp;
}
void mirrorTree(BSTNode* root)
{
    if (root == NULL) return;
    swapTreeNode(root->left, root->right);
    mirrorTree(root->left);
    mirrorTree(root->right);
}

/* 将搜素二叉树转化为双向链表 */
void treeToList(BSTNode* &head, BSTNode* &tail, BSTNode* root)
{
    if (root == NULL){
        head = NULL;
        tail = NULL;
        return;
    }
    BSTNode *lt, *rh;
    treeToList(head, lt, root->left);
    treeToList(rh, tail, root->right);
    if (lt){
        lt->right = root;
        root->left = lt;
    }
    else head = root;
    if (rh){
        rh->left = root;
        root->right = rh;
    }
    else tail = root;
}
void headList(BSTNode* head)
{
    while (head){
        cout << head->key <<  ;
        head = head->right;
    }
    cout << endl;
}
void tailList(BSTNode* tail)
{
    while (tail){
        cout << tail->key <<  ;
        tail = tail->left;
    }
    cout << endl;
}

/* 判断某序列是否为二叉树后序遍历的结果 */
int isPostorderResult(int *a, int len)
{
    if (a == NULL || len <= 0) return 0;
    int root = a[len - 1];
    int i = 0;
    for (; i < len - 1; ++i)
        if (a[i] > root) break;
    int j = i;
    for (; j < len - 1; ++j)
        if (a[j] < root) return 0;
    int left = 1;
    if (i>0)
        left = isPostorderResult(a, i);
    int right = 1;
    if (i < len - 1)
        right = isPostorderResult(a + i, len - i - 1);
    return left&&right;
}
int main()
{
    BSTNode *root = NULL;
    double a[] = { 8, 4, 1, 2, 6, 0, 4, 3, 5, 7, 9 };
    for (int i = 0; i < 11; ++i)
        insertTree(root, a[i]);
    printf("Recursive Traversal : inorder preorder postorder\n");
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
    postorder(root);
    printf("\n");

    deleteTree(root, 5);
    deleteTree(root, 9);
    deleteTree(root, 8);

    printf("Iteration Traversal : level inorder preorder postorder\n");
    printByLevel(root);
    printByInorder(root);
    printByPreorder(root);
    printf("Postorder Traversal print in reverse: ");
    printByPostorder(root);

    keyType path[10];
    printPaths(root, 10, path, 0);
    mirrorTree(root);
    printByLevel(root);
    printByInorder(root);
    printByPreorder(root);
    BSTNode *head, *tail;
    treeToList(head, tail, root);
    headList(head);
    tailList(tail);

    int x[] = { 3, 4, 1, 2, 9, 11, 10, 5 };
    int y[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    if (isPostorderResult(y, 8))
        printf("y is post order result of tree\n");
}

 

二叉搜索树

标签:

原文地址:http://www.cnblogs.com/jokoz/p/4766776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!