How to insert an element into a BST (DSA) ?
Today we will learn bst and how to insert a single element (or we can say a single node) into a bst**. This is easy for those who already know bst and double-linked lists, and these topics are important before reading this article. So I provided links to these topics, you can refer to it. -
1. For double-linked tables
2. For binary trees
So before you understand how to insert a single node into bst. You must know what bst is, bst is a
** Binary search tree**
It has some properties, such as :-
- The value of the left node is smaller or compared to the root and right elements
- The root node has a smaller value than the right node
- When we triple the node through the in-order tritree application, it will give an ascending array.
Looks like this
In order to insert the element into bst, we need a pointer to the root node, because in some parts we have to compare the key to the root data so that we know whether the key will be inserted to the left or right.
First we create a node and initialize it to bst.
This is the code you can refer to, the code is implemented in C language.
#include<stdio.h> #include<stdlib.h> struct node{ struct node* left; int data; struct node* right; }; struct node* createNode(int key){ struct node * newNode = NULL; newNode = malloc(sizeof(struct node)); newNode->left = NULL; newNode->data = key; newNode->right = NULL; return newNode; } void insertNewNode(struct node* root , int key){ struct node * prev = NULL; while(root!=NULL){ prev = root; if(key==root){ printf("element cannot insert it is present inside the bst already"); return ; } else if(key>root->data) { root = root->right; } else{ root = root->left; } } struct node * newNode = createNode(key); if(key>prev->data){ prev->right = newNode; } else{ prev->left = newNode; } } void inOrder(struct node* root){ if(root == NULL){ return root; } inOrder(root->left); printf("%d",root->data1`1); inOrder(root->right); } int main(){ struct node* head1 = createBst(20); struct node* head2 = createBst(10); struct node* head3 = createBst(30); head1->left=head2; head1->right=head3; insertNewNode(head1,40); printf("%d\n",head1->right->right->data); inOrder(head1); return 0; } </stdlib.h></stdio.h>
The above is the detailed content of How to insert an element into a BST (DSA) ?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To create a data table using phpMyAdmin, the following steps are essential: Connect to the database and click the New tab. Name the table and select the storage engine (InnoDB recommended). Add column details by clicking the Add Column button, including column name, data type, whether to allow null values, and other properties. Select one or more columns as primary keys. Click the Save button to create tables and columns.

Creating an Oracle database is not easy, you need to understand the underlying mechanism. 1. You need to understand the concepts of database and Oracle DBMS; 2. Master the core concepts such as SID, CDB (container database), PDB (pluggable database); 3. Use SQL*Plus to create CDB, and then create PDB, you need to specify parameters such as size, number of data files, and paths; 4. Advanced applications need to adjust the character set, memory and other parameters, and perform performance tuning; 5. Pay attention to disk space, permissions and parameter settings, and continuously monitor and optimize database performance. Only by mastering it skillfully requires continuous practice can you truly understand the creation and management of Oracle databases.

To create an Oracle database, the common method is to use the dbca graphical tool. The steps are as follows: 1. Use the dbca tool to set the dbName to specify the database name; 2. Set sysPassword and systemPassword to strong passwords; 3. Set characterSet and nationalCharacterSet to AL32UTF8; 4. Set memorySize and tablespaceSize to adjust according to actual needs; 5. Specify the logFile path. Advanced methods are created manually using SQL commands, but are more complex and prone to errors. Pay attention to password strength, character set selection, tablespace size and memory

The core of Oracle SQL statements is SELECT, INSERT, UPDATE and DELETE, as well as the flexible application of various clauses. It is crucial to understand the execution mechanism behind the statement, such as index optimization. Advanced usages include subqueries, connection queries, analysis functions, and PL/SQL. Common errors include syntax errors, performance issues, and data consistency issues. Performance optimization best practices involve using appropriate indexes, avoiding SELECT *, optimizing WHERE clauses, and using bound variables. Mastering Oracle SQL requires practice, including code writing, debugging, thinking and understanding the underlying mechanisms.

Field operation guide in MySQL: Add, modify, and delete fields. Add field: ALTER TABLE table_name ADD column_name data_type [NOT NULL] [DEFAULT default_value] [PRIMARY KEY] [AUTO_INCREMENT] Modify field: ALTER TABLE table_name MODIFY column_name data_type [NOT NULL] [DEFAULT default_value] [PRIMARY KEY]

Nested queries are a way to include another query in one query. They are mainly used to retrieve data that meets complex conditions, associate multiple tables, and calculate summary values or statistical information. Examples include finding employees above average wages, finding orders for a specific category, and calculating the total order volume for each product. When writing nested queries, you need to follow: write subqueries, write their results to outer queries (referenced with alias or AS clauses), and optimize query performance (using indexes).

The integrity constraints of Oracle databases can ensure data accuracy, including: NOT NULL: null values are prohibited; UNIQUE: guarantee uniqueness, allowing a single NULL value; PRIMARY KEY: primary key constraint, strengthen UNIQUE, and prohibit NULL values; FOREIGN KEY: maintain relationships between tables, foreign keys refer to primary table primary keys; CHECK: limit column values according to conditions.

Oracle is the world's largest database management system (DBMS) software company. Its main products include the following functions: relational database management system (Oracle database) development tools (Oracle APEX, Oracle Visual Builder) middleware (Oracle WebLogic Server, Oracle SOA Suite) cloud service (Oracle Cloud Infrastructure) analysis and business intelligence (Oracle Analytics Cloud, Oracle Essbase) blockchain (Oracle Blockchain Pla
