Create a two-fork tree

Source: Internet
Author: User
Tags print format

How do I position nodes in a binary tree? Pointing Road Method Locating node: According to the experience of life: Turn left, right, turn left 、、、
General idea:By locating the root node relative to the target node, this method can avoid the "linear positioning" of the recursive nature of the binary tree.
C Description: Use bit bit to refer to the road
#define BT_LEFT 0#define bt_right 1typedef unsigned long bt_pos;


two operation of the fork tree:Create Tree--btree_createDestroy Tree--bTree_destroyEmpty the tree--bTree_clearInsert Junction--bTree_insertDelete Node--bTree_deleteGet junction--b.Tree_getGet root node--bTree_rootGets the node count of the tree--bTree_countGet the degree of the tree--bTree_degree
? Insert node:
/* Parameter Description tree: Two-tree node to insert node: the node pos to be inserted: the bit bit for "finger" count: The number of times you need to refer to a path flag: Consider the node to be inserted is not the leaf node, but instead of a node in the binary tree to insert the operation, this flag Used to indicate whether the node being replaced and its subtree should be on the left or right of the insertion node. */int Btree_insert (btree* tree, btree_node* node, bt_pos pos, int count, int flag) {head_node* Btree_head_node = (head_nod e*) tree; int ret = (Btree_head_node! = null) && (node! = null) && ((flag = = Bt_left) | | (flag = = Bt_right));  int bit = 0;  if (ret) {btree_node* parent = NULL;   btree_node* current = btree_head_node->root;  Node->left = NULL;     Node->right = NULL;   while ((Count > 0) && (current! = NULL)) {bit = pos & 1;      pos = pos >> 1;      /* Record the location of the parent node of the node you want to insert */parent = current;   if (bit = = Bt_left) {current = current->left;   } else if (bit = = bt_right) {current = current->right;  } count--;  } if (flag = bt_left) {node->left = current;  } else if (flag = bt_right) {node->right = current; if (parent! = NULL) {/* is determined by the last step of the insertion node at the left or right of the parent node */IF (bit = = Bt_left) {parent->left = node;   } else if (bit = = bt_right) {parent->right = node;  }} else {/* This is the case where the insertion node is the root node */btree_head_node->root = node; } btree_head_node->count++; } return ret;}


? print function:The need to reflect the shape of the binary tree, the left node exists and the right node does not exist, the corresponding print out the right node format.
Implementation code:
/* Parameter Description: node: Printed node P_func: print function format: print format div: format character */static void Recursive_display (Btree_node *node, btree_printf* p_ func, int format, char div) {int i = 0;  if ((node! = null) && (P_func! = null)) {  for (i=0; i<format; i++)  {   printf ("%c", div);  }   P_func (node);   printf ("\ n");   if ((node->left! = NULL) | | (Node->right! = NULL))  {   Recursive_display (node->left, P_func, format+2, div);   Recursive_display (Node->right, P_func, format+2, Div);  } } else {  for (i=0; i<format; i++)  {   printf ("%c", div);  }  printf ("\ n"); }}void Btree_display (btree* tree, btree_printf* P_func, Char div) {head_node* Btree_head_node = (head_node*) tree;  if (btree_head_node! = NULL) {   



? Delete a node:Node number of the subtree that dropped the node
Implementation code:
/* Statistics node number */static int Recursive_count (btree_node* node) {int ret = 0;  if (node! = NULL) {ret = Recursive_count (node->left) + recursive_count (node->right) + 1;} return ret;} btree_node* btree_delete (btree* tree, Bt_pos pos, int count) {head_node* Btree_head_node = (head_node*) tree; int bit = 0;  btree_node* ret = NULL;  if (Btree_head_node! = null) {btree_node* parent = null;   btree_node* current = btree_head_node->root;   while ((Count > 0) && (current! = NULL)) {bit = (pos & 1);      pos = (pos >> 1);      parent = current;   if (bit = = Bt_left) {current = current->left;   } else if (bit = = bt_right) {current = current->right;  } count--;    if (parent! = NULL) {if (bit = = Bt_left) {parent->left = null;       } else if (bit = = bt_right) {parent->right = NULL;  }} else {btree_head_node->root = NULL;   } ret = current; /* Statistics on the number of two fork tree nodes after deletion of the node, and the number of nodes on the subtree that deleted the node is also removed. */Btree_head_node->count = Btree_head_node->count-recursive_count (current); } return ret;}  /* Returns the number of nodes */int Btree_count (btree* tree) {head_node* Btree_head_node = (head_node*) tree; int ret = 0;  if (btree_head_node! = NULL) {ret = btree_head_node->count;} return ret;}



? Height of the tree:
Implementation code:
static int Recursive_height (btree_node* node) {int ret = 0;  if (node! = NULL) {  int lh = recursive_height (node->left);  int RH = Recursive_height (node->right);   ret = ((LH > RH) LH:RH) + 1;  }  return ret;} int btree_height (btree* tree) {head_node* Btree_head_node = (head_node*) tree; int ret = 0;  if (btree_head_node! = NULL) {  ret = recursive_height (btree_head_node->root);}  




? The degree of the tree:
Considering the nature of the two-fork tree, the maximum is 2, to use this feature to improve the efficiency of traversing the binary tree can be described in the code: the left child of the Joghen node exists, the degree plus 1, if the right child also exists, the degree plus 1. You can return directly, without traversing the other subtrees. If the left and right children do not exist, it can be judged that the tree has only the root node, the degree of 0. return directly. If the left and right child exists only one, then recursive traversal begins.
Implementation code:
static int Recursive_degree (btree_node* node) {int ret = 0;  if (node! = null) {  if (node->left! = null)  {   ret++;  }  if (node->right! = NULL)  {   ret++;  }  if (ret = = 1)  {   int ld = Recursive_degree (node->left);   int rd = Recursive_degree (node->right);         if (Ret < ld)      {    ret = ld;   }      if (Ret < rd)   {    ret = rd;   }}  } return ret;} int Btree_degree (btree* tree) {head_node* Btree_head_node = (head_node*) tree; int ret = 0;  if (btree_head_node! = NULL) {  ret = Recursive_degree (btree_head_node->root);}  return ret;}


Create a two-fork 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.