1.把二元查找树转变成排序的双向链表
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
首先我们定义的二元查找树节点的数据结构如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
ANSWER:
This is a traditional problem that can be solved using recursion.
For each node, connect the double linked lists created from left and right child node to form a full list.
/**
* @param root The root node of the tree
* @return The head node of the converted list.
*/
BSTreeNode * treeToLinkedList(BSTreeNode * root) {
BSTreeNode * head, * tail;
helper(head, tail, root);
return head;
}
void helper(BSTreeNode *& head, BSTreeNode *& tail, BSTreeNode *root) {
BSTreeNode *lt, *rh;
if (root == NULL) {
head = NULL, tail = NULL;
return;
}
helper(head, lt, root->m_pLeft);
helper(rh, tail, root->m_pRight);
if (lt!=NULL) {
lt->m_pRight = root;
root->m_pLeft = lt;
} else {
head = root;
}
if (rh!=NULL) {
root->m_pRight=rh;
rh->m_pLeft = root;
} else {
tail = root;
}
}
思路:迭代中序遍历
/**************************************************************************/
#include<iostream>
#define Max 100
struct search_tree
{
int n;
search_tree *left;
search_tree *right;
};
struct stack
{
search_tree *a[Max];
int top;
};
void push(stack *sta, search_tree *p);
search_tree * pop(stack *sta);
void insert_BST(search_tree *ptr, int n);
search_tree* bulid_tree();
void mid_order(search_tree *ptr);
search_tree * d_link(search_tree *ptr);
search_tree* print_r(search_tree *node);
search_tree* print_l(search_tree *node);
int main()
{
using namespace std;
search_tree *ptr;
cout << "enter numbers!" << endl;
ptr = bulid_tree();
ptr = d_link(ptr);
ptr = print_r(ptr);
cout << endl;
// cout << "5"<<endl;
ptr = print_l(ptr);
cin.get();
return 0;
}
void push(stack *sta, search_tree *p)
{
++sta->top;
sta->a[sta->top] = p;
}
search_tree* pop(stack *sta)
{
return sta->a[sta->top--];
}
void insert_BST(search_tree *ptr, int n)
{
search_tree *temp = NULL;
search_tree *head = NULL, *parent = NULL;
temp = new search_tree;
temp->n = n;
temp->left = NULL;
temp->right = NULL;
head = ptr;
while (ptr)
{
parent = ptr;
if (ptr->n > n)
ptr = ptr->left;
else
ptr = ptr->right;
}
if (parent->n > n) parent->left = temp;
else parent->right = temp;
}
search_tree * bulid_tree()
{
int n;
char c;
search_tree *ptr = NULL;
while (1)
{
std::cin >> n;
std::cin.get(c);
if (ptr == NULL)
{
ptr = new search_tree;
ptr->n = n;
ptr->left = NULL;
ptr->right = NULL;
}
else insert_BST(ptr, n);
if (c == '\n') break;
}
return ptr;
}
void mid_order(search_tree *ptr)
{
stack *sta;
sta = new(stack);
sta->top = -1;
while (1)
{
while (ptr)
{
push(sta, ptr);
ptr = ptr->left;
}
if (sta->top == -1) break;
ptr = pop(sta);
std::cout << ptr->n << "->";
ptr = ptr->right;
}
}
search_tree * d_link(search_tree *ptr)
{
int flag = 0;
search_tree *pre = NULL, *ppre = NULL, *head = NULL;
stack *sta;
sta = new(stack);
sta->top = -1;
while (1)
{
while (ptr)
{
push(sta, ptr);
ptr = ptr->left;
}
if (sta->top == -1)
{
pre->right = NULL;
pre->left = ppre;
break;
}
ptr = pop(sta);
//std::cout << ptr->n << "->";
if (!flag)
{
flag = 1;
head = ptr;
}
else
{
pre->right = ptr;
pre->left = ppre;
}
ppre = pre;
pre = ptr;
ptr = ptr->right;
}
return head;
}
search_tree * print_r(search_tree *node)
{
search_tree *temp = NULL;
while (node != NULL)
{
std::cout << node->n << "->";
temp = node;
node = node->right;
}
return temp;
}
search_tree* print_l(search_tree *node)
{
search_tree *temp = NULL;
while (node != NULL)
{
std::cout << node->n << "->";
temp = node;
node = node->left;
}
return temp;
}