Binary search tree based duplicate elimination. - C++ Data Structure

C++ examples for Data Structure:Tree

Description

Binary search tree based duplicate elimination.

Demo Code

#include <iostream>
using namespace std;

template <typename NODETYPE>
class Tree;//  w w w . ja v a2 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;
};



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 {
    // data to insert is less than data in current node
    if (value < (*ptr)->data) {
      insertNodeHelper(&((*ptr)->leftPtr), value);
    }
    else {
      // data to insert is greater than data in current node
      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);
}
// utility function to perform preorder traversal of Tree
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);
  }
}
// begin inorder traversal of Tree
template <typename NODETYPE>
void Tree<NODETYPE>::inOrderTraversal() const {
  inOrderHelper(rootPtr);
}
// utility function to perform inorder traversal of Tree
template <typename NODETYPE>
void Tree<NODETYPE>::inOrderHelper(TreeNode<NODETYPE> *ptr) const {
  if (ptr != 0) {
    inOrderHelper(ptr->leftPtr);
    std::cout << ptr->data << ' ';
    inOrderHelper(ptr->rightPtr);
  }
}
// begin postorder traversal of Tree
template <typename NODETYPE>
void Tree<NODETYPE>::postOrderTraversal() const {
  postOrderHelper(rootPtr);
}
// utility function to perform postorder traversal of Tree
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
  }
}


template<typename T>
void duplicateElimination(T arr[], size_t SIZE) {
  for (unsigned int i = 0; i < SIZE; ++i) {
    for (unsigned int j = i + 1; j < SIZE;) {
      // duplicate found remove it and shift remaining values
      if (arr[i] == arr[j]) {
        for (unsigned int k = j; k < SIZE; ++k) {
          arr[k] = arr[k + 1];
        }
        // reduce size
        --SIZE;
      }
      else {  // only increment if no match (consecutive duplicates)
        ++j;
      }
    }
  }
}

int main(int argc, const char* argv[]) {
  const size_t LENGTH = 10;

  Tree<int> intTree;
  int intArr[LENGTH];

  std::cout << "Enter 10 integers (duplicates welcome): ";

  // insert 10 integers to intTree
  int intValue;
  for (unsigned int i = 0; i < LENGTH; ++i) {
    std::cin >> intValue;

    intTree.insertNode(intValue);

    intArr[i] = intValue;
  }

  std::cout << "Tree" << std::endl;
  intTree.inOrderTraversal();

  std::cout << "\n\nArray - Before elimination:" << std::endl;

  for (unsigned int i = 0; i < LENGTH; ++i)
    printf("%d ", intArr[i]);

  duplicateElimination(intArr, LENGTH);

  std::cout << "\nAfter elimination: " << std::endl;

  for (unsigned int i = 0; i < LENGTH; ++i)
    printf("%d ", intArr[i]);

  std::cout << std::endl;

  return 0;
}

Result


Related Tutorials