Binary Tree of Strings - C++ Data Structure

C++ examples for Data Structure:Tree

Description

Binary Tree of Strings

Demo Code

template <typename NODETYPE>
class Tree;/* w w w.j a v a 2  s  .  c o  m*/

template <typename NODETYPE>
class TreeNode {
    friend class Tree<NODETYPE>;
 public:
    explicit TreeNode(const NODETYPE &d) : leftPtr(0), data(d), rightPtr(0) {}

    NODETYPE getData() const { return data; }

 private:
    TreeNode<NODETYPE> *leftPtr;
    NODETYPE data;
    TreeNode<NODETYPE> *rightPtr;
};


#include <iostream>

template <typename NODETYPE>
class Tree {
 public:
        Tree();

        void insertNode(const NODETYPE &);
        void preOrderTraversal() const;
        void inOrderTraversal() const;
        void postOrderTraversal() const;

 private:
        TreeNode<NODETYPE> *rootPtr;

        // utility functions
        void insertNodeHelper(TreeNode<NODETYPE> **, const NODETYPE &);
        void preOrderHelper(TreeNode<NODETYPE> *) const;
        void inOrderHelper(TreeNode<NODETYPE> *) const;
        void postOrderHelper(TreeNode<NODETYPE> *) const;
};

template <typename NODETYPE>
Tree<NODETYPE>::Tree() {
    rootPtr = 0;  // indicate tree is initially empty
}
template <typename NODETYPE>
void Tree<NODETYPE>::insertNode(const NODETYPE &value) {
    insertNodeHelper(&rootPtr, value);
}
template <typename NODETYPE>
void Tree<NODETYPE>::insertNodeHelper(TreeNode<NODETYPE> **ptr,
        const NODETYPE &value) {
    if (*ptr == 0) {
        *ptr = new TreeNode<NODETYPE>(value);
    } else {
        if (value < (*ptr)->data) {
            insertNodeHelper(&((*ptr)->leftPtr), value);
        } else {
            if (value > (*ptr)->data)
                insertNodeHelper(&((*ptr)->rightPtr), value);
            else  // duplicate value ignored
                std::cout << value << " dup" << std::endl;
        }
    }
}
template <typename NODETYPE>
void Tree<NODETYPE>::preOrderTraversal() const {
    preOrderHelper(rootPtr);
}
template <typename NODETYPE>
void Tree<NODETYPE>::preOrderHelper(TreeNode<NODETYPE> *ptr) const {
    if (ptr != 0) {
        std::cout << ptr->data << ' ';  // process node
        preOrderHelper(ptr->leftPtr);
        preOrderHelper(ptr->rightPtr);
    }
}
template <typename NODETYPE>
void Tree<NODETYPE>::inOrderTraversal() const {
    inOrderHelper(rootPtr);
}
template <typename NODETYPE>
void Tree<NODETYPE>::inOrderHelper(TreeNode<NODETYPE> *ptr) const {
    if (ptr != 0) {
        inOrderHelper(ptr->leftPtr);
        std::cout << ptr->data << ' ';
        inOrderHelper(ptr->rightPtr);
    }
}
template <typename NODETYPE>
void Tree<NODETYPE>::postOrderTraversal() const {
    postOrderHelper(rootPtr);
}
template <typename NODETYPE>
void Tree<NODETYPE>::postOrderHelper(TreeNode<NODETYPE> *ptr) const {
    if (ptr != 0) {
        postOrderHelper(ptr->leftPtr);
        postOrderHelper(ptr->rightPtr);
        std::cout << ptr->data << ' ';  // process node
    }
}


#include <iostream>
#include <string>
#include <sstream>

int main(int argc, const char* argv[]) {
    Tree<std::string> strTree;

    std::string input;
    std::cout << "Enter a string: ";
    std::getline(std::cin, input);

    std::istringstream ss(input);
    std::string parsed;

    while (ss >> parsed) {
        strTree.insertNode(parsed);
    }

    std::cout << std::endl;

    strTree.inOrderTraversal();
    strTree.preOrderTraversal();
    strTree.postOrderTraversal();

    return 0;
}

Result


Related Tutorials