Algorithm-1. Turn the two-dollar lookup tree into a sorted doubly linked list

Source: Internet
Author: User

Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list. Requires that no new nodes be created, only the pointer is adjusted. Such as.

10

/\

6 14

/\     /\

4 8 12 16

Convert to doubly linked list
4=6=8=10=12=14=16.

This is a binary tree in the middle sequence traversal.

typedef struct bstreenode {

int data;

struct bstreenode *m_pleft;

struct bstreenode *m_pright;

}bstreenode,*pbstreenode;

Pbstreenode linklist = NULL; global variables, pointers to the current linked list

Pbstreenode pformer = NULL; of the previous

void bintreetolinklist (pbstreenode BT) {

if(bt!=NULL) {

bintreetolinklist(bt->m_pleft);

if(linklist= =NULL) {

linklist=bt;

pformer=bt;

}Else{

pformer, m_pright=bt;

bt->m_pleft=pformer;

pformer=bt;

}

bintreetolinklist(bt->m_pright);

}

}

How to verify it, first create a binary tree.

void Addbstreenode (pbstreenode *pnode,int data) {

if (*pnode = = NULL) {

*pnode = (pbstreenode)malloc(sizeof(bstreenode));

(*pnode),data = data;

(*pnode),m_pleft = NULL;

(*pnode),m_pright = NULL;

}

Else if((*pnode),data<data) {

Addbstreenode(& (*pnode), m_pright, data);

}

Else if((*pnode),data>data) {

Addbstreenode(& (*pnode), M_pleft, data);

}

}

Create

void Createtree () {

bstreenode *proot = NULL;

addbstreenode(&proot,ten);

Addbstreenode(&proot,6);

Addbstreenode(&proot,);

Addbstreenode(&proot,4);

Addbstreenode(&proot,8);

Addbstreenode(&proot,N);

Addbstreenode(&proot,+);

bintreetolinklist ( proot);

  }

Algorithm-1. Turn the two-dollar lookup tree into a sorted doubly linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.