Binary Tree problem summary (1)-basic questions

Source: Internet
Author: User
1. Create a binary search tree

The binary search tree is a Sort binary tree. That is, the value of the Left subtree is not greater than that of the root node. The value of the right subtree is larger than that of the root node. You can use recursive or non-recursive methods to insert a node into a binary search tree. The code using recursive methods is simple, and non-recursive methods are easy to understand.

Binary Search Tree BST definition:

struct node {    int data;    struct node* left;    struct node* right;};

Code for creating a node in the binary search tree:

Struct node * newnode (INT data) {struct node * nD = (struct node *) malloc (sizeof (struct node); // allocate space Nd-> DATA = data; // set the data domain Nd-> left = Nd-> right = NULL; // The left and right child nodes are initialized to NULL return Nd ;}

Code 1 is inserted into the binary search tree (recursion, the function returns the root node ):

Struct node * insert (struct node * root, int data) {If (root = NULL) {// If the binary tree is null, the creation node is returned directly. Return newnode (data);} else {If (root-> DATA> = data) {// if the root node value is greater than or equal to the inserted value, insert the node to the left subtree. Root-> left = insert (root-> left, data);} else {// if the root node value is smaller than the inserted value, insert the node to the right subtree. Root-> right = insert (root-> right, data);} return root; // return the root node pointer. }}

Insert Node Code 2 into the binary search tree (recursion, no return value for the function ):

void insert2(struct node** rootRef, int data)          {                                                          struct node* root = *rootRef;                          if (root == NULL) {                                        *rootRef = newNode(data);                              return;                                            }                                                      if (root->data >= data)                                    insert2(&(root->left), data);                      else                                                       insert2(&(root->right), data);                 }                                                                        

Code 3 for inserting a binary search tree into a node (non-recursive ):

Void insert3 (struct node ** rootref, int data) {struct node * root = * rootref; struct node * nD = newnode (data); If (root = NULL) {* rootref = Nd; return;} struct node * parent = NULL; struct node * Current = root; while (current! = NULL) {parent = current; // Insert the parent tracking node until its child node is null if (current-> DATA> = data) Current = Current-> left; else current = Current-> right;} If (parent-> DATA> = data) // determine whether to insert data to the left or right child parent-> left = Nd; else parent-> right = Nd ;}

Create a binary search tree with three nodes:

// Directly create a binary tree struct node * build123a () {struct node * root = newnode (2); root-> left = newnode (1 ); root-> right = newnode (3); Return (Root );}
// Create a binary search tree struct node * build123b () {struct node * root = NULL; root = insert (root, 2); root = insert (root, 1 ); root = insert (root, 3); Return (Root );}

2. Number of Binary Tree nodes (no binary search tree is required)

// Number of nodes = number of left subtree nodes + number of right subtree nodes + 1 (root node) int size (struct node * root) {If (root = NULL) return 0; return size (root-> left) + size (root-> right) + 1 ;}

3. Binary Tree depth (the distance from the root node to the farthest leaf node, and the depth of the empty tree is 0)

Int maxdepth (struct node * root) {If (root = NULL) return 0; else {int ldepth = maxdepth (root-> left ); // recursively retrieve the left subtree depth int rdepth = maxdepth (root-> right); // obtain the right subtree depth return ldepth> rdepth? Ldepth + 1: rdepth + 1; // the depth of a binary tree is obtained when the value is greater than limit + 1 }}

4. the minimum value of the Binary Search Tree (similar to the maximum value, query the right subtree)

// Query int minvalue (struct node * root) {assert (root! = NULL); struct node * Current = root; while (current-> left! = NULL) {current = Current-> left;} Return Current-> data ;}

// Recursive Algorithms query the minimum int minvalue (struct node * root) {assert (root! = NULL); If (root-> left = NULL) return root-> data; else return minvalue (root-> left );}

5. check whether a value exists in the binary search tree.

/* Check whether a target node exists in the binary search tree */bool Lookup (struct node * root, int target) {If (root = NULL) return false; if (root-> DATA = target) return true; else if (root-> DATA> Target) return Lookup (root-> left, target ); else return Lookup (root-> right, target );}

 

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.