binary-search-tree

convert Sorted Linked List to Balanced BST

别等时光非礼了梦想. 提交于 2019-12-06 11:18:23
问题 I am working on below interview question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. I am trying to understand below solution and its complexity? Can someone help me understand how it works? Is below solution O(n) time complexity and O(log n) space complexity? Also is

Convert Sorted Array to Binary Search Tree (Picturing the Recursion)

杀马特。学长 韩版系。学妹 提交于 2019-12-06 09:02:56
I want to convert a sorted integer array into a binary search tree. I believe I understand how to do this. I have posted my pseudo-code below. What I cannot picture is how the recursion actually works. So if my array is 1, 2, 3, 4, 5... I first make 3 the root of my BST. Then I make 2 the left-child node of 3. Then do I make 1 the left-child node of 2, and come back to 2? I don't get how the recursion steps through the whole process... Thanks in advance, and apologies if this question is poorly explained. I don't want explicit code, but I would really appreciate if someone could help me go

Can a red node have just 1 black child in a red-black tree?

二次信任 提交于 2019-12-06 07:45:23
The rules for a Red-Black Tree: Every node is either red or black. The root is black. Every leaf (NIL) is black. If a node is red, then both its children are black. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. Rule 4 mentions that red nodes need both black childs but what if there is just one child to begin with? Is there an argument to prove or disprove this? No,a red node cannot have one child,consider the following cases:- 1.If the single child it has is red...this cannot happen because no two consecutive nodes can be red. 2.If

Counting the nodes in a binary search tree

核能气质少年 提交于 2019-12-06 07:43:50
I need to create a recursive method that takes as a parameter the root node of a binary search tree. This recursive method will then return the int value of the total number of nodes in the entire binary search tree. This is what I have so far: public class BinarySearchTree<E> extends AbstractSet<E> { protected Entry<E> root; //called by the main method public int nodes() { return nodes(root); } //nodes() will count and return the nodes in the binary search tree private int nodes(Entry<E> current) { if(current.element != null) { if(current.left == null && current.right == null) { if(current

Binary Search Tree to inOrder Array

天涯浪子 提交于 2019-12-06 07:24:21
Pretty easy question: Recursively how can I create an array of a binary search tree (in order) which uses this constructor: public class OrderedSet<E extends Comparable<E>> { private class TreeNode { private E data; private TreeNode left, right; public TreeNode(E el) { data = el; left = null; right = null; } } private TreeNode root; public int size = 0; public OrderedSet() { root = null; } In-Order means you first have to traverse the left part of the tree, so: TreeNode tree // this is your tree you want to traverse E[] array = new E[tree.size]; // the arrays length must be equivalent to the

maximum length of a descending path in a tree which always goes left|right

柔情痞子 提交于 2019-12-06 02:36:54
问题 I'm prepearing for tech interview, so basically learning algorithms from very beginning :) we are given a BST. I need to find the max length of a desc path in it, which always goes left or right In other words, an example tree's descending path is 2, ie 15-10-6 5 / \ 2 15 / 10 / \ 6 14 I'm very new to algorithmic problems.what are my steps to solving this? My idea was to use DFS and a counter to store the longest path. but I can't figure out how to employ recursion to do the job, whereas

Find median in O(1) in binary tree

百般思念 提交于 2019-12-06 02:10:37
问题 Suppose I have a balanced BST (binary search tree). Each tree node contains a special field count , which counts all descendants of that node + the node itself. They call this data structure order statistics binary tree . This data structure supports two operations of O(logN): rank(x) -- number of elements that are less than x findByRank(k) -- find the node with rank == k Now I would like to add a new operation median() to find the median. Can I assume this operation is O(1) if the tree is

How to convert python list of tuples into tree?

泄露秘密 提交于 2019-12-05 20:05:52
I have a list of tuples like list_of_tuples = [(number, name, id, parent_id), (number, name, id, parent_id), ] I am trying to sort it into an ordered structure like: { parent: [(id, name), (id, name)], parent: {parent: [(id, name)] { So, any node could have a parent and/or children I tried with: tree = defaultdict(lambda: [None, ()]) ancestors = set([item[3] for item in list_of_tuples]) for items in list_of_tuples: children_root = {} descendants = [] number, name, id, parent = items if parent is None: tree[id] = [(id, name)] elif parent: if parent not in tree.keys(): node = tree.get(parent)

Is a node in a tree considered its own ancestor?

为君一笑 提交于 2019-12-05 19:21:52
问题 I'm wondering what the consensus is on the definition of "ancestor" in a computer science context. I only ask because in Introduction to Algorithms, Second Edition, p. 259 there is a description of the algorithm Tree-Successor(x) that seems odd. In finding the successor of node x , [...] if the right subtree of node x is empty and x has a successor y , then y is the lowest ancestor of x whose left child is also an ancestor of x . In a binary search tree with a root having key 2 and children 1

Sorted list to complete BST array representation

心已入冬 提交于 2019-12-05 15:06:30
I'm wondering whether there is a mapping between a sorted array (e.g., [1, 2, 3, 4, 5, 6]) and the representation that one obtains when one constructs a complete binary search tree from this sorted array, and expresses said binary search tree as an array (e.g., [4, 2, 6, 1, 3, 5], see graphic below)? 4 2 6 1 3 5 Here's some more context: It is well known that one can take a sorted array and construct a complete binary search tree from it (there is a unique representation). A recursive algorithm is: find the appropriate mid (this is actually quite tricky), treat it as the root, then recurse on