我有一个列表中的数据,如:
L = [(3,4,5),(1,4,5),(1,2,3),(1,2,3)]
Run Code Online (Sandbox Code Playgroud)
我需要随机抽取大小为2,所以我想使用:
import random
t1 = random.sample(set(L),2)
Run Code Online (Sandbox Code Playgroud)
现在T1是一个随机拉取值的列表,但我想从我们的初始列表中删除从我们的初始列表中随机拉出的那些.我可以做一个线性for循环但是对于任务我正在尝试为更大的列表执行此操作.所以运行时间将永远!
关于如何解决这个问题的任何建议?
也许快速/简单的问题。我已经实现了二叉树,然后我希望将二叉搜索树转换为数组,或者至少将其打印出来,就像在数组中一样。我遇到问题的地方是如何在“\0”中获取 NULL/标志。
例如,假设我有一棵树,如:
10
/ \
6 12
/ \ \
1 8 15
\
4
Run Code Online (Sandbox Code Playgroud)
我希望它打印它应该如何打印。喜欢:
[10,6,12,1,8,\0,15,\0,4,\0,\0,\0,\0,\0,\0]
^Something Like this^ I don't know if I counted the NULL correctly.
Run Code Online (Sandbox Code Playgroud)
或者关于我想如何在视觉上显示我的树的另一个选项是如何正确输出间距,就像'/'和'\'指向父母的键一样:
10
/ \
6 12
/ \ \
1 8 15
\
4
Run Code Online (Sandbox Code Playgroud)
这是我尝试在代码方面详细阐述的内容,但我卡住了:
void BreadthFirstTravseral(struct node* root)
{
queue<node*> q;
if (!root) {
return;
}
for (q.push(root); !q.empty(); q.pop()) {
const node * const temp_node = q.front();
cout<<temp_node->data << " ";
if (temp_node->left) {
q.push(temp_node->left);
}
if (temp_node->right) …Run Code Online (Sandbox Code Playgroud) 我正在实现使用Array实现表示的二进制搜索树.这是我到目前为止的代码:请注意我完成了树的结构并将其保存为链接列表.我想将此链表转换为数组.
关于如何解决这个问题的想法如下.创建一个return_array函数.将数组的大小设置为最大节点数(2 ^(n-1)+1)并通过链接列表.根节点将是阵列上的@ postion 0,然后他的L-child =(2*[index_of_parent] +1)并且R-child =(2*[index_of_parent] +2).我环顾四周寻找一些可以让我知道如何保持每个节点的固定以及如何通过每个节点的东西.
我是否在思考这个问题?可以有递归吗?
我也在考虑创建一个可视树而不是一个数组,但不知道如何正确地将它分隔出来.如果有人知道如何做到这一点,那么更好地理解它将是非常棒的.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* node){
if(node){
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}
}
void insert(struct node** node, int key){
if(*node == NULL){
(*node) = (struct node*)malloc(sizeof(struct node));
(*node)->data = key;
(*node)->left = NULL;
(*node)->right = NULL;
printf("inserted node with data %d\n", (*node)->data); …Run Code Online (Sandbox Code Playgroud)