Algorithm of binary tree various traversal

Source: Internet
Author: User
Tags printf requires

Tree structure is a kind of important nonlinear data structure, in which tree and two-fork trees are most commonly used.

A binary tree is an ordered tree with a maximum of two subtrees per node. Usually the root of the subtree is referred to as the left subtree and right subtree. Binary trees are often used as binary search trees and two-fork or binary-ordered trees. Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2 i-1 nodes, and a two-tree with a depth of K has a maximum of 2^ (k)-1 nodes; for any binary tree T, if its terminal node number (that is, the leaf node number) is N0, the 2 node is n2, then N0 = n2 + 1.

The chain storage structure of binary tree is a kind of important data structure, and its form is defined as follows:

Binary tree node
typedef struct bitnode{
	//Data
	char;
	Left and right child pointer
	struct Bitnode *lchild,*rchild;
} Bitnode,*bitree;


Binary Tree creation:

By reading a string, the algorithm for building a two-fork tree is as follows:

Create two
-fork-Tree int createbitree (bitree &t) {
	char data,
	in order sequence) Enter the value of the node in the binary tree in order of precedence (one character), ' # ' for the Empty tree
	scanf ("%c", &data);
	if (data = = ' # ') {
		T = NULL;
	}
	else{
		T = (bitree) malloc (sizeof (Bitnode));
		Generate root node
		t->data = data;
		Constructs left subtree
		createbitree (t->lchild);
		Construct right subtree
		createbitree (t->rchild);
	}
	return 0;
}

Traversal of a binary tree:

Traversal is one of the most basic operations of the tree, so-called traversal of the binary tree, that is, according to a certain rules and order all the nodes of the binary tree, so that each node is visited once, and only be visited once. Because the binary tree is a nonlinear structure, the traversal of the tree is essentially the transformation of each node of the two-fork tree into a linear sequence to be represented.

Recursive algorithm:

Output
void Visit (Bitree T) {
	if (t->data! = ' # ') {
		printf ("%c", T->data);}
}
First order traversal
void preorder (Bitree t) {
	if (t! = NULL) {
		//Access root node
		Visit (t);
		Access to left Dial hand node
		preorder (t->lchild);
		Access right child node
		preorder (t->rchild);}
}
Middle order traversal
void inorder (Bitree t) {
	if (t! = NULL) {
		//Access left Dial hand node
		inorder (t->lchild);
		Access root node
		Visit (T);
		Access right child node
		inorder (T->rchild);}
}
Post
-order traversal of void Postorder (Bitree t) {
	if (t! = NULL) {
		//Access left Dial hand node
		postorder (t->lchild);
		Access to the right child node
		postorder (t->rchild);
		Access root node
		Visit (T);
	}
}

Non-recursive algorithm:

<1> first-order traversal:

"Thinking": After access to T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then sequentially traverse the right subtree of T.

