Recursive and non-recursive Methods Insert new nodes into the binary sorting tree

Source: Internet
Author: User

Today, on the way back to the dormitory, I talked to me about inserting a new node into the binary tree. Isn't that easy? I wrote a program to show him back to the dormitory, he said that what I want is a recursive method. As for recursion, if a new node is not inserted every time a function is called, isn't it a binary sorting tree, instead of a node. I also looked at the non-recursive method and found that when the root is empty, the newly created node P is directly assigned to the root node T, which mainly uses the principle of referencing and assigning values, I can use the reference principle. (1) When the root is empty, the root T is directly assigned to the newly created node pointer to return (2) when the value of the root node is greater than the value of the node to be inserted, recursively Insert the new node to the right position of the Left subtree of the Root Node
(3) Otherwise, recursively Insert the new node to the right subtree of the root node.

The Code is as follows:

// Create a binary tree. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> using namespace STD; // defines the node typedef struct node {int data; struct node * lchild; // left child struct node * rchild; // right child} node, * pnode; pnode T = NULL; // The root node of the Binary Tree // The middle-order traversal of the Binary Tree void midsequence (pnode t) {If (T! = NULL) {midsequence (t-> lchild); cout <t-> data <""; midsequence (t-> rchild );}} // Insert a new node (non-Recursive Implementation) to the binary sorting tree void createbsorttree (pnode & T, int data) {// create a new node pnode P = (pnode) malloc (sizeof (node); P-> DATA = data; P-> lchild = NULL; P-> rchild = NULL; If (t = NULL) {T = P; return;} pnode q = T, parent; do {parent = Q; If (parent-> DATA> data) q = parent-> lchild; elseq = parent-> rchild;} while (Q! = NULL); If (parent-> DATA> data) parent-> lchild = P; else parent-> rchild = P ;} // Insert a new node to the binary sorting tree (Recursive Implementation) void createbsorttree2 (pnode & T, int data) {If (t = NULL) {// create a new node pnode P = (pnode) malloc (sizeof (node); P-> DATA = data; P-> lchild = NULL; p-> rchild = NULL; t = P; return;} else if (t-> DATA> data) createbsorttree2 (t-> lchild, data ); else createbsorttree2 (t-> rchild, data);} // test int _ tmain (INT argc, _ tchar * argv []) {int A [] = {0, 5, 8, 4, 2, 3, 10}; int Len = sizeof (a)/sizeof (INT); For (INT I = 0; I <Len; I ++) {createbsorttree2 (t, A [I]);} midsequence (t); System ("pause"); Return 0 ;}

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.