// BinaryTreeToDoubleList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; // template<typename T> struct Node { T data; Node<T> *left; Node<T> *right; Node(): left(NULL), right(NULL) { } }; //create binary sorted tree template <typename T> void AddBstNode(Node<T> *& node, T value) { if(NULL == node) { node = new Node<T>(); node->data = value; } else { if(value < node->data) { AddBstNode(node->left, value); } else { AddBstNode(node->right, value); } } } // template <typename T> void PrintBstNode(Node<T> *& node) { if(NULL != node) { PrintBstNode(node->left); cout << node->data << endl; PrintBstNode(node->right); } } //change Binary sorted tree to double list template <typename T> void ChangeBstToDl(Node<T> * node, Node<int> *& last) { if(NULL == node) { return; } ChangeBstToDl(node->left, last); if(NULL != last) { node->left = last; last->right = node; } last = node; ChangeBstToDl(node->right, last); } // template <typename T> void PrintDoubleList(Node<T> * node) { if(NULL == node) { return; } if(NULL == node->right) { while(node) { cout << node->data << endl; node = node->left; } } else { while(node) { cout << node->data << endl; node = node->right; } } } // int _tmain(int argc, _TCHAR* argv[]) { Node<int>* root = NULL; // AddBstNode<int>(root, 10); AddBstNode<int>(root, 4); AddBstNode<int>(root, 6); AddBstNode<int>(root, 8); AddBstNode<int>(root, 12); AddBstNode<int>(root, 14); AddBstNode<int>(root, 7); AddBstNode<int>(root, 15); AddBstNode<int>(root, 6); AddBstNode<int>(root, 16); // PrintBstNode<int>(root); // Node<int>* last = NULL; // ChangeBstToDl(root, last); // PrintDoubleList(last); // int c; cin >> c; // return 0; }