There is another, well-known way to create a binary tree from a shared tree without “connection nodes”. This method is best understood as follows:
Node{ Node{ data; data; first_child; => left; next_sibling; right; } }
Basically, this is a list of children of a common tree in the form of a linked list with the addition of each node that has a link to a linked list of its children. As you can see, this is structurally equivalent to a binary tree.
So, in pseudo-code (with extreme cases omitted for simplicity)
BinaryTree(gtree){ root=BinaryNode(gtree.data,BinaryNode(gtree.children),null); } BinaryNode(List<gnode> sibs){ BinaryNode(sibs.first.data,BinaryNode(sibs.first.children),BinaryTree(sibs.rest)); } BinaryNode(data,left,right){ data=data; left=left; right=right; }
Of course, if you need a structure that you described, it would be useless, but overall this is a pretty good way to create a binary tree from a shared tree.
source share