I was looking at an example of a binary tree in an adjacency list format and I couldn't figure out what the binary tree equivalent looks like. There was a representation but the ordering didn't make sense e.g. left/right/parent.
Searching didn't yield any results of how to figure out a binary tree from an adjacency list.
Given some adjacency list like the following where each index i is the vertex and the unordered lists are the vertex i neighbors [[5,7,9], [2], [1,5,6]] (not sure if this is a binary tree, just wrote it to my best guess) how do you construct a binary tree?
Related
I am a beginner in the field of data structures, I am studying binary trees and in my textbook there's a tree which is not a binary tree but I am not able to make out why the tree is not a binary tree because every node in the tree has atmost two children.
According to Wikipedia definition of binary tree is "In computer science, a binary tree is a treedata structure in which each node has at most two children, which are referred to as the left child and the right child."
The tree in the picture seems to satisfy the condition as mentioned in the definition of binary tree.
I want an explanation for why the tree is not a binary tree?
This is not even a tree, let alone binary tree. Node I has two parents which violates the tree property.
I got the answer, This not even a tree because a tree is connected acyclic graph also a binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees called the left and right subtrees of the original tree.
Here the word disjoint answers the problem.
It's not a binary tree because of node I
This can be ABEI or ACFI
This would mean the node can be represented by 2 binary numbers which is incorrect
Each node has either 0 or 1 parents. 0 in the case of the root node. 1 otherwise. I has 2 parents E and F
A splay tree is a type of self-adjusting binary search tree. Inserting a node into a splay tree involves inserting it as a leaf in the binary search tree, then bringing that node up to the root via a "splay" operation.
Let us say a binary search tree is "splay-constructible" if the tree can be produced by inserting its elements into an initially empty splay tree in some order.
Not all binary search trees are splay-constructible. For example, the following is a minimal non-splay-constructible binary search tree:
What is an efficient algorithm that, given a binary search tree, determines whether it is splay-constructible?
This question was inspired by a related question regarding concordance between AVL and splay trees.
More details: I have code to generate a splay tree from a given sequence, so I could perform a brute-force test in O(n! log(n)) time or so, but I suspect polynomial time performance is possible using some form of dynamic programming over the tree structure. Presumably such an algorithm would exploit the fact that every splay-constructible tree of size n can be produced by inserting the current root into some splay-constructible tree of size n-1, then do something to take advantage of overlapping/isomorphic subproblems.
Looking
into representations of graphs, the representation with
objects&pointers
is favorable to the adjacency-list representation and adjacency-matrix representation
when the graph is a tree of type
n-ary, binary (ordinary binary, balanced-- red-black, AVL, etc) trie, b-tree, ... , and obviously not a heap.
This is because:
1.) in a tree structure, the edges have distinct meanings-- "left-child",
"right-child", "parent", "i-th child" in b-trees,
the link value in tries/radix trees, etc.
Tree operations require the traversal of a specific link-- based on the link types
(left-, right-, .. i-th-child, etc).
The use of pointers can easily allow to distinguish the link types.
Adjacency list representation would require an extra field on the
link-node to tell what kind of a link it is.
Matrix representation would require coded matrix entries to tell what kind of a link it is.
2.) Tree operations often require moving around the subtrees of a node.
This can easily be done,
in constant time, by changing the pointer values.
In an adjacency list and matrix representations,
this operation would take searching for the link-to-be altered
in the edge-list of the nodes and is linear in the number of edges or the graph degree-- no longer in O(1).
The object&pointer representation of a tree can be backed by array structures:
So, each vertex is
still represented by an object. This object has a pointer to each child and one
to the parent (whichever the way the tree being implemented calls for).
But this time, the objects are array entries, and the pointer values are array indices. This is a mix of adjacency-list and objects&pointers representations of graphs
and is powerful.
My Q is: What other general cases are there in the representations of graphs
where the representation with objects&pointers is preferable to the adjacency-list
representation and to the the adjacency-matrix
representation?
I'm looking for references to this.
Thanks in advance.
I have two binary trees and I want to merge them.
My first question is that whether we can merge two binary trees and if yes how efficiently I can perform the merge operations and what are the various ways i can perform the merging operations. ..?
1) Flatten both the trees in to sorted lists.
2) Merge the lists from what you got in 1)
3) Construct the tree out of what you got in 2)
This algorithm might help you.
Not considering efficiency this answer may work Algorithm of combining two binary trees? . If sorted or balanced, discussion on efficiency in How to merge two BST's efficiently? and Concatenating/Merging/Joining two AVL trees
Create a new node and point one tail at the head of one of the trees and point the other tail at the head of the other tree. Perhaps you need to clarify your question to be more specific. What relationships are you trying to preserve?
A tree is also a graph, so output the edge vertex-pairs (u,v) for each tree, and then union these edge sets, and output the resulting graph.
The issue arises with how to map vertices in the one tree to vertices in the other (e.g. we have edge pair (5,9) in tree 1 and edge pair (5,6) in tree 2, do these 5s correspond to the same vertex ? ).
Coming up with a numbering of vertices (perhaps that assigns numbers to each vertex in an incomplete binary tree, as if it were a complete binary tree, so in other words assigns the vertices in any partial binary tree to slots of a hypothetical complete binary tree of which that tree is a subtree), that somehow provides an desirable equivalence is something that works.
How can you convert Binary Tree to Binary Search Tree with O(1) extra space ?
Converting an unordered binary tree into an ordered binary search tree is trivial, but a bit more difficult to do fast.
Here's a naive implementation that should satisfy your criteria, I will not describe the actual steps to take, just the overall algorithm.
Grab a random leaf node from your existing tree
Unlink the leaf node from your existing tree
Make the node the root of your new binary search tree
Grab another random leaf node from your existing tree
Unlink that node from your existing tree
Find the right spot for, and link the node, into your new binary search tree
Repeat step 4-6 until the original tree is empty
You should require only a few variables, like the parent of the leaf node you're unlinking (unless the nodes has parent-links), the root node of the new tree, and a couple of temporary variables, all within your O(1) space criteria.
This will not produce an optimal binary search tree. For that you need to either sort the nodes before adding them, and adding them in the right order, or use a balancing binary search tree, like a red-black tree or a splay tree.
Convert Binary Tree to a doubly linked list- can be done inplace in O(n)
Then sort it using merge sort, nlogn
Convert the list back to a tree - O(n)
Simple nlogn solution.