/* First-order traversal (non-recursive)
   idea: After accessing T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then the first traversal of the right sub-tree of T.
*
/void PreOrder2 (Bitree T) {
	stack<bitree> stack;
	P is the Traverse pointer
	bitree p = T;
	Stack is not empty or p is not empty loop while
	(P | |!stack.empty ()) {
		if (P! = NULL) {
			//deposit in Stack
			Stack.push (p);
			Access the root node
			printf ("%c", p->data);
			Traverse left subtree
			p = p->lchild;
		}
		else{
			//return stack
			p = stack.top ();
			Stack.pop ();
			Access right sub-tree
			p = p->rchild;
		}
	} While
}


<2> in-sequence traversal

"Idea": T is to traverse the root of the tree pointer, the middle sequence traversal requires that after traversing the left dial hand tree, access to the root, and then traverse the right subtree.
First, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, access to T->data, and then the right subtree of t traversal.

void InOrder2 (Bitree T) {
	stack<bitree> stack;
	P is the Traverse pointer
	bitree p = T;
	Stack is not empty or p is not empty loop while
	(P | |!stack.empty ()) {
		if (P! = NULL) {
			//deposit in Stack
			Stack.push (p);
			Traverse left subtree
			p = p->lchild;
		}
		else{
			//fallback, Access root node
			p = stack.top ();
			printf ("%c", p->data);
			Stack.pop ();
			Access right sub-tree
			p = p->rchild;
		}
	} While
}

<3> Post-traversal

"Thinking": T is to traverse the root of the tree pointer, post-order traversal requirements after traversing the left and right subtree, and then access to the root. It is necessary to determine whether the left and right subtrees of the root node have been traversed.

Sequential traversal (non-recursive)
typedef struct bitnodepost{
	bitree bitree;
	char tag;
} Bitnodepost,*bitreepost;

void PostOrder2 (Bitree T) {
	stack<bitreepost> stack;
	P is the Traverse pointer
	bitree p = T;
	Bitreepost BT;
	Stack is not empty or p is not empty when looping while
	(P! = NULL | |!stack.empty ()) {
		//traversal left subtree while
		(P! = null) {
			BT = (bitreepost) malloc (sizeof (bitnodepost));
			Bt->bitree = p;
			Visited left dial hand tree
			bt->tag = ' L ';
			Stack.push (BT);
			p = p->lchild;
		}
		The left and right subtree accesses the root node
		while (!stack.empty () && (Stack.top ())->tag = = ' R ') {
			BT = Stack.top ();
			Stack
			stack.pop ();
			bt->bitree;
			printf ("%c", Bt->bitree->data);
		}
		Traverse right subtree
		if (!stack.empty ()) {
			BT = Stack.top ();
			Visited right subtree
			bt->tag = ' R ';
			p = bt->bitree;
			p = p->rchild;
		}
	} While
}


<4> Hierarchy Traversal

"Thinking": Each node is accessed hierarchically from top to bottom, from left to right, and the queue is used during the hierarchical traversal process.

Hierarchy traversal
void Levelorder (Bitree t) {
	bitree p = T;
	Queues
	queue<bitree> queue;
	The root node is enqueued
	Queue.push (p);
	Queue not empty loop
	while (!queue.empty ()) {
		//Head element out of team
		P = queue.front ();
		Access P points to the node
		printf ("%c", p->data);
		Exit queue
		Queue.pop ();
		Left dial hand tree is not empty, the left subtree is enqueued
		if (p->lchild! = NULL) {
			queue.push (p->lchild);
		}
		Right subtree is not empty, the right subtree is enqueued
		if (p->rchild! = NULL) {
			queue.push (p->rchild);}}
}


Test Case:


Input:

abc# #DE #g# #F # # #

Output:


Code:

#include <iostream> #include <stack> #include <queue> using namespace std;
	Binary tree node typedef struct bitnode{//data char;
Left and right child pointer struct Bitnode *lchild,*rchild;

}bitnode,*bitree;
	Create two-fork-tree int createbitree (Bitree &t) {char data, in order sequence)
	Enter the value of the node in the binary tree in order of precedence (one character), ' # ' for the Empty Tree scanf ("%c", &data);
	if (data = = ' # ') {T = NULL;
		} else{T = (bitree) malloc (sizeof (Bitnode));
		Generate root node t->data = data;
		Constructs left subtree Createbitree (t->lchild);
	Construct right subtree Createbitree (t->rchild);
} return 0;
	}//Output void Visit (Bitree T) {if (T->data! = ' # ') {printf ("%c", t->data);
		}}//ordinal traversal void preorder (Bitree t) {if (t! = NULL) {//Access root node Visit (t);
		Access to left Dial hand node preorder (t->lchild);
	Access to the right child node preorder (t->rchild);  
        }}//middle order traversal void inorder (Bitree t) {if (t! = NULL) {//Access left Dial hand node inorder (t->lchild);  
        Access root node Visit (T);  
    Access to the right child node inorder (t->rchild);
}}//post-traversal of void Postorder (Bitree T) {	if (T! = NULL) {//Access left Dial hand node postorder (t->lchild);
		Access to the right child node Postorder (t->rchild);
	Access root node Visit (T);
}}//First order traversal (non-recursive) idea: After accessing T->data, the T is put into the stack, traversing the left subtree, and when the Zuozi returns, the top element of the stack should be T, out of the stack, and then the right subtree of T is traversed first.
	*/void PreOrder2 (Bitree T) {stack<bitree> stack;
	P is the Traverse pointer bitree p = T;
			Stack is not empty or p is not empty loop while (P | |!stack.empty ()) {if (P! = NULL) {//deposit in Stack Stack.push (p);
			Access the root node printf ("%c", p->data);
		Traverse left subtree p = p->lchild;
			} else{//rewind the stack P = stack.top ();
			Stack.pop ();
		Access right sub-tree P = p->rchild;
         }}//while}//In sequence traversal (non-recursive) idea: T is to traverse the root of the tree pointer, the middle order traversal requires that after traversing the left dial hand tree, access to the root, and then traverse the right subtree.
First, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, access to T->data, and then the right subtree of t traversal.
	*/void InOrder2 (Bitree T) {stack<bitree> stack;
	P is the Traverse pointer bitree p = T;
			Stack is not empty or p is not empty loop while (P | |!stack.empty ()) {if (P! = NULL) {//deposit in Stack Stack.push (p);
		Traverse left subtree p = p->lchild;
			} else{//fallback, access root node P = stack.top ();
			printf ("%c", p->data);
			Stack.pop ();
		Access right sub-tree P = p->rchild; }
	}//While}//post-traversal (non-recursive) typedef struct bitnodepost{Bitree bitree;
char tag;

}bitnodepost,*bitreepost;
	void PostOrder2 (Bitree T) {stack<bitreepost> stack;
	P is the Traverse pointer bitree p = T;
	Bitreepost BT; Stack is not empty or p is not empty when looping while (P! = NULL | |!stack.empty ()) {//Traversal left subtree while (P! = NULL) {BT = (bitreepost) malloc (sizeof (Bitnode
			Post));
			Bt->bitree = p;
			Visited left dial hand tree Bt->tag = ' L ';
			Stack.push (BT);
		p = p->lchild;
			}//Left and right subtree accesses the root node while (!stack.empty () && (Stack.top ())->tag = = ' R ') {BT = Stack.top ();
			Stack Stack.pop ();
		printf ("%c", bt->bitree->data);
			}//Traverse right subtree if (!stack.empty ()) {BT = Stack.top ();
			Visited right subtree Bt->tag = ' R ';
			p = bt->bitree;
		p = p->rchild;
	}}//while}//Hierarchy traversal void Levelorder (Bitree t) {bitree p = T;
	Queues queue<bitree> queue;
	The root node is enqueued Queue.push (p);
		Queue not empty loop while (!queue.empty ()) {//Head element out of Team P = Queue.front ();
		Access P points to the node printf ("%c", p->data);
		Exit queue Queue.pop (); Left dial hand tree is not empty,Enqueue the left subtree if (p->lchild! = NULL) {Queue.push (p->lchild);
		}//Right subtree is not empty, the right subtree is enqueued if (p->rchild! = NULL) {Queue.push (p->rchild);
	}}} int main () {Bitree T;
	Createbitree (T);
	printf ("Pre-order traversal: \ n");
	Preorder (T);
	printf ("\ n");
	printf ("Sequential traversal (non-recursive): \ n");
	PreOrder2 (T);
	printf ("\ n");
	printf ("middle order traversal: \ n");
	Inorder (T);
	printf ("\ n");
	printf ("Middle sequence traversal (non-recursive): \ n");
	InOrder2 (T);
	printf ("\ n");
	printf ("post-post traversal: \ n");
	Postorder (T);
	printf ("\ n");
	printf ("Post-traversal (non-recursive): \ n");
	PostOrder2 (T);
	printf ("\ n");
	printf ("Hierarchy traversal: \ n");
	Levelorder (T);
    printf ("\ n");
return 0; }


NEW: Click to open link



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.