binary-tree

Find Path to Specified Node in Binary Tree (Python)

淺唱寂寞╮ 提交于 2020-06-12 22:05:57
问题 I'm having trouble computing the path from the root to a specified node in a binary tree (this is specifically about a Python solution to this problem). Here's an example. Given the binary tree below, if I specify the node whose value is 4, I want to return [1, 2, 4]. If I specify the node whose value is 5, I want to return [1, 2, 5]. 1 / \ 2 3 / \ 4 5 Here's my attemped solution. class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None def path(root, k, l=[]):

Priority Queue with a find function - Fastest Implementation

断了今生、忘了曾经 提交于 2020-06-09 17:00:09
问题 I am looking at implementing a priority queue with an added requirement, a find/search function which will tell whether an item is anywhere within the queue. So the functions will be: insert, del-min and find. I am unsure whether I should use a Heap or a Self-balancing binary search tree. It appears PQs are usually implemented with a Heap, but I am wondering if there is any advantage in using a binary search tree since I also need that find function. Furthermore, on average I'll be doing more

Priority Queue with a find function - Fastest Implementation

无人久伴 提交于 2020-06-09 16:59:10
问题 I am looking at implementing a priority queue with an added requirement, a find/search function which will tell whether an item is anywhere within the queue. So the functions will be: insert, del-min and find. I am unsure whether I should use a Heap or a Self-balancing binary search tree. It appears PQs are usually implemented with a Heap, but I am wondering if there is any advantage in using a binary search tree since I also need that find function. Furthermore, on average I'll be doing more

How to print a binary tree in as a structure of nodes in Python

倾然丶 夕夏残阳落幕 提交于 2020-05-14 07:25:17
问题 I have a python code to convert a string mathematical expression into a binary tree and order the nodes of the tree so the left child will be always smaller than the right child. I want to print the binary tree in the following order. For example consider the mathematical expression ((2 * 75) / 4). buildParseTree() converts the string expression into a tree and printNodeInLevels() rearranges the nodes so left child is smaller than the right right child at each level. Operands < operators and

How to print a binary tree in as a structure of nodes in Python

这一生的挚爱 提交于 2020-05-14 07:19:31
问题 I have a python code to convert a string mathematical expression into a binary tree and order the nodes of the tree so the left child will be always smaller than the right child. I want to print the binary tree in the following order. For example consider the mathematical expression ((2 * 75) / 4). buildParseTree() converts the string expression into a tree and printNodeInLevels() rearranges the nodes so left child is smaller than the right right child at each level. Operands < operators and

Trying to print top view of a tree using two if statements

ぃ、小莉子 提交于 2020-05-12 11:38:05
问题 Problem Statement You are given a pointer to the root of a binary tree. Print the top view of the binary tree. You only have to complete the function. My Code: void top_view(Node root) { Node r = root; if(r.left!=null){ top_view(r.left); System.out.print(r.data + " "); } if(r.right!=null){ System.out.print(r.data + " "); top_view(r.right); } } The two if statements are executed every time the function is called, but I need only one of them to execute. I tried switch but its giving constant

Trying to print top view of a tree using two if statements

大城市里の小女人 提交于 2020-05-12 11:37:28
问题 Problem Statement You are given a pointer to the root of a binary tree. Print the top view of the binary tree. You only have to complete the function. My Code: void top_view(Node root) { Node r = root; if(r.left!=null){ top_view(r.left); System.out.print(r.data + " "); } if(r.right!=null){ System.out.print(r.data + " "); top_view(r.right); } } The two if statements are executed every time the function is called, but I need only one of them to execute. I tried switch but its giving constant

Erlang Binary Tree function

天大地大妈咪最大 提交于 2020-05-09 15:51:28
问题 I have a Perfect Binary Tree which every node is represented like that [Value, LeftNode, RightNode] Value is the node value and each LeftNode and RightNode are the Node's sons which are too Binary Trees recursively. And the last nodes (leafs) are represented like that [Value, [], []] example: L1=[4, [], []], L2=[5, [], []], L3=[6, [], []], L4=[7, [], []], L5=[2, L1, L2], L6=[3, L3, L4], Tree=[1,L5 , L6]. so I have the function that returns the last left leaf lastLeftLeaf([H, [], []]) ->H;

Erlang Binary Tree function

大城市里の小女人 提交于 2020-05-09 15:51:26
问题 I have a Perfect Binary Tree which every node is represented like that [Value, LeftNode, RightNode] Value is the node value and each LeftNode and RightNode are the Node's sons which are too Binary Trees recursively. And the last nodes (leafs) are represented like that [Value, [], []] example: L1=[4, [], []], L2=[5, [], []], L3=[6, [], []], L4=[7, [], []], L5=[2, L1, L2], L6=[3, L3, L4], Tree=[1,L5 , L6]. so I have the function that returns the last left leaf lastLeftLeaf([H, [], []]) ->H;

Erlang Binary Tree function

旧巷老猫 提交于 2020-05-09 15:51:20
问题 I have a Perfect Binary Tree which every node is represented like that [Value, LeftNode, RightNode] Value is the node value and each LeftNode and RightNode are the Node's sons which are too Binary Trees recursively. And the last nodes (leafs) are represented like that [Value, [], []] example: L1=[4, [], []], L2=[5, [], []], L3=[6, [], []], L4=[7, [], []], L5=[2, L1, L2], L6=[3, L3, L4], Tree=[1,L5 , L6]. so I have the function that returns the last left leaf lastLeftLeaf([H, [], []]) ->H;