Пример #1
0
struct node *treeToList(struct node *root)
{
  if (NULL == root)
  {
    return root;
  }

  struct node *lefttail = treeProcessor(root->small);

  struct node *lefthead = treeToList(root->small);
  struct node *righthead = treeToList(root->large);

  root->large = righthead;
  if (righthead != NULL)
  {
    righthead->small = root;
  }

  if (lefthead != NULL)
  {
    lefttail->large = root;
    root->small = lefttail;
  }
  else
  {
    root->small = NULL;
    lefthead = root;
  }

  return lefthead;
}
Пример #2
0
/*
 --Recursion--
 Given an ordered binary tree, recursively change it into
 a circular doubly linked list which is returned.
*/
static Node treeToList(Node root) {
    Node aList, bList;
    
    if (root==NULL) return(NULL);

    /* recursively solve subtrees -- leap of faith! */
    aList = treeToList(root->small);
    bList = treeToList(root->large);
    
    /* Make a length-1 list ouf of the root */
    root->small = root;
    root->large = root;

    /* Append everything together in sorted order */
    aList = append(aList, root);
    aList = append(aList, bList);
    
    return(aList);



/* Create a new node */
static Node newNode(int data) {
    Node node = (Node) malloc(sizeof(struct node));
    node->data = data;
    node->small = NULL;
    node->large = NULL;
    return(node);
}
Пример #3
0
static Node treeToList(Node root){
	Node aList,bList;
	if(root==NULL)return NULL;

	aList=treeToList(root->small);
	bList=treeToList(root->large);

	root->small=root;
	root->large=root;


	//Append everything together
	aList=append(aList,root);
	bList=append(bList,bList);

	return aList;
}
Пример #4
0
/*
 --Recursion--
 Given an ordered binary tree, recursively change it into
 a circular doubly linked list which is returned.
*/
static Node treeToList(Node root) {
    Node aList, bList;

    if (root==NULL) return(NULL);
    /* recursively solve subtrees -- leap of faith! */
    aList = treeToList(root->small);
      bList = treeToList(root->large);

    /* Make a length-1 list ouf of the root */
    root->small = root;
    root->large = root;
    /* Append everything together in sorted order */
    aList = append(aList, root);
    aList = append(aList, bList);

    return(aList);
}
Пример #5
0
/*Recursion--Given an binary tree,return a circular doubly linked list*/
static Node treeToList(Node root)
{
	Node alist, blist;
	if (root == nullptr)return nullptr;

	/*recursively solve subtrees  --leap of faith*/
	alist = treeToList(root->small);
	blist = treeToList(root->large);

	/*Make a length-1 list ouf of the root*/
	root->small = root;
	root->large = root;

	/*Append everything togeter in sorted order*/
	alist = append(alist, root);
	alist = append(alist, blist);

	return alist;
}
Пример #6
0
 ListNode* treeToList(TreeNode* root) {
     if(root==nullptr){
         return nullptr;
     }
     ListNode* head = nullptr;
     ListNode* cur = new ListNode(root->val);
     ListNode* left = treeToList(root->left);
     ListNode* right = treeToList(root->right);
     if(left==nullptr){
         head = cur;
     }else{
        head = left;
        ListNode* last = left;
        while(last->next){
            last = last->next;
        }
        last->next = cur;
     }
     cur->next = right;
     return head;
 }
Пример #7
0
dlist* treeToList(node *root)
{
	if(!root || !(*root))
	{
	return NULL;
	}
	dlist *rlist, *llist,*retlist;
	retlist = (dlist*)malloc(sizeof(dlist));
	retlist ->head = retlist ->tail = root;
	rlist = treeToList(root->right);
	llist = treeToList(root->left);
	root->right = root->left = NULL;
	if(rlist)
	{
		retlist->head = rlist->head;
	  if(llist)
	  {
		rlist->tail->right = llist->head;
		llist->head->left =  rlist->tail;
		llist->tail->right = root;
		root->left  = llist->tail;
	   }
	   else
	   {
		rlist->tail->right = root;
		root->left = rlist->tail;
	   }
	
	}
	else
	if(llist)
	{	
		llist->tail->right = root;
		root->left = llist->tail;
	}	
return retlist;
}
Пример #8
0
/* Demo that the code works */
int main() {
    Node root = NULL;
    Node head;

    treeInsert(&root, 4);
    treeInsert(&root, 2);
    treeInsert(&root, 1);
    treeInsert(&root, 3);
    treeInsert(&root, 5);

    head = treeToList(root);

    printList(head);    /* prints: 1 2 3 4 5  */

    return(0);
}
Пример #9
0
/*Demo that the code works*/
int main()
{
	Node root = nullptr;
	Node head;
	treeInsert(&root, 4);
	treeInsert(&root, 2);
	treeInsert(&root, 1);
	treeInsert(&root, 3);
	treeInsert(&root, 5);

	head = treeToList(root);

	printList(head);	/*prints:1,2,3,4,5*/

	return 0;
}