Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ How do I write a function that traverse a binary tree and insert items in nodes into a linked list while traversing. I don't

C++

How do I write a function that traverse a binary tree and insert items in nodes into a linked list while traversing. I don't know what I'm doing and its getting worse, been stuck for days, help please! Pretend that I have items stored in my BTNodes.

These are the header files I have.

template class BTNode { public: T item; BTNode *left, *right; BTNode(T); };

template BTNode::BTNode(T newItem) { item = newItem; left = right = NULL;

} ----------------------------------------------------------------------------------------------------

(Linked List node)

template class Node { public: T item; Node *next; Node(T);

};

template Node::Node(T newitem) { item = newitem; next = NULL;

}

-------------------------------------------------------------------------------------

BST.h

class BST { private: int count; BTNode *root;

void insertList2(BTNode *);

public:

bool insertList(T);

template void BST::insertintoList2(BTNode* cur, List list) { if (cur == NULL) return; list.List_insert(cur->item); insertList2(cur->left); // L insertList2(cur->right); // R

}

template bool BST::insertintoList(T) { if (root == NULL) return false;

insertList2(root);

return true;

}

-------------------------------------------------------------------------------------------------------

Linked List header

List.h

template class List { private: int count; Node *head; Node *find(int);

public:

List();

bool List_insert(T);

template List::List() { head = NULL; count = 0; }

template bool List::List_insert(T newItem) { Node *pre, *cur, *tmp = new Node(newItem);

if (!tmp) return false; if (empty()) { head = tmp; count++; return true; } count++; if (head->item >= newItem) { tmp->next = head; head = tmp; return true; } pre = head; cur = pre->next; for (; cur != NULL;) { if (cur->item >= newItem) break; pre = cur; cur = cur->next; } tmp->next = cur; pre->next = tmp; return true; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

Students also viewed these Databases questions