The basic operation of binary tree

Source: Internet
Author: User

The initialization operation of the binary tree. The initialization of the binary tree requires that the pointer to the root node of the binary tree be set to null: void Initbittree (Bitree *t)//Two binary tree initialization operation {*t=null;} The destruction operation of the binary tree. If the binary tree is present, release the two-tree storage space: void Destroybittree (Bitree *t)//Destroy binary tree operation {if (*t)//If non-empty binary tree {if ((*t)->lchild) Destroybittree ( & ((*t)->lchild), if ((*t)->rchild) Destroybittree (& ((*t)->rchild)); free (*t); *t=null;}} Creates a two-tree operation. According to the recursive definition of the binary tree, the root node of the binary tree is given, the element value is assigned to the data field of the node, and then the right subtree is created//Saozi recursively. Where ' # ' means null: void Creatbittree (Bitree *t)//recursive Create two fork tree {DataType ch;scanf ("%c", &ch); if (ch== ' # ') *t=null;else{*t= (bitree ) malloc (sizeof (Bitnode));//Generate root node if (!) ( *t)) exit ( -1);(*t)->data=ch; Creatbittree (& (*t)->lchild); Creatbittree (& (*t)->rchild);}} The left insert operation of the binary tree. The pointer p points to a node of the binary tree T, and the subtree C is inserted into t so that C becomes the left subtree of P point node//,p the original Zuozi that points to the node becomes the right subtree of c: int insertleftchild (Bitree p,bitree C)//two fork tree left insert operation {if ( p)//if pointer p is not empty {C->rchild=p->lchild;//p's original left subtree becomes C's right subtree p->lchild=c;//subtree C as the left subtree of P return 1;} return 0;} The right insert operation of the binary tree. The pointer p points to a node of the binary tree T, and the subtree C is inserted into t so that C becomes the right subtree of the P-pointing node//,p the original right subtree of the point node becomes the right subtree of c: int insertrightchild (Bitree p,bitree C)//two fork tree right insert action {if (p)//If the pointer p is not empty {C->rchilD=p->rchild;//p's original right subtree becomes C's right subtree p->rchild=c;//subtree C as P's right subtree return 1;} return 0;} /* Returns the pointer operation of the binary tree node. Finds a node in a binary tree that points to an element value of E, returns a pointer to the node if it is found, or returns NULL. Implementation: Define a queue Q, used to hold a pointer to a node in a two-fork tree, starting at the root node, judging whether the value of the node is equal to E, and if it is equal, returns a pointer to that node; otherwise, the pointer to the left child node of the node and the pointer to the right child node are queued. If the node has a left child node, the pointer of its left child is in the queue, and if the node has a right child node, the right child's pointer is placed in the queue. The pointer of the team head is then queued to determine if the element value of the node pointed to by the pointer is equal to E, and if it is equal, the pointer to that node is returned, otherwise the pointer to the left child node of the node and the pointer to the right child node are queued. Repeat this operation until the queue is empty. */bitree Point (Bitree t,datatype e)//Find pointer to node with element value E {bitree q[maxsize];//define a queue to hold the pointer int front=0,rear=0;//for the node in the two-fork tree Initialize the queue Bitnode *p;if (T)//If the binary tree is not empty {q[rear]=t;rear++;while (front!=rear)//If the queue is non-empty {p=q[front];//Remove the team head pointer front++;//            Put the team head pointer out of the team if (p->data==e) return p;if (p->lchild)//If the left child node exists, the left child pointer is enqueued {q[rear]=p->lchild;//left child node of the pointer queue rear++;} if (p->rchild)//If the right child node exists, the right child pointer to the queue {q[rear]=p->rchild;//Right child node pointer to the queue rear++;}}  return NULL;} Returns the left child element value operation for the node of the binary tree. If a node with an element value of e exists and the left child node of the node is present, the element value of the left child node of the node is returned. DataType leftchild (Bitree t,datatype e)//return binary tree left child node element value operation {Bitree p;if (T)//If the binary tree is not empty {p=point (t,e);//p is the pointer to the node of the element value E if (p &&p-&gtLchild)//If p is not empty and P's left child node exists return p->lchild->data;} return;} Returns the right child element value operation for the node of a binary tree. If a node with an element value of e exists and the right child node of that node exists, then the element value of the right child node of the node is returned. DataType rightchild (Bitree t,datatype e)//return binary Tree Right child node element value operation {Bitree p;if (T)//If the binary tree is not empty {p=point (t,e);//p is the pointer to the node of the element value E if (p &&p->rchild)//If p is not empty and P's right child node exists return p->rchild->data;} return;} The left delete operation of the binary tree. In a binary tree, the pointer p points to a node in the binary tree and deletes the left subtree of the node to which P points. If delete//succeeded, return 1, otherwise return 0.int Deleteleftchild (Bitree p)//two fork tree left delete operation {if (p)//if pointer p is not empty {Destroybittree (& (P->lchild)); /delete left subtree return 1;} return 0;} The right delete operation of the binary tree. In a binary tree, the pointer p points to a node in the binary tree and deletes the right subtree of the node to which P points. If delete//success, return 1, otherwise return 0.int Deleterightchild (Bitree p)//two fork tree right delete operation {if (p)//if pointer p is not empty {Destroybittree (& (P->rchild)) ;//Delete Right sub-tree return 1;} return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The basic operation of binary tree

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.