无法以相反的顺序在给定级别打印BST元素

时间:2012-10-14 07:51:18

标签: python algorithm binary-search-tree

我正在尝试编写一个代码,给定二进制搜索树根和一个级别,在该级别打印出树的元素。这很好用:

def myprint(root,level):
    if root:
        if not level:
            print root.data,
        else:
            myprint(root.left,level-1)
            myprint(root.right,level-1)

然而,当我尝试调整它以相反顺序打印元素时,它不起作用。对于以下树:

                         26
                    /          \
                   13          39
                  /  \        /  \
                 6    19     32   51
                / \   / \    / \  / \
               4   8  14    31 33   68
                        \
                         17

如果我想从右到左输出级别3(根级别为0)的元素,则输出应为68 33 31 14 8 4。上面的代码正确反转,即打印出4 8 14 31 33 68。但是下面的代码没有正确打印相反的顺序,而是打印出31 33 68 4 8 14代替:

def revprint(root,level):
    if root:
        if not level:
            print root.data,
        else:
            myprint(root.right,level-1)
            myprint(root.left,level-1)

有人能发现错误,并告诉我如何纠正错误吗?初始化树的代码如下:

class tree:
    def __init__(self,data):
        self.data = data
        self.successor,self.left,self.right = None,None,None
    def push(self,data):
        root = self
        while root:
            oldroot = root
            if root.data > data:
                root = root.left
            elif root.data < data:
                root = root.right
        if data > oldroot.data:
            oldroot.right = tree(data)
        else:
            oldroot.left = tree(data)

a = tree(26)
for x in [13,39,6,19,4,8,5,10,9,14,17,15,32,51,68,31,33,36,34]:
a.push(x)

2 个答案:

答案 0 :(得分:2)

Here my code prints the tree level by level as well as upside down

int counter=0;// to count the toatl no. of elments in the tree


void tree::print_treeupsidedown_levelbylevel(int *array)
{
int j=2;  
int next=j;
int temp=0;
while(j<2*counter)
{
    if(array[j]==0)
    break;

    while(array[j]!=-1)
    {
        j++;
    }

    for(int i=next,k=j-1 ;i<k; i++,k--)
    {
        temp=array[i];
        array[i]=array[k];
        array[k]=temp;
    }

    next=j+1;
    j++;
}

for(int i=2*counter-1;i>=0;i--)
{
    if(array[i]>0)
    printf("%d ",array[i]);

    if(array[i]==-1)
    printf("\n");
}
} 

void tree::BFS()
{
queue<node *>p;

node *leaf=root;

int array[2*counter];
for(int i=0;i<2*counter;i++)
array[i]=0;

int count=0;

node *newline=new node; //this node helps to print a tree level by level
newline->val=0;
newline->left=NULL;
newline->right=NULL;
newline->parent=NULL;

p.push(leaf);
p.push(newline);

while(!p.empty())
{
    leaf=p.front();
    if(leaf==newline)
    {
        printf("\n");
        p.pop();
        if(!p.empty())
        p.push(newline);
        array[count++]=-1;
    }
    else
    {
        cout<<leaf->val<<" ";
        array[count++]=leaf->val;

        if(leaf->left!=NULL)
        {
            p.push(leaf->left);
        }
        if(leaf->right!=NULL)
        {
            p.push(leaf->right);
        }
        p.pop();
    }
}
delete newline;

print_treeupsidedown_levelbylevel(array);
}


 Here in my code the function BFS prints the tree level by level, which 
 also fills the data in an int array for printing the tree upside down.
(note there is a bit of swapping is used while printing the tree upside down 
 which helps to achieve our goal). 
 if the swaping is not performed then for a tree like

                     8
                   /  \
                  1    12
                  \     /
                   5   9
                 /   \
                4     7
                     /
                    6
  o/p will be
  6
  7 4
  9 5
  12 1
  8

  but the o/p has to be
  6
  4 7
  5 9
  1 12
  8

  this the reason why swapping part wass needed in that array.

答案 1 :(得分:1)

您正在myprint中呼叫revprintrevprint的固定版本:

def revprint(root,level):
    if root:
        if not level:
            print root.data,
        else:
            revprint(root.right,level-1)
            revprint(root.left,level-1)