How to build a binary tree and four traversal methods

How to build a binary tree and four traversal methods

1. First of all, we need to know what the nature of a binary tree is

On the binary tree, a parent can have only two children . Suppose the parent node a has a left child node b and a right child node c, then the value of the left child node is less than the parent node, and the value of the right child node is greater than or equal to the parent node , that is, b<a≤c.

2. How to build a binary search tree?

Given a sequence of length n, the N values ​​in the sequence are not repeated
Build a binary search tree, and use four methods to traverse

Test Data

in:
7
5 3 4 2 7 8 6

  • We can read it node by node, the first is 5, at this time the binary tree is empty, so we take it as the root node of the binary tree;
  • Then it is 3, first compare with the root node, 3 <5, so 3 is on the left subtree of 5, since the left subtree of 5 is empty, so 3 is used as the left child of 5;
  • Then it is 4, first compare with 5, 4<5, so 4 is on the left subtree of 5, and the left subtree of 5 is not empty at this time, then 4 is compared with the left child of 5, 4>3, so 4 To be placed on the right subtree of 3, since the right subtree of 3 is empty, 4 is regarded as the right child of 3
  • By analogy, a binary search tree can be built

3. Pre-order traversal

After traversing, output the root node first, then output the left child node, and finally output the right child node

Preorder traversal:
5 3 2 4 7 6 8

Using the nature of recursion, the root node is first found. At this time, the value of the root node is directly output, and then the left child node is regarded as the root node to continue recursive traversal, and finally the right child node is regarded as the root node. First order.

4. In-order traversal

After traversing, output the left child node first, then output the root node, and finally output the right child node

Mid-order traversal:
2 3 4 5 6 7 8

On the basis of the current node, the left subtree is traversed first, and the value of the current node is output after the left subtree is traversed, and then the right subtree is traversed.

5. Post-order traversal

After traversing, output the left child node first, then output the right child node, and finally output the root node

Post-order traversal:
2 4 3 6 8 7 5

On the basis of the current node, first traverse the left subtree, then traverse the right subtree, and output the value of the current node after the traversal.

6. Summarize the above three traversal laws

In fact, the recursive call sequence is the same, but the timing of outputting the value of the current node is different. But it is regular. Whenever the root node is traversed, the current node value is output (because the current node is the root node).

Preorder: Root (output) left and right

Middle order: left root (output) right

Post sequence: left and right roots (output)

7. Sequence traversal

Sequence traversal uses the breadth-first search idea to traverse the binary tree: first put the root node into the queue, then the head of the queue is out of the queue, output the value, and then put its left and right child nodes into the queue in turn, and then take the head of the queue out of the queue. Output value, put its left and right child nodes into the queue... and so on.

**Must know the nature of the queue: **First in, first out

撸Code:

/*
给出一个长为 n 的序列,序列里面的N个值不重复
建立二叉搜索树,并且用四种方法遍历 
	
测试数据

in: 
7
5 3 4 2 7 8 6 

out:
先序遍历:
5 3 2 4 7 6 8
中序遍历:
2 3 4 5 6 7 8
后序遍历:
2 4 3 6 8 7 5

*/
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;

struct node {
    
    
	int val;//当前节点的值
	node *lch,*rch;//左  右子树结构体指针  (指向结构体的指针变量)
};

node *insert(node *p,int x) {
    
     //传进来一个结构体地址 以及要插入的值
	if(p==NULL) {
    
    
		node *q=new node;//新节点,新的建立结构体
		q->val=x;
		q->lch=q->rch=NULL;
		return q;
	} else {
    
    
		if(x<p->val) p->lch=insert(p->lch,x);//递归 直到找到可以插入的位置; 原则就是 左子节点小于根节点,根节点大于等于右子节点 
		else p->rch=insert(p->rch,x);
		return p;
	}
}

void find1(node *p) {
    
     //先序遍历  根-左-右
	if(p!=NULL) {
    
    
		printf("%d ",p->val);
		find1(p->lch);
		find1(p->rch);
	}
}

void find2(node *p){
    
    //中序遍历  左-根-右 
	if(p!=NULL){
    
    
		find2(p->lch);
		printf("%d ",p->val);
		find2(p->rch);
	}
}

void find3(node *p) {
    
     //后序遍历 左-右-根 
	 if(p!=NULL) {
    
    
		find3(p->lch); 
		find3(p->rch);
		printf("%d ",p->val);
	 }
}

/*层序遍历:节点按层从上到下输出,同一层的节点从左到右输出*/
void find4(node *p) {
    
     
	queue<node*>Q; //队列中存放结构体的地址 
	Q.push(p) ;
	while(!Q.empty()){
    
    
		p=Q.front();
		Q.pop();
		printf("%d ",p->val);
		if(p->lch!=NULL){
    
    
			Q.push(p->lch);
		}
		if(p->rch!=NULL){
    
    
			Q.push(p->rch);
		}	
	}
}


int main() {
    
     
	int n;
	scanf("%d",&n);
	node *p=NULL;
	int data;
	for(int i=0; i<n; i++) {
    
    
		scanf("%d",&data);
		p=insert(p,data);//将 data 插入到二叉搜索树
	}
	printf("先序遍历:\n");
	find1(p);//先序遍历
	printf("\n");
	
	printf("中序遍历:\n");
	find2(p);//中序遍历
	printf("\n");	
	
	printf("后序遍历:\n");
	find3(p);//后序遍历
	printf("\n");
	
	printf("层序遍历:\n");
	find4(p);
	printf("\n");
	return 0;
}

Guess you like

Origin blog.csdn.net/DREAM_yao/article/details/109330077