| | |
how to print null subtree of a null subtree?
Thread Solved
![]() |
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
Hi everybody,
for example I have such a tree in c++:
23
/ \
1 45
\ / \
2 31 52
\ \
3 234
\
5
I can print this tree in this way by using breadthFirst-level method:
23,
1, 45
2, 31, 52
3, 234
5
But how can I print this tree like this:
23
1,45
-,2 | 31,52
-,- | -,3 | -,- | -,234
-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-
At the last two line it is necessary to print null subtree of a null subtree by "-"
Please help me...
for example I have such a tree in c++:
23
/ \
1 45
\ / \
2 31 52
\ \
3 234
\
5
I can print this tree in this way by using breadthFirst-level method:
23,
1, 45
2, 31, 52
3, 234
5
But how can I print this tree like this:
23
1,45
-,2 | 31,52
-,- | -,3 | -,- | -,234
-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-
At the last two line it is necessary to print null subtree of a null subtree by "-"
Please help me...
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
The actual tree structure is below:
C++ Syntax (Toggle Plain Text)
23 / \ 1 45 \ / \ 2 31 52 \ \ 3 234 \ 5
•
•
•
•
But how can I print this tree like this:
23
1,45
-,2 | 31,52
-,- | -,3 | -,- | -,234
-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-
At the last two line it is necessary to print null subtree of a null subtree by "-"
C++ Syntax (Toggle Plain Text)
[-] [-][-] [-][-][-][-] [-][-][-][-][-][-][-][-] [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-]
[23] [-][-] [-][-][-][-] [-][-][-][-][-][-][-][-] [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-] [23] [1][-] [-][-][-][-] [-][-][-][-][-][-][-][-] [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-] [23] [1][-] [-][2][-][-] [-][-][-][-][-][-][-][-] [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-] [23] [1][-] [-][2][-][-] [-][-][-][3][-][-][-][-] [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-] [23] [1][-] [-][2][-][-] [-][-][-][3][-][-][-][-] [-][-][-][-][-][-][-][5][-][-][-][-][-][-][-][-] [23] [1][45] [-][2][-][-] [-][-][-][3][-][-][-][-] [-][-][-][-][-][-][-][5][-][-][-][-][-][-][-][-] [23] [1][45] [-][2][31][-] [-][-][-][3][-][-][-][-] [-][-][-][-][-][-][-][5][-][-][-][-][-][-][-][-] [23] [1][45] [-][2][31][-] [-][-][-][3][-][234][-][-] [-][-][-][-][-][-][-][5][-][-][-][-][-][-][-][-] [23] [1][45] [-][2][31][52] [-][-][-][3][-][234][-][-] [-][-][-][-][-][-][-][5][-][-][-][-][-][-][-][-]
At a glance, I don't see any problems with the logistics and the code should be pretty simple. But I can't say for sure unless I try it myself. Last edited by Ravalon; Jan 6th, 2007 at 8:27 pm.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
Like Ravalon I've never done this before either. My thought would be if you aren't comfortable developing sparse arrays you might be able to mimic the sparse array protocol proposed using a full binary tree of the same height as the original tree instead.
To do this I'd first determine the length of the longest path in the original tree, which should be the same as the height of the original tree. Then I'd use the height of the original tree to develop a full binary tree of the same height, setting each node to a default value. Then I'd traverse the original tree and copy the values of each node in the original tree into the respective nodes of the full binary tree. Lastly, I'd do a level order traversal of the full binary tree printing out a hyphen if the value of the node is the default value and the non default value of the node if it's not the default value.
To do this I'd first determine the length of the longest path in the original tree, which should be the same as the height of the original tree. Then I'd use the height of the original tree to develop a full binary tree of the same height, setting each node to a default value. Then I'd traverse the original tree and copy the values of each node in the original tree into the respective nodes of the full binary tree. Lastly, I'd do a level order traversal of the full binary tree printing out a hyphen if the value of the node is the default value and the non default value of the node if it's not the default value.
Last edited by Lerner; Jan 6th, 2007 at 9:21 pm.
If each node in a tree contains exactly two references, the tree is called a binary tree. Because reference can be null, each node in a binary tree can have 0, 1, or 2 successor nodes. Example:
A binary tree is full if all of its leaves are on the same level, and all internal nodes have exactly two children. A full binary tree with height h has
nodes. A binary tree is complete if all the "holes" are on the right side of the deepest level.
A binary tree can also be stored in an array in the following way: first, put the root in index 0, then, for every node in index i, put its left child at
, and its right child at
. In this way, the above binary tree will be represented as
The "_" in the array is a special value indicating nonexistent values, or "holes". The holes of course will be "null". If the binary tree is complete, then there is no "hole" when it is stored in an array between nodes.
Does that give you any ideas?
C++ Syntax (Toggle Plain Text)
A / B / \ E C \ / \ F G D
A binary tree is full if all of its leaves are on the same level, and all internal nodes have exactly two children. A full binary tree with height h has
A binary tree can also be stored in an array in the following way: first, put the root in index 0, then, for every node in index i, put its left child at
C++ Syntax (Toggle Plain Text)
{A, B, _, E, C, _, _, _, F, G, D}
The "_" in the array is a special value indicating nonexistent values, or "holes". The holes of course will be "null". If the binary tree is complete, then there is no "hole" when it is stored in an array between nodes.
Does that give you any ideas?
*Voted best profile in the world*
Just a quick example let's use the same tree
Ok let us consider the node
. We know
is on the right hand side of
.
So by our rule
will occupy the array of index
where
will obviously be
. Because that's the position where E was in our array.
Thus...
Therefore
must occupy index
. And of course it does.
You just go through each level of the tree as you build the array.
C++ Syntax (Toggle Plain Text)
A / B / \ E C \ / \ F G D
{A, B, _, E, C, _, _, _, F, G, D}
| | | | | | | | | | |
0 1 2 3 4 5 6 7 8 9 10 <-index positionsOk let us consider the node
So by our rule
Thus...
Therefore
You just go through each level of the tree as you build the array.
Last edited by iamthwee; Jan 7th, 2007 at 7:17 am.
*Voted best profile in the world*
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
But B will occupy the array of index 2i+1 where http://www.daniweb.com/cgi-bin/mimetex.cgi?i will obviously be 1 so index B will be 3 Because that's the position where A was in our array. But in your example its index was 1.
Also in this I could not print null subtree of null subtree?
Please help meee
Also in this I could not print null subtree of null subtree?
Please help meee
![]() |
Other Threads in the C++ Forum
- Previous Thread: Wow good book.
- Next Thread: graphics in c++
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets







(