C 如何将给定级别的二叉搜索树转换为链接链?
我必须做一个函数,给定int n和二元搜索树,我必须将bst int的n级转换为链表。 例如,如果给定数字2和此树C 如何将给定级别的二叉搜索树转换为链接链?,c,linked-list,binary-search-tree,nodes,C,Linked List,Binary Search Tree,Nodes,我必须做一个函数,给定int n和二元搜索树,我必须将bst int的n级转换为链表。 例如,如果给定数字2和此树 2 / \ 5 3 我必须用5-3做一个喜欢的列表 我很难到达给定的级别,然后获取该级别上的每个节点,因为如果我到达该级别,我不知道如何到达下一个节点。也就是说,我只能在一个分支上达到这个级别,我想不出任何递归的方法 这是bst和链接链的结构: struct nodo { info_t dato; nodo *anterio
2
/ \
5 3
我必须用5-3做一个喜欢的列表
我很难到达给定的级别,然后获取该级别上的每个节点,因为如果我到达该级别,我不知道如何到达下一个节点。也就是说,我只能在一个分支上达到这个级别,我想不出任何递归的方法
这是bst和链接链的结构:
struct nodo {
info_t dato;
nodo *anterior;
nodo *siguiente;
};
struct rep_cadena {
nodo *inicio;
nodo *final;
};
struct rep_binario {
info_t dato;
rep_binario *izq;
rep_binario *der;
};
这是我无法理解的函数:
cadena_t nivel_en_binario(nat l, binario_t b)
我尝试过使用我已经做过的另一个函数,计算树的高度,但我不能停留在想要的水平上
nat altura_binario(binario_t b) {
if (b==NULL) return 0;
else return maximo(altura_binario(b->izq), altura_binario(b->der))+ 1;
}
其中maximo()返回两个给定数字之间的最大数字。您可以通过实现算法并稍加修改来实现这一点。您可以将成对的
(节点,级别)
(其中节点级别=父级别+1)排队,而不是仅将节点排队,然后排队时,您可以检查是否已达到所需级别,并将其作为结果输出,而不是进一步排队
伪代码草图:
target_level = ...read from input...
let level_nodes = ...an empty list...
let queue = ...an empty queue...
queue.enqueue((root_node, 0))
while queue is not empty:
node, level = queue.dequeue()
if level == target_level:
level_nodes.append(node)
else:
if node has left child:
queue.enqueue((left_child_node, level + 1))
if node has right child:
queue.enqueue((right_child_node, level + 1))
您可以通过实现算法并稍加修改来实现这一点。您可以将成对的
(节点,级别)
(其中节点级别=父级别+1)排队,而不是仅将节点排队,然后排队时,您可以检查是否已达到所需级别,并将其作为结果输出,而不是进一步排队
伪代码草图:
target_level = ...read from input...
let level_nodes = ...an empty list...
let queue = ...an empty queue...
queue.enqueue((root_node, 0))
while queue is not empty:
node, level = queue.dequeue()
if level == target_level:
level_nodes.append(node)
else:
if node has left child:
queue.enqueue((left_child_node, level + 1))
if node has right child:
queue.enqueue((right_child_node, level + 1))
Adivino,Tarea 3 Programacion 2 Fing jajaja,estamos en la Mism,pudiste solucionarlo?Adivino,Tarea 3 Programacion 2 Fing jajaja,estamos en la Mism,pudiste solucionarlo?这是一个很好的解决方案,问题是我不能使用队列,我只能使用上面列出的结构或简单的结构(如数组等)。也许我可以尝试只使用列表和我已经拥有的所有函数(如remove first和last)。是的,你可以对队列使用列表结构,因为它有头指针和尾指针。这是一个很好的解决方案,问题是我不能使用队列,我只能使用上面列出的结构或简单的结构(如数组等)。也许我可以尝试只使用列表和我已经拥有的所有函数(如remove first和last)来执行此操作。是的,您可以对队列使用列表结构,因为它具有头指针和尾指针。欢迎使用堆栈溢出…欢迎使用堆栈溢出。。。