From e68084401d05b72c2c6daf5390e7db387b2ad8b8 Mon Sep 17 00:00:00 2001 From: Varun kumar Date: Sat, 24 Nov 2018 15:18:21 +0530 Subject: [PATCH] add code for createNode function (#22472) --- .../data-structures/trees/index.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/guide/english/computer-science/data-structures/trees/index.md b/guide/english/computer-science/data-structures/trees/index.md index 42dad45bbd..7b2f4cadbf 100644 --- a/guide/english/computer-science/data-structures/trees/index.md +++ b/guide/english/computer-science/data-structures/trees/index.md @@ -102,6 +102,20 @@ struct node }; ``` +### Code for node creation +createNode() returns a new node with the given data and NULL left and right pointers. + +``` c++ +struct node* newNode(int element) +{ + struct node* temp = (node*)malloc(sizeof(node)); //Allocate memeory for temp node + temp->data = element; // Assign element to temp + temp->left = NULL; // Initialize left child as NULL + temp->right = NULL; // Initialize right child as NULL + return temp; +} +``` + #### More Information: * [CMU lesson notes](http://www.cs.cmu.edu/~clo/www/CMU/DataStructures/Lessons/lesson4_1.htm)