A2Pro THPT Buôn Ma Thuột [2007-2010]

Có một nơi mà chúng ta sẽ không bao giờ quên.. nơi thấm đẫm tình bạn, tình yêu thương thân thiết, nơi gắn bó mãi mãi với những kí ức đẹp nhất của thời học sinh yêu dấu... Nơi đó là đây !

Đăng Nhập

Quên mật khẩu

Latest topics


CTDL - Tree

Share

FR
Silver Dragon
Silver Dragon

Gender : Nam

Posts Posts : 1023 Thanked : 660
Coins Coins : 6192
Tham Gia : 26/08/2008
Birthday : 05/08/1920

Status Status : Sẽ ra sao nếu như, tao là mày và mày là tao ?

code CTDL - Tree

Bài gửi by FR on Thu Oct 18, 2012 8:46 am

Code:
#ifndef __TREESAMPLE__H__
#define __TREESAMPLE__H__

#include "stdio.h"
#include "iostream.h"

enum Error_code {success,fail,rangeerror,overflow,not_present,
                dupplicated,found};

//---------------------------------------------------------
class Node {
public:
    int data;
    Node *left, *right;
    Node();
    Node(int );
    Node(int item, Node * left, Node * right);
};

//---------------------------------------------------------
class Tree {
public:
    Tree();
    ~Tree();
   
    bool empty();
//    int size();
    Error_code insertAt(Node *, bool, int, Node **);
    void printLNR();
//    void printLRN();
//    void printNLR();

protected:
    Node *root;
    void destroy(Node *);
    void printLNR_recursive(Node *);
};

#endif //__TREESAMPLE__H__

Code:


#include "stdafx.h"

//---------------------------------------------------------
Node::Node() {
    left = right = NULL;
}
//---------------------------------------------------------
Node::Node(int item) {
    this->data = item;
    left = right = NULL;
}
//---------------------------------------------------------
Node::Node(int item, Node* left, Node*right) {
    this->data = item;
    this->left = left;
    this->right = right;
}
//---------------------------------------------------------
Tree::Tree() {
    root = NULL;
}
//---------------------------------------------------------
Tree::~Tree() {
    destroy(root);
    root = NULL;
}
//---------------------------------------------------------
void Tree::destroy(Node *root) {
    if (root != NULL) {
        destroy(root->left);
        destroy(root->right);
        delete root;
    }
}
//---------------------------------------------------------
//Tree::size() {
//}
//---------------------------------------------------------
Error_code Tree::insertAt(Node *parent, bool left_or_right, int x, Node **new_node) {
    //This method make a new node of value x and insert into the left or the right of the parent node.
    //If the parent is NULL, and the root of the tree is NULL, the new node becomes the root of the tree
    //If the parent is not NULL, and the left_or_right is true (false), and the left (right) child of parent is NULL,
    //the new node is added to the left (right) of the parent
    //The return value is success for those cases and the new_node points to the new node.
    //Otherwise, the return value is fail and the new_node is unchanged
    if ((parent == NULL) && (root != NULL))
        return fail;
    if ((parent!=NULL ) && (    (left_or_right &&  (parent->left != NULL))
                            || (!left_or_right && (parent->right != NULL))   
                          )
        )
        return fail;

    *new_node = new Node(x);
    if (parent == NULL){
        root = *new_node;
    } else {
        if (left_or_right)
            parent->left = *new_node;
        else
            parent->right = *new_node;
    }
    return success;
}

//---------------------------------------------------------
bool Tree::empty() {
    return (root==NULL);
}
//---------------------------------------------------------
void Tree::printLNR_recursive(Node *root) {
    if (root != NULL) {
    printLNR_recursive(root->left);
    cout << root->data << " ";
    printLNR_recursive(root->right);
    }
}

//---------------------------------------------------------
void Tree::printLNR() {
    printLNR_recursive(root);
}


Code:
// TestTree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void main() {
    Tree aTree;
    Node *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9, *p10;
    aTree.insertAt(NULL, true, 3, &p1);
    aTree.insertAt(p1, true, 5, &p2); 
    aTree.insertAt(p1, false, 26, &p3);
    aTree.insertAt(p2, true, 14, &p4); 
    aTree.insertAt(p2, false, 7, &p5); 
    aTree.insertAt(p3, true, 6, &p6); 
    aTree.insertAt(p5, true, 11, &p7); 
    aTree.insertAt(p6, true, 22, &p8); 
    aTree.insertAt(p6, false, 190, &p9); 
    aTree.insertAt(p7, true, 19, &p10); 
    /* At this point, the tree is
              3
            /  \
          5      26
          / \    /
        14    7    6   
            /    / \
          11  22  190
          /
        19
    */
    aTree.printLNR();  //the result: 14 5 19 11 7 3 22 6 190 26
}

    Hôm nay: Thu Dec 08, 2016 9:10 pm