Is there a way to convert from a general tree to binary SEARCH tree?
NickName:Captain Man Ask DateTime:2013-04-18T23:24:00

Is there a way to convert from a general tree to binary SEARCH tree?

I know how to convert from a general tree to a binary tree just fine,

      a             a
    / | \          /
   b  c  d   ->   b
                   \
                    c
                     \
                      d

I was just asked how to convert from a general tree to a binary search tree though. My thoughts are that the person who asked me either didn't mean binary search tree (I asked him, he said he did), or he's misunderstanding something from his class notes. In any case, has anyone heard of doing this? General tree to binary search tree? The answer I gave him was first convert to a binary tree then sort it to get a binary search tree. Is this correct?

Copyright Notice:Content Author:「Captain Man」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16086958/is-there-a-way-to-convert-from-a-general-tree-to-binary-search-tree

Answers
dragos2 2013-04-18T15:29:12

I think you just have to traverse the initial tree and insert each node into a binary search tree. After that, you will have converted your initial tree into a BST.\n\nFor traversing the tree click here\n\nFor binary search tree info and insertion method click here",


user5096772 2015-11-25T23:20:09

**\n\n\n This is an algorithm I created that can convert a tree data structure\n into a 2 degree tree true (Binary tree). Following is the node\n structure for the tree nodes.\n\n\n**\n\ntemplate<class T>\nclass Node\n{\npublic:\n T _data; // The Node Data\n\n std::vector<Node<T>*> _link; // Possible OutLinks from the Node.\n\n Node(){}\n\n void setValue(T D){ _data = D; }\n\n T getValue()const{ return _data; }\n\n ~Node(){}\n};\n\nFollowing is the tree adapter.\n\ntemplate <class T> \n\nclass tree{\n\n Node<T>*_root; // The Pointer holding the root node.\n\n int _degree;\n\n std::string _type; // shows the tree type,Binary Or Any other outdegree \n\n//degrees.\n\npublic:\n\n tree(){ _degree = 0; _root = NULL; }\n\n tree(Node<T>*R, int D) :_root(R),_degree(D){}\n\n\n Node<T>* getRoot()const{ return _root; }\n\n ~tree(){ _root = NULL; }\n};\n\n//.........\n//This is the Algorithm for converting a x-order tree to a binary tree.\n\n///*This Template function is used to convert a general tree into a binary tree \n\nwithout loosing any nodes,with the same root node.*/\n\ntemplate<class T> \n\ntree<T> makeBinaryTree(tree<T> _tree){\n\n Node<T>* node = _tree.getRoot();\n\n Node<T>* root = new Node<T>;\n\n root = node;\n\n\n int i = 0;\n\n int k = 0;\n\n std::queue<Node<T>*> que; // que used to save the links other than the \n\nleftmost one.\n\n std::stack<Node<T>*> s1; // stack for saving the tree nodes,while going deep \ninto left\n\n std::stack<char>s2;\n\n Node<T>* s3;\n\n char flag = ' ';\n\n while (true){\n\n while (root&&flag!='A'){\n\n s1.push(root);\n\n if (root->_link[0] == NULL && root->_link.size())\n\n s2.push('C');\n\n else if (root->_link.size() > 1){\n\n s2.push('B');\n\n }\n\n else\n\n s2.push('A');\n\n root = root->_link[0];\n\n }\n\n if (s1.empty())break;\n\n root = s1.pop();\n\n flag = s2.pop();\n\n\n if (flag == 'C'){ // handles the deep left node with any number of nodes \n\nother than in socket 0.\n\n while (true){\n\n i = 1;\n\n if (root->_link[0] == NULL&&root->_link.size() == 0){ flag = 'A'; break; \n\n}\n if (root->_link.size() >= 1){\n\n while (true)\n\n {\n\n if (root->_link[i]){\n\n root->_link[0] = root->_link[i];\n\n root->_link.erase(i);\n\n if (root->_link.size() > 1){\n\n s1.push(root);\n\n s2.push('B');\n\n }\n\n break;\n\n }\n\n ++i;\n\n }\n\n root = root->_link[0];\n\n }\n\n }\n\n }\n\n\n if (flag == 'B'){ // any node except the deep left node that has more links \n\nfrom it.\n\n i = root->_link.size()-1;\n\n k = i-1;\n\n while (K!=0){\n\n root->_link.at(k)->_link.push(root->_link.at(k + 1));\n\n --k;\n\n }\n\n s3->_link[1] = root->_link[1];\n\n root->_link.erase[1];\n\n s1.push(root);\n\n s2.push('A');\n // Now You have to manage link 1 of s3.\n\n s3 = s3->_link[1];\n\n\n\n if (s3->_link.size()){\n\n //TODO...\n\n //Testing...\n\n root = s3;\n\n }\n\n //AT the end \n\n s3 = NULL;\n\n\n\n }\n\n\n\n\n\n if (flag == 'A'){ // the safe nodes,i.e having only one node from it \n\n,other than the deep left node\n\n s3 = root;\n\n }\n\n\n\n }//end of main while loop.\n\nreturn (new tree<T>(node,2));\n\n}\n",


More about “Is there a way to convert from a general tree to binary SEARCH tree?” related questions

Is there a way to convert from a general tree to binary SEARCH tree?

I know how to convert from a general tree to a binary tree just fine, a a / | \ / b c d -&gt; b \ c ...

Show Detail

How do I create a Binary Search Tree from a General tree

As the title states. I am trying to create a binary search tree from a general tree that I have created. The code for my general node class is: Node&lt;E&gt; parent; E data; ArrayList&lt;Node&lt...

Show Detail

convert a not-full binary search tree to full binary search tree

I have a trouble in full binary search tree How can I convert a a not-full binary search tree to full binary search tree, such as this example: I have a structure of Node in tree: struct Node { ...

Show Detail

Binary tree to general tree

I know that from a general tree you can construct a unique binary tree, but is the reverse true? i.e can you get a unique general tree from a binary tree?

Show Detail

Convert General "Family" Tree to Binary Tree (Not BST)

I am currently considering different ways to accomplish the task of converting a family-type general tree (where each parent can have as many children as necessary) to a binary tree such that the l...

Show Detail

convert Binary tree to Binary Search Tree inplace using C

Without using any extra space convert Binary Tree to Binary Search tree.I came up with the following algo but It doesn't work. BTtoBST(node *root) 1.if the root is NULL return 2.else current=roo...

Show Detail

Binary tree to Binary Search Tree (BST)

How can you convert Binary Tree to Binary Search Tree with O(1) extra space ?

Show Detail

Convert a General tree to a binary tree - XML Parser

im trying to convert a general tree (unlimited child nodes) associate to an XML file named "pays.xml" like this: &lt;?xml version="1.0" encoding="iso-8859-1"?&gt; &lt;country&gt; &l

Show Detail

Binary Tree, Binary Search Tree, Binary search

I am trying to understand the Binary tree and referring to online material , and questions asked in SO. I understood that: Binary tree -> Tree in which each node can have at most 2 nodes. Binary

Show Detail

How to design a general Node for a Binary Search Tree?

My design problem is that I'm trying to write a general class that would fit several possible subtypes. Specifically, I'm trying to write a Node class for a binary search tree, that would fit several

Show Detail