Given a preorder traversal of a binary tree, how many possible binary trees are there


title: Given the preorder traversal of a binary tree, how many possible binary trees are there
date: 2023-05-16 11:42:26
tags: Data Structures and Algorithms


  • Given a preorder traversal of a binary tree, how many possible binary trees are there

    **Question:** How many possible binary tree
    git addresses are there for a given binary tree preorder traversal: https://github.com/944613709/HIT-Data-Structures-and-Algorithms

    Algorithm idea:

    In the binary tree pre-order traversal non-recursive algorithm, the pre-order traversal sequence of the binary tree is the stacking order of the binary tree nodes, and the in-order traversal sequence of the binary tree is the stacking order of the binary tree nodes. The pre-order traversal sequence and inorder of the binary tree are known A binary tree can be determined by traversing the sequence, so the problem is equivalent to how many possible popping orders are there for the stacking order of the known binary tree nodes.

    Use a queue to simulate input, and the output of the queue is in the order of the original sequence. Then use a stack to simulate stacking and popping, and the result is saved in another queue

    For any element k, either push it into the stack to process the next element, or judge whether the stack is empty or not, then pop it out and continue processing the element. When all the elements are put into the stack, when k=n+1, the elements of the queue and the stack can be output to become a pop-up sequence, the count is increased by one, and then backtracking and recursion are performed.

    Algorithm steps:

    \1. Initialize stack and queue

    \2. Call OutPut_S(1); function execution starts from k=1

    (1) If k! =n+1, need to continue recursion

    ① Push the current element k onto the stack

    ② Process the next element k+1 and call OutPut_S(k+1);

    ③ Go back to the state of the previous element using pop(s)

    ④ Determine whether the stack is empty, if empty

    \1) The top element of the stack is popped out of the stack

    \2) Store in queue

    \3) Call OutPut_S(k) again; process the current element k

    \4) dequeue

    \5) Push the element at this time into the stack, perform a backtracking operation, that is, erase the previous processing, and recursively process other elements

    (2) If k==n+1, the recursion is completed, and all elements have been loaded into the stack, as an exit

    ① Print queue elements

    \1) While until p->next!=NULL

    a. Every printf and p=p->next;

    ② Print stack elements

    \1) Use for loop to printf all elements

    ③ And count using cout++

    test sample

    img

    imgimg

    specific code

  `\#include <stdio.h>`

  `\#include <stdlib.h>`

  `\#include <string.h>`

  `\#include <stdbool.h>`

  `\#define MAX 99`

  `` 

  `typedef struct STACK`

  `{`

    `int top;`

    `int data[MAX];`

  `}STACK;`

  `void push(STACK *s,int data)`

  `{`

    `s->data[++s->top]=data;`

  `}`

  `void pop(STACK *s)`

  `{`

    `s->top--;`

  `}`

  `void STACK_Initial(STACK *s)`

  `{`    

    `s->top=-1;`

  `}`

  `` 

  `int top(STACK *s)`

  `{`

    `return s->data[s->top];`

  `}`

  `bool Isempty(STACK *s)`

  `{`

    `if(s->top==-1)`

  ​    `return true;`

    `else`

  ​    `return false;`

  `}`

  `typedef struct Qnode`

  `{`

    `int data;`

    `struct Qnode* next;`

  `}Qnode;`

  `` 

  `typedef struct Queue`

  `{`

    `Qnode* front;`

    `Qnode* rear;`

  `}queue;`

  `void Q_Initial(queue *q)`

  `{`

    `Qnode *p=malloc(sizeof(Qnode));`

    `p->next=NULL;`

    `q->front=p;`

    `q->rear=p;`

  `}`

  `void enqueue(queue *q,int k)`

  `{`

    `Qnode *p=malloc(sizeof(Qnode));`

    `p->data=k;`

    `p->next=NULL;`

    `q->rear->next=p;`

    `q->rear=p;`

  `}`

  `void dequeue(queue *q)`

  `{`

    `Qnode *p=q->front->next;`

    `q->front->next=p->next;`

    `free(p);`

    `if(q->front->next==NULL)`

  ​    `q->rear=q->front;`

  `}`

  `queue *q; //队列保存已出栈元素`

  `int n;`

  `int count=0;`

  `STACK *s;`

  `` 

  `void OutPut_S(int k)`

  `{`

    `int temp;`

    `if(k!=n+1)//当k!=n+1时候继续递归` 

    `{`

  ​    `push(s,k);` 

  ​          `//当前元素k入栈`

  ​    `OutPut_S(k+1);` 

  ​          `//处理下一元素k+1`

  ​    `pop(s);  //回溯至当前元素状态`

  ​    `if(!Isempty(s)) //当栈非空时`

  ​    `{`

  ​      `temp=top(s);`

  ​      `pop(s);`

  ​      `enqueue(q,temp);` 

  ​      `OutPut_S(k);`

  ​      `dequeue(q);`

  ​      `push(s,temp);`

  ​    `}`

    `}`

    `else if(k==n+1)` 

    `{//当k==n+1时候结束递归` 

  ​    `Qnode* p=q->front;`

  ​    `while(p->next!=NULL)`

  ​    `{    //将队列元素打印——已经出栈的元素` 

  ​      `printf("%d ",p->next->data);` 

  ​      `p=p->next;`

  ​    `}`

  ​    `int j;`

  ​    `//然后再打印栈的元素 ,然后回溯` 

  ​    `for(j=s->top;j>=0;j--)`

  ​      `printf("%d ",s->data[j]);` 

  ​    `count++; //计数共有几种可能`

  ​    `printf("\n");`

  ​    `return ;`

    `}`

  ``  

  `}`

  `` 

  `int main()`

  `{`

    `s = malloc(sizeof(STACK));`

    `STACK_Initial(s);`

    `q = malloc(sizeof(queue));`

    `Q_Initial(q);`

    `printf("请输入先序序列有多少个元素\n");`

  ​    `scanf("%d",&n);`

    `OutPut_S(1);`

    `printf("共有%d种可能",count);`

    `return 0;`

  `}`

  `` 

Guess you like

Origin blog.csdn.net/qq_35798433/article/details/130700814