List 如何将二叉搜索树转换为双向列表?
这是一个二进制搜索树: 现在我想把它转换成这样的双向列表:46810121416,怎么做List 如何将二叉搜索树转换为双向列表?,list,data-structures,binary-search-tree,List,Data Structures,Binary Search Tree,这是一个二进制搜索树: 现在我想把它转换成这样的双向列表:46810121416,怎么做 Thx执行有序树遍历: 其中,visit(node)将节点附加到一个双链接列表中。您可以按顺序遍历树并将数据插入列表中 // Doubly linked list structure typedef struct node { int data; struct node *prev; struct node *next; }list; // Create node list* crea
Thx执行有序树遍历:
其中,
visit(node)
将节点附加到一个双链接列表中。您可以按顺序遍历树并将数据插入列表中
// Doubly linked list structure
typedef struct node
{
int data;
struct node *prev;
struct node *next;
}list;
// Create node
list* createnode(int x)
{
list* temp=(list*)malloc(sizeof(list));
temp->data=x;
temp->prev=NULL;
temp->next=NULL;
return temp;
}
list *head=NULL; // To keep track of head
list *ptr=NULL; //To keep track of last pointer in list
Inorder(tree * root)
{
if(root==NULL) return;
Inorder(root->left);
// Insert head node
if(head==NULL)
{
head=createnode(root->data);
ptr=head;
}
// Insert other nodes and provide links
else
{
list *temp=createnode(root->data);
ptr->next=temp;
temp->prev=ptr;
ptr=temp;
}
Inorder(root->right);
}