I want know what is the best : Array OR Binary search tree in ( insert , delete , find max and min ) and how can I Improve both of them ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An array allows random access to each element in it. so you get insert, delete and look for a specific element in
O(1), and max/min, delete inO(n). [you can also make max/minO(1)and deleteO(n)instead]. If you are keeping your array sorted, it will cause insert/delete to beO(n), but you will gainO(logn)find, andO(1)min/max.A BST is sorted by definition, and for a regular [unbalanced] BST, you get
O(n)worst case behavior. For balanced BST, you getO(logn)insert/delete/find. You can getO(1)min/max any how for both.Arrays are also usually faster to iterate [assuming order of iteration is not important] since you gain better cache performance. Also, unlike BST – which has unbounded size by nature, an array requires reallocation and copying the data when your array is full.
Improving a BST can be done by making it balanced – like AVL or red-black-trees.
Which is better? it depends on the application. Usually when you are planning to insert data and keep it sorted, BST will be prefered. If random access or iteration is the main purpose: you usually use an array.