• 【C++】【十一】二叉树递归遍历与非递归遍历的实现及思路


    此文转载自:https://my.oschina.net/u/4260285/blog/4755128

    非递归遍历实现思路:
     

    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    
    typedef struct LINKNODE {
    	struct LINKNODE* next;
    }linknode;
    
    typedef struct LINKLIST {
    	linknode head;
    	int size;
    }stack_list;
    
    #define MY_TRUE 1
    #define MY_FALSE 0
    
    typedef struct BinaryNode {
    	char ch;
    	struct BinaryNode* lchild;
    	struct BinaryNode* rchild;
    }binarynode;
    
    typedef struct BITREESTACKNODE {
    	linknode node;
    	BinaryNode* root;
    	int flag;
    }bitreestacknode;
    
    stack_list* Init_stack_list()
    {
    	stack_list* stack = (stack_list*)malloc(sizeof(stack_list));
    	stack->head.next = NULL;
    	stack->size = 0;
    	return stack;
    }
    
    void Push_stack_list(stack_list* stack, linknode* data)
    {
    	if (stack == NULL) {
    		return;
    	}
    	if (data == NULL) {
    		return;
    	}
    	data->next = stack->head.next;
    	stack->head.next = data;
    	stack->size++;
    }
    
    void Pop_stack_list(stack_list* stack)
    {
    	if (stack == NULL) {
    		return;
    	}
    	if (stack->size == 0) {
    		return;
    	}
    	linknode* pnext = stack->head.next;
    	stack->head.next = pnext->next;
    	stack->size--;
    }
    
    linknode* Top_stack_list(stack_list* stack)
    {
    	if (stack == NULL) {
    		return NULL;
    	}
    	if (stack->size == 0) {
    		return NULL;
    	}
    	return stack->head.next;
    }
    
    int Size_stack_list(stack_list* stack)
    {
    	if (stack == NULL) {
    		return -1;
    	}
    	return stack->size;
    }
    
    void Clear_stack_list(stack_list* stack)
    {
    	if (stack == NULL) {
    		return;
    	}
    	stack->head.next = NULL;
    	stack->size = 0;
    }
    
    void Free_stack_list(stack_list* stack)
    {
    	if (stack == NULL) {
    		return;
    	}
    	free(stack);
    }
    
    
    
    //创建栈中的节点
    BITREESTACKNODE* CreatBitreeStackNode(BinaryNode* node,int flag ) {
    	BITREESTACKNODE* newnode = (BITREESTACKNODE*)malloc(sizeof(BITREESTACKNODE));
    	newnode->root = node;
    	newnode->flag = flag;
    	return newnode;
    }
    //递归遍历
    
    void Recursion(BinaryNode* root) {
    	if (root == NULL) {
    
    		return;
    	}
    	printf("%c", root->ch);
    	//打印左子树
    	Recursion(root->lchild);
    	//打印右子树
    	Recursion(root->rchild);
    }
    //非递归遍历
    void NonRecursion(BinaryNode* root) {
    	stack_list* stack = Init_stack_list();
    	Push_stack_list(stack, (linknode*)CreatBitreeStackNode(root, MY_FALSE));
    
    	while (Size_stack_list(stack) > 0) {
    	
    		//弹出栈顶元素
    		bitreestacknode* node = (bitreestacknode*)Top_stack_list(stack);
    		Pop_stack_list(stack);
    
    		//判断弹出节点是否为空
    		if (node->root == NULL) {
    			continue;
    		}
    
    		if (node->flag == MY_TRUE) {
    			printf("%c", node->root->ch);
    		}
    		else {
    			//当前节点的右节点入栈
    			Push_stack_list(stack, (LINKNODE*)CreatBitreeStackNode(node->root->rchild, MY_FALSE));
    			//当前节点的左节点入栈
    			Push_stack_list(stack, (LINKNODE*)CreatBitreeStackNode(node->root->lchild, MY_FALSE));
    			//当前节点的节点入栈
    			node->flag = MY_TRUE;
    			Push_stack_list(stack, (LINKNODE*)node);
    		}
    	}
    
    
    }
    
    void CreatBinaryTree() {
    	binarynode node1 = { 'A',NULL,NULL };
    	binarynode node2 = { 'B',NULL,NULL };
    	binarynode node3 = { 'C',NULL,NULL };
    	binarynode node4 = { 'D',NULL,NULL };
    	binarynode node5 = { 'E',NULL,NULL };
    	binarynode node6 = { 'F',NULL,NULL };
    	binarynode node7 = { 'G',NULL,NULL };
    	binarynode node8 = { 'H',NULL,NULL };
    
    	//建立节点关系
    	node1.lchild = &node2;
    	node1.rchild = &node6;
    	node2.rchild = &node3;
    	node3.lchild = &node4;
    	node3.rchild = &node5;
    	node6.rchild = &node7;
    	node7.lchild = &node8;
    	printf("Recursion traverse:
    ");
    	//递归遍历
    	Recursion(&node1);
    	printf("
    ");
    	printf("Non_Recursion traverse:
    ");
    	//非递归遍历
    	NonRecursion(&node1);
    
    	printf("
    ");
    }
    
    int main()
    {
    	CreatBinaryTree();
    	system("pause");
    	return 0;
    }
    

       

    更多内容详见微信公众号:Python测试和开发

    Python测试和开发

  • 相关阅读:
    Leetcode Spiral Matrix
    Leetcode Sqrt(x)
    Leetcode Pow(x,n)
    Leetcode Rotate Image
    Leetcode Multiply Strings
    Leetcode Length of Last Word
    Topcoder SRM 626 DIV2 SumOfPower
    Topcoder SRM 626 DIV2 FixedDiceGameDiv2
    Leetcode Largest Rectangle in Histogram
    Leetcode Set Matrix Zeroes
  • 原文地址:https://www.cnblogs.com/phyger/p/14048146.html
Copyright © 2020-2023  润新知