Look at the data structure write code (27) Realization of three-fork linked list the realization of __ chain list

Source: Internet
Author: User

Source Network disk Address: Click to open the link

The three-fork linked list has a pointer to the parent node more than the binary chain, which is helpful in the implementation of algorithms that need to find the father, ancestor, and the nearest ancestor of any two nodes. So when the algorithm has a large number of such operations, it is necessary to define the data structure as a three-fork list.

When the algorithm often needs to traverse or find the predecessor of the node in the traversal process, it is necessary to define the data structure as the Clue two fork list.

It's not difficult to write data structures, but it's not easy to choose the right structure from the actual problem . There is a word of wisdom in the computer: program = data structure + algorithm.


Gossip is not much to say, the following code is not difficult.

Mainly say the creation of the three-fork linked list, using the sequence method to create a more appropriate. The algorithm is also relatively simple. Take a closer look and you should understand.


Special NOTE: "Data structure." Min version of a book, said in the use of the three-link list to achieve the first order, in order, after the next traversal, do not need to use the stack, but the algorithm is more complex.

always want to use code to achieve a bit. Suffer from the problem of time and no algorithm ideas. Later slowly filled.

BinaryTree3.cpp: Three Forks linked list//#include "stdafx.h" #include "stack.h" #include "queue.h" typedef char ElementType;
	typedef struct TREENODE {ElementType data;
	TreeNode * Father;
	TreeNode * LEFTCHILD;
TreeNode * RIGHTCHILD;

}*tree;

void Treeinit (tree * tree) {* tree = NULL;}
	Assign a node tree Makenode () {ElementType data = ';
	scanf ("%c\n", &data);
	if (data = = ' # ') {return NULL;
	Tree T = (tree) malloc (sizeof (TreeNode));
	T->data = data;
return t;
	///According to the sequence of the Three Forks linked list can not be more suitable for the void treecreate (tree) {*tree = Makenode ();
		if (*tree!= NULL) {linkqueue queue;
		Queueinit (&queue);
		Enqueue (&queue,*tree);
		(*tree)->father = null;//root node father is empty.
			while (!queueempty (queue)) {tree father;
			Dequeue (&queue,&father);
			Tree left = Makenode ();
			Father->leftchild = left;
				if (left!= NULL) {left->father = father;
			Enqueue (&queue,left);
			Tree right = Makenode ();
			Father->rightchild = right; if (right!= NULL)
			{right->father = father;
			Enqueue (&queue,right);
	}//Free queue Memory queuedestory (&queue);
		}//To Empty void treeclear (tree * tree) {if (!= NULL) {linkstack stack) by first order non-recursive return;
		Stackinit (&stack);
		Stackpush (&stack,*tree);
			while (!stackempty (stack)) {Tree T;
			Stackpop (&stack,&t);
			Must first the right subtree into the stack, after Zuozi into the stack if (t->rightchild!= NULL) {Stackpush (&stack,t->rightchild);
			} if (T->leftchild!= NULL) {Stackpush (&stack,t->leftchild);
			//Must be finally released, or there will be a memory problem.

		Free (t);
		} *tree = null;//empty tree.
	Stackdestory (&stack);

} void Treedestory (tree * tree) {treeclear;}
When tree = = NULL, the trees are empty.

BOOL Treeempty {return tree = = NULL true:false;}
	Middle order non-recursive ... int treelen (tree tree) {int len = 0;
	Linkstack Stack;
	Stackinit (&stack);
	Stackpush (&stack,tree);
		while (!stackempty (stack)) {Tree T; while (Stackgettop (stack,&t) && t) Stackpush (&Stack,t->leftchild);
			Stackpop (&stack,&t)//null pointer fallback stack if (!stackempty (stack)) {Stackpop (&stack,&t);
			Len + +;
		Stackpush (&stack,t->rightchild);
	}//Release stack space stackdestory (&stack);
return Len; }//three fork list first void Preordertraverse (Tree t) {}//three fork list sequence void Inordertraverse (Tree t) {}//three fork list sequential void Postordertraverse (Tre  
        e t) {}//sequence//Use queue void levelordertraverse (tree tree) {if (!= NULL) {linkqueue queue;  
        Queueinit (&queue);  
        Enqueue (&queue,tree);  
            while (Dequeue (&queue,&tree) && tree) {printf ("%c\t", tree->data);  
            if (tree->leftchild!= NULL) {enqueue (&queue,tree->leftchild); } if (Tree->rightchild!= NULL) {enqueue (&queue,tree->rightchil  
            D);  
    } queuedestory (&queue); 

}  
}Tree Treegetnode (Tree T, ElementType data) {Tree Findtree = NULL;
		if (t!= NULL) {if (T->data = = data) {return t;
		} Findtree = Treegetnode (T->leftchild,data);
		if (Findtree = = NULL) {findtree = Treegetnode (T->rightchild,data);
} return findtree;
	} void Treegetallparent (tree t,elementtype data) {Tree Findtree = Treegetnode (T,data);
		if (Findtree!= NULL) {printf ("ancestor of%c node is:", data);
		Tree father = findtree->father;
			while (Father!= NULL) {printf ("%c\t", father->data);
		Father = father->father;
	printf ("\ n");
	else {printf ("No ancestor of%c node", data);
	} int _tmain (int argc, _tchar* argv[]) {tree tree;
	printf ("---------------sequence to generate a triple linked list-------------\ n");
	Treecreate (&tree);
	Levelordertraverse (tree); char * isempyt = Treeempty (tree)?
	"Yes": "No";
	int len = Treelen (tree);
	printf ("Whether the tree is empty:%s, the length of the tree is:%d\n", Isempyt,len);
	printf ("---------------Look for ancestor problems (with a triple linked list)-------------\ n");
	Treegetallparent (tree, ' I '); Treedestory (&amP;tree);
return 0;
 }

Run Screenshots:




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.