How to weave through two chains - c++

I have two chains where lets say chain 1 has head, 1 , 2 , 3 , 4 and chain 2 had head, 5, 6, 7, 8. Weaving it should produce chain 1 with head, 1, 5, 2, 6, 3, 7, 4, 8. My function is currently weaving, but only for the first 2 and it stops.
void Chain::weave(Chain & other) {
while (length_ > 0 && other.length_ > -1){
Node * current = head_->next;
Node * otherNode = other.head_->next;
Node * currentNext = current->next;
Node * currentPrev = current->prev;
Node * otherNext = otherNode->next;
Node * otherPrev = otherNode->prev;
if (current != NULL && otherNode != NULL){
current->next = otherNode;
currentNext = otherNode->next;
currentNext->next = otherNext;
length_++;
other.length_--;
}

Related

Is there any way to determine the node which has maximum amount of leaf-to-leaft node go through it on tree?

Here I have a dp-on-tree problem needs helped. So, the tree has N node and N - 1 edges, and a leaf-to-leaf path is a path from a leaf-node to another leaf-node on tree. Therefore, if the tree has M leaf, the total path will be M * (M + 1) / 2.
How can I find the node which has maximum leaf-to-leaf path go through it? For example, if I have the tree like this, the answer will be node 2. All the paths go through node 2 are: {1 -> 6, 1 -> 7, 6 -> 7, 1 -> 4, 4 -> 6, 4 -> 7, 1 -> 5, 5 -> 6, 5 -> 7}.
I think it's a dp-on-tree problem, but I can't find out the function and dp-formula.
Thks you so much! Also, pls show me some line of code if available.
Example Here
Given a suitable struct node { int id; node *left, *right;} and pre-created tree:
std::tuple<node*, int> max_candidate{nullptr, -1};
void report(node *root, int paths) {
if (paths > std::get<1>(max_candidate)) {
max_candidate = {root, paths};
}
}
int visit(node *root) {
if (!root)
return 0;
if (!root->left && !root->right)
return 1;
int leaves_left = visit(root->left);
int leaves_right = visit(root->right);
report(root, leaves_left * leaves_right);
return leaves_left + leaves_right;
}

Replace NULL with -1 in all nodes of a binary search tree

Given a Binary Search Tree
Instead of making the right or left or both pointers of nodes point to NULL, make them point to nodes containing data value -1.(the -1 nodes then in turn point to NULL).
Can anyone provide a code snippet for the above, preferably C++.
A solution for that is to understand a BST as an array.
Suppose you have this tree, where -1 represent children that don't exist yet:
If we understand the children of a node i as the nodes 2i + 1 (left child) and 2i + 2 (right child), we can represent such tree as an array like this:
[5, 3, 8, 2, 4, 6, 9, 1, -1, -1, -1, -1, 7, -1, 10]
Here's an example of such implementation with the tree above:
#include <iostream>
using namespace std;
struct BST{
int tree[40];
BST(){for(int i = 0; i < 40; i++) tree[i] = -1;}
void add(int value){
int i = 0;
while(i < 40 && tree[i] != -1){
if(tree[i] <= value) i = 2 * i + 2;
else i = 2 * i + 1;
}
tree[i] = value;
}
};
int main (){
BST bst;
bst.add(5); bst.add(3); bst.add(8); bst.add(2); bst.add(4);
bst.add(6); bst.add(9); bst.add(1); bst.add(7); bst.add(10);
for(int i = 0; i < 40; i++){
if(bst.tree[i] != -1){
cout<<bst.tree[i]<<": ["<<bst.tree[2 * i + 1]<<", "<<bst.tree[2 * i + 2]<<"]"<<endl;
}
}
}
OUTPUT (node i: [left child of i, right child of i]).
5: [3, 8]
3: [2, 4]
8: [6, 9]
2: [1, -1]
4: [-1, -1]
6: [-1, 7]
9: [-1, 10]
1: [-1, -1]
7: [-1, -1]
10: [-1, -1]

how does Insert node function of Marching Cube works?

I am trying to understand MarchingCube algorithm:
My previous question was this. Now I am stuck in one function where the node ( which holds each grid of whole cube in linked list kind of structure) is inserted. I can understand the first few line of the code but, when I look at the code I find some part of the code redundant.
NODE *Insert(NODE *listset, NODE *tmpNode) {
1 NODE *temp;
2
3 if(listset == NULL) return tmpNode;
4 else {
5 tmpNode->next = listset->next;
6 listset->next = tmpNode;
7 return listset;
8 }
9 temp = listset;
10 if(temp->next == NULL) {
11 if(temp->depth > tmpNode->depth) temp->next = tmpNode;
12 else {
13 tmpNode->next = temp;
14 listset = tmpNode;
15 }
16 return listset;
17 }
18 while(temp->next != NULL) {
19 if(temp->next->depth > tmpNode->depth) temp = temp->next;
20 else {
21 tmpNode->next = temp->next;
22 temp->next = tmpNode;
23 return listset;
24 }
25 }
26 temp->next = tmpNode;
27 return listset;
}
In this function from 1 to 8 perfectly makes sense ( its just inserting new node to its end).
How could code ever reach after that point (I mean code from 9-27) ?? Is it even necessary??
Can anyone please explain what is going in the part 9-27.

BinaryTree Insert Method

For class I have to create a binaryTree and I can't seem to get the insert method to work properly.
Expected results:
first: tree is not empty
no of nodes = 15
height of tree = 5
The elements of 'first' in inorder:
-11 8 -3 12 -1 -9 -5 2 16 10 6 -13 4 14 -7
The elements of 'first' in preorder:
2 -1 -3 8 -11 12 -5 -9 4 6 10 16 -13 -7 14
The elements of 'first' in postorder:
-11 8 12 -3 -9 -5 -1 16 10 -13 6 14 -7 4 2
second: tree is not empty
no of nodes = 9
height of tree = 4
The elements of 'second' in inorder:
7 3.25 0.75 -7.75 -0.5 -11.5 4.5 -4 8.25
The elements of 'second' in preorder:
-0.5 0.75 3.25 7 -7.75 -4 4.5 -11.5 8.25
The elements of 'second' in postorder:
7 3.25 -7.75 0.75 -11.5 4.5 8.25 -4 -0.5
third: tree is not empty
no of nodes = 7
height of tree = 4
The elements of 'third' in inorder:
objects. list is string This of a
The elements of 'third' in preorder:
This is list objects. string a of
The elements of 'third' in postorder:
objects. list string is of a This
My Results:
first: tree is not empty
no of nodes = 15
height of tree = 4
The elements of 'first' in inorder:
-9 -5 4 16 -1 -13 10 -7 2 14 8 6 -3 -11 12
The elements of 'first' in preorder:
2 -1 4 -5 -9 16 -7 10 -13 -3 6 8 14 12 -11
The elements of 'first' in postorder:
-9 -5 16 4 -13 10 -7 -1 14 8 6 -11 12 -3 2
second: tree is not empty
no of nodes = 9
height of tree = 3
The elements of 'second' in inorder:
-7.75 -4 0.75 -11.5 8.25 -0.5 7 4.5 3.25
The elements of 'second' in preorder:
-0.5 0.75 -4 -7.75 8.25 -11.5 3.25 4.5 7
The elements of 'second' in postorder:
-7.75 -4 -11.5 8.25 0.75 7 4.5 3.25 -0.5
third: tree is not empty
no of nodes = 7
height of tree = 3
The elements of 'third' in inorder:
string a is This objects. of list
The elements of 'third' in preorder:
This is a string list of objects.
The elements of 'third' in postorder:
string a is objects. of list This
Code:
template <class T>
void binTree<T>::insert(binTreeNode < T >*& node, const T& data) {
if(node == NULL) {
root = new binTreeNode<T>(data, NULL, NULL);
return;
}
binTreeNode<T>* ptr1 = node;
binTreeNode<T>* ptr2 = node;
bool placeRight = 0;
while(ptr1 != NULL) {
ptr2 = ptr1;
if(height(ptr1->left) > height(ptr1->right)) {
placeRight = true;
ptr1 = ptr1->right;
} else if (height(ptr1->right) > height(ptr1->left)) {
placeRight = false;
ptr1 = ptr1->left;
} else {
placeRight = false;
ptr1 = ptr1->left;
}
}
if(placeRight) {
ptr2->right = new binTreeNode<T>(data, NULL, NULL);
} else {
ptr2->left = new binTreeNode<T>(data, NULL, NULL);
}
}
Driver Program:
const vector<int> A { 1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15 };
const vector<float> B { 0.5, 1.75, -3, 4.25, 5.50, -6.75, 8, 9.25, -10.5 };
const vector<string> C { "This", "is", "a", "list", "of", "string", "objects." };
int main() {
binTree<int> intTree = binTree<int>();
binTree<float> floatTree = binTree<float>();
binTree<string> strTree = binTree<string>();
for (std::vector<int>::const_iterator it = A.begin() ; it != A.end(); ++it) {
intTree.insert(*it);
}
intTree.preorder(increase);
cout << "first: ";
header(intTree);
inorder(intTree, "first");
preorder(intTree, "first");
postOrder(intTree, "first");
}
Functions to display results: (should be correct)
template <class T>
void binTree<T>::inorder(binTreeNode < T >* node, void (*f)(T&))
{
if (node == NULL) {
return;
}
inorder(node->left,f);
f(node->data);
inorder(node->right,f);
}
template <class T>
void binTree<T>::preorder(binTreeNode < T >* node, void(*f)(T&))
{
if (node == NULL) {
return;
}
f(node->data);
preorder(node->left, f);
preorder(node->right, f);
}
template <class T>
void binTree<T>::postorder(binTreeNode < T >* node, void(*f)(T&))
{
if (node == NULL) {
return;
}
postorder(node->left, f);
postorder(node->right, f);
f(node->data);
}
template <class T>
int binTree<T>::height(binTreeNode <T>* node) const {
if (node == NULL || ((node->left == NULL) && (node->right == NULL))) {
return 0;
}
int leftSide = height(node->left);
int rightSide = height(node->right);
if (leftSide > rightSide) {
return leftSide + 1;
} else {
return rightSide + 1;
}
}
Your bug is in your height method. If you have a node which is not null but has no children, you are returning zero. You should be returning 1.
Change this condition in your height method from:
if (node == NULL || ((node->left == NULL) && (node->right == NULL))) {
return 0;
}
to:
if (node == NULL) {
return 0;
}
It appears the sign of your vector A is backwards. You have 1,-2,3,-4,... but the correct solution has -1,2,-3,4,.... Similarly, you B is
const vector<float> B { 0.5, 1.75, -3, 4.25, 5.50, -6.75, 8, 9.25, -10.5 };
Comparing this with the elements you say we are expecting:
7, 3.25, 0.75, -7.75, -0.5, -11.5, 4.5, -4, 8.25
These don't look even close to identical.
Transcription error somewhere?
What is your height() function ?
I think you misunderstand the definition of the BST:
A. the value of the left child is smaller than the value of root node.
B. the value of the right child is bigger than the value of root node.
C. his left child tree and right child tree are also a BST.
But through your code here:
while(ptr1 != NULL) {
ptr2 = ptr1;
if(height(ptr1->left) > height(ptr1->right)) {
placeRight = true;
ptr1 = ptr1->right;
} else if (height(ptr1->right) > height(ptr1->left)) {
placeRight = false;
ptr1 = ptr1->left;
} else {
placeRight = false;
ptr1 = ptr1->left;
}
}
you just compare the height of your node instead of comparing the real value of the node.

Binary Search Tree with size 7 and height 3

I am constructing a binary search tree of size 7 and height 3. I only have to hard-code it, not generate it through a function.
This is the tree that I have hard-coded
Node (Node (Node (Empty, 0, Empty), 1,
Node (Empty, 3, Empty)), 5
Node (Empty, 7, Node (8, 9, Empty))))
What I want is for Node 9 to have two children (8 and Empty). However, I keep on getting an error on 8 that says "this expression has type int but an expression of was expected of type int tree". How can I correct this?
Thanks!
You cannot write 8 for the leaf. You have to write Node (Empty, 8, Empty).
type tree = Empty | Node of tree * int * tree
(* the tree
5
/ \
/ \
1 7
/ \ \
0 3 9
/
8
*)
let t =
Node (
Node (Node (Empty, 0, Empty),
1,
Node (Empty, 3, Empty)),
5,
Node (
Empty,
7,
Node (Node (Empty, 8, Empty),
9,
Empty)
)
)
(* With an auxliary function we can do this to get the same tree: *)
let leaf k = Node (Empty, k, Empty)
let t' =
Node (
Node (leaf 0, 1, leaf 3),
5,
Node (Empty, 7, Node (leaf 8, 9, Empty)))
The first element of a Node has to be a tree, not something else like an int.
Therefore, you can't put 8 in a place where a tree is expected. Probably, you meant to have Node (Empty, 8 Empty) instead of 8.