Tree definition and basic operations

Source: Internet
Author: User

First, what is a tree?
The number definition is recursive:
The definition tree meets the following conditions and contains a finite set of at least one node:
(1) A specific node in a tree is called a root or a root.
(2) other nodes are divided into N> = 0 non-intersecting sets T1... TN, each set is also a tree, but it is called the root subtree.
The main operations of a tree include: finding the depth of the tree, finding the child node of a given node, Brother node, traversing the tree, inserting the child tree, deleting the child tree, and so on. Because the definition of the tree itself is recursive, many operations in the actual program are also done through recursion.
How is a tree represented? There is a common expression that is used to represent children and brothers. For each node, the Child Pointer Points to its own child node and the brother Pointer Points to its right brother.

Typedef char elemtype; // determine the entire tree typedef struct tnode {elemtype data; tnode * firstchild, * nextsibling;} tnode, * tree through the first child and the next brother; // initialize a node tree inittree (elemtype e) {tree pt; Pt = (tnode *) malloc (sizeof (tnode); Pt-> firstchild = NULL; pt-> nextsibling = NULL; Pt-> DATA = E; return pt;} // Delete tree void deletetree (TREE * t) {If (null = * t) return; deletetree (& (* t)-> nextsibling); deletetree (& (* t)-> firstchild); free (* t); * t = NULL;} // Print the void printtree (tree T) {If (null = T) return; else {printf ("% C", T-> data ); /* // if you are not used to recursion, you can write tree P = T-> firstchild; while (P! = NULL) {printtree (p); P = p-> nextsibling;} * // The comments are equivalent to the following two lines: printtree (t-> firstchild ); printtree (t-> nextsibling) ;}// calculate the height of the tree int treedepth (tree T) {int hmax = 0; int subtreedepth = 0; If (null = T) return 0; tree P = T-> firstchild; while (P! = NULL) {subtreedepth = treedepth (p); P = p-> nextsibling; If (subtreedepth> hmax) hmax = subtreedepth;} return hmax + 1 ;} // address tree result of the element found in the global variable record; void locateelem (tree T, elemtype e) {If (null = T) return; if (t-> DATA = e) Result = T;/* tree P = T-> firstchild; while (P! = NULL) {locateelem (p, e); P = p-> nextsibling;} */locateelem (t-> firstchild, e); locateelem (t-> nextsibling, e);} // return the pointer tree findchild (tree T, elemtype e) {result = NULL; locateelem (T, E ); // If no result is found or the node is a leaf node, null if (null = Result & null = Result-> firstchild) return NULL; elsereturn result-> firstchild;} is returned ;} // return the pointer tree findsibling (tree T, elemtype e) {result = NULL; locateelem (T, E) of the sibling node; // if not found or the node does not have the right sibling, return NULL if (null = Result & null = Result-> nextsibling) return NULL; elsereturn result-> nextsibling;} // Insert the subtree bool inserttree (tree T, tree Tadd, elemtype E) {result = NULL; locateelem (T, E); If (result! = NULL) {Tadd-> nextsibling = Result-> firstchild; Result-> firstchild = Tadd; return true;} elsereturn false;} // Delete the subtree bool deletesubtree (tree T, elemtype e) {result = NULL; locateelem (T, E); // first, judge the node where the result is. If (result-> firstchild! = NULL) {deletetree (& (result-> firstchild); Return true;} elsereturn false ;}

When initializing a node, note that the return value of the function is used to remove the dynamically allocated node, but the relationship between nodes is not described, therefore, you need to manually connect the child and brother pointers of each node in the main function. There is no doubt that the core operation of a tree is how to recursively traverse a tree. Our practice is as follows: For a node, first access its data domain, then access its child through the Child pointer until the leaf node along this branch, at this time, the brother who accesses the node until all the brothers have accessed the node, then rolls back to the previous node, the brother who accesses the node, and so on.
In the program, there are two writing methods. Taking printtree as an example, the comments are closer to our description, while the actual code used is more abstract and concise. It is reasonable to say that the two can be converted to each other, but for the height of the tree, I have no good solution.
There is an annoying problem with the product discovery through recursive operations, that is, finding out how to bring the pointer to this node in the future. I tried many methods and did not use them. Instead, I used global variables. Therefore, the result pointer must be initialized to null each time a search-related operation (query, specified location insert, and specified location delete) is performed. If you have a better solution, please feel free to advise.
The operation of finding its parent node through a node is not provided here, although this work can be completed "possibly, however, the simplest and easiest way is to add a pointer to the parent in the struct.

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.