Talk:Tree sort

message: I would suggest merging and redirecting Tree sort to binary search tree.  The information on Tree sort is already covered in binary search tree under the Operations section.  It seems unnecessary to have a separate entry for this topic. 300user (talk) 04:57, 13 February 2008 (UTC)
thread_title: Merge and Redirect to Binary Search Tree

message: The C++ code example is not illuminating in the slightest. Most of the sort pages show implementations of the *idea*, while this C++ code is just... "call the library function." Terrible.  —Preceding unsigned comment added by 209.6.209.152 (talk) 19:59, 23 January 2010 (UTC)
replies: text: I disagree. The C++ version that was on there was the only version on the page that performed the efficient O(n log n) sort. The very description of this sorting algorithm calls for the use of a particular data structure, in this case a self-balancing binary search tree. So the example does just that -- use the data structure. How such a data structure might be implemented internally is not relevant, and is described by other articles. This is called abstraction -- we treat it as a black box, knowing what this data structure guarantees us. The brevity of the code is just a reflection of simplicity of the description of this sorting method. --71.141.136.107 (talk) 08:17, 20 August 2010 (UTC)

text: I agree that the C++ code shown is not useful here. The purpose of code samples should be to show how the algorithm could be implemented, not how to use a library. --Joshua Issac (talk) 02:10, 11 March 2011 (UTC)
thread_title:  C++ not illuminating 

message: I have been struggling with this program for too long. Please look and tell me what I am doing wrong:
// BT.CPP
/**
  Traverse a created binary tree and present in sorted order.
  */
#include <stdio.h>
struct node {
  int key_value;
  node *left;
  node *right;
};
void printInorder(struct node *leaf);
class btree {
  public:
    btree();
    ~btree();
    void insert(int key);
    node *search(int key);
    void destroy_tree();
  private:
    void destroy_tree(node *leaf);
    void insert(int key, node *leaf);
    node *search(int key, node *leaf);
    void printInorder();
    void printInorder(int key, node *leaf);
    node *root;
};
btree::btree() {
  root = NULL;
}
btree::~btree() {
  destroy_tree();
}
void btree::destroy_tree(node *leaf) {
  if (leaf != NULL) {
    destroy_tree(leaf->left);
    destroy_tree(leaf->right);
    delete leaf;
  }
}
void btree::insert(int key,node *leaf) {
  if (key < leaf->key_value) {
    if (leaf->left != NULL) {
      insert(key,leaf->left);
    } else {
      leaf->left = new node;
      leaf->left->key_value = key;
      leaf->left->left = NULL;
      leaf->left->right = NULL;
    }
  } else {
    if (leaf->right != NULL) {
      insert(key,leaf->right);
    } else {
      leaf->right= new node;
      leaf->right->key_value = key;
      leaf->right->left = NULL;
      leaf->right->right = NULL;
    }
  }
}
node *btree::search(int key, node *leaf) {
  if (leaf != NULL) {
    if (key == leaf->key_value) {
      return leaf;
    }
    if (key < leaf->key_value) {
      return search(key, leaf->left);
    } else {
      return search(key, leaf->right);
    }
  }
  return leaf;
}
void btree::insert(int key) {
  if (root != NULL) {
    insert(key, root);
  } else {
    root = new node;
    root->key_value = key;
    root->left=NULL;
    root->right=NULL;
  }
}
node *btree::search(int key) {
  return search(key, root);
}
void btree::destroy_tree() {
  destroy_tree(root);
}
void btree::printInorder() {
  printfInorder(root);
}
void btree::printInorder(int key, node *leaf) {
  if (leaf != NULL) {
    search(key,leaf->left);
    printf("%d, ",leaf->key_value);
    search(key,leaf->right);
  }
  return leaf;
}
/** using data from page 45 of book */
int MyData[12] = {128,76,106,402,100,46,354,1018,112,28,396,35};
int main() {
  int i;
  btree MyBtree;
  printf("The original order:\n\n");
  for (i=0;i<12;i++) {
    printf("%d, ",MyData[i]);
    MyBtree.insert(MyData[i]);
  }
/** So now, how do I get it to print in sorted order? */
  printf("The sorted order:\n\n");
  MyBtree.printInorder();
  return 0;
}
Michael Flower (talk) 15:55, 28 March 2016 (UTC)
thread_title:  Why does this binary tree sort not work?