This document discusses binary search trees (BSTs). It explains that BSTs organize data to allow searching, insertion, and deletion operations to occur in O(log n) time. The document outlines pseudocode and examples for searching, inserting, and removing nodes from a BST. It also discusses how the order of data insertion can impact the shape and efficiency of operations on the BST. Balanced search tree data structures are suggested to maintain optimal O(log n) performance during updates.
In this playlist
https://youtube.com/playlist?list=PLT...
I'll illustrate algorithms and data structures course, and implement the data structures using java programming language.
the playlist language is arabic.
The Topics:
--------------------
1- Arrays
2- Linear and Binary search
3- Linked List
4- Recursion
5- Algorithm analysis
6- Stack
7- Queue
8- Binary search tree
9- Selection sort
10- Insertion sort
11- Bubble sort
12- merge sort
13- Quick sort
14- Graphs
15- Hash table
16- Binary Heaps
Reference : Object-Oriented Data Structures Using Java - Third Edition by NELL DALE, DANEIEL T.JOYCE and CHIP WEIMS
Slides is owned by College of Computing & Information Technology
King Abdulaziz University, So thanks alot for these great materials
Succinct Data Structure for Analyzing Document CollectionPreferred Networks
Invited talk at the workshop ALSIP2011. The topic includes, Rank/Select data structure, and its implementation, wavelet trees and its applications, suffix arrays, suffix trees, Burrows Wheeler's Transform, FM-index, Top-k document query.
In this playlist
https://youtube.com/playlist?list=PLT...
I'll illustrate algorithms and data structures course, and implement the data structures using java programming language.
the playlist language is arabic.
The Topics:
--------------------
1- Arrays
2- Linear and Binary search
3- Linked List
4- Recursion
5- Algorithm analysis
6- Stack
7- Queue
8- Binary search tree
9- Selection sort
10- Insertion sort
11- Bubble sort
12- merge sort
13- Quick sort
14- Graphs
15- Hash table
16- Binary Heaps
Reference : Object-Oriented Data Structures Using Java - Third Edition by NELL DALE, DANEIEL T.JOYCE and CHIP WEIMS
Slides is owned by College of Computing & Information Technology
King Abdulaziz University, So thanks alot for these great materials
Succinct Data Structure for Analyzing Document CollectionPreferred Networks
Invited talk at the workshop ALSIP2011. The topic includes, Rank/Select data structure, and its implementation, wavelet trees and its applications, suffix arrays, suffix trees, Burrows Wheeler's Transform, FM-index, Top-k document query.
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
Presented at PyCon JP 2014.
Video is available at
http://bit.ly/1tXYhw6
This talk explores case studies of effective usage of Numpy/Scipy and shows that the computational speed sometimes improves drastically with the appropriate derivation of formulas and performance-conscious implementation. I especially focus on scipy.sparse, the module for sparse matrices, which is often useful in the areas of machine learning and natural language processing.
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
DSA (Data Structure and Algorithm) Questions.
University Assignment/Homework Questions.
Stack, Queue , Binary Tree, Heap, AVL, Linked List.
Important DSA Questions to boost the concept.
A basic introduction to the R programming language. No prior programming knowledge needed. Download to access embedded files. Slide content includes:
Part zero: Getting started (Interacting with R)
Part one: Objects (Vectors, Matrices, Character arrays)
Part two: Data manipulation (Analysing data, T-test)
Part three: External data (Reading data into R, ANOVA )
Part four: Packages and libraries (Installing new packages into R)
Part five: Scripts (Using pre-written code)
Part six: Logic (programming, other functions in R)
Please use the comments to list any issues with the slides or suggest improvements
Trees. Defining, Creating and Traversing Trees. Traversing the File System
Binary Search Trees. Balanced Trees
Graphs and Graphs Traversal Algorithms
Exercises: Working with Trees and Graphs
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
Presented at PyCon JP 2014.
Video is available at
http://bit.ly/1tXYhw6
This talk explores case studies of effective usage of Numpy/Scipy and shows that the computational speed sometimes improves drastically with the appropriate derivation of formulas and performance-conscious implementation. I especially focus on scipy.sparse, the module for sparse matrices, which is often useful in the areas of machine learning and natural language processing.
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
DSA (Data Structure and Algorithm) Questions.
University Assignment/Homework Questions.
Stack, Queue , Binary Tree, Heap, AVL, Linked List.
Important DSA Questions to boost the concept.
A basic introduction to the R programming language. No prior programming knowledge needed. Download to access embedded files. Slide content includes:
Part zero: Getting started (Interacting with R)
Part one: Objects (Vectors, Matrices, Character arrays)
Part two: Data manipulation (Analysing data, T-test)
Part three: External data (Reading data into R, ANOVA )
Part four: Packages and libraries (Installing new packages into R)
Part five: Scripts (Using pre-written code)
Part six: Logic (programming, other functions in R)
Please use the comments to list any issues with the slides or suggest improvements
Trees. Defining, Creating and Traversing Trees. Traversing the File System
Binary Search Trees. Balanced Trees
Graphs and Graphs Traversal Algorithms
Exercises: Working with Trees and Graphs
Class lecture of Data Structure and Algorithms and Python.
Stack, Queue, Tree, Python, Python Code, Computer Science, Data, Data Analysis, Machine Learning, Artificial Intellegence, Deep Learning, Programming, Information Technology, Psuedocide, Tree, pseudocode, Binary Tree, Binary Search Tree, implementation, Binary search, linear search, Binary search operation, real-life example of binary search, linear search operation, real-life example of linear search, example bubble sort, sorting, insertion sort example, stack implementation, queue implementation, binary tree implementation, priority queue, binary heap, binary heap implementation, object-oriented programming, def, in BST, Binary search tree, Red-Black tree, Splay Tree, Problem-solving using Binary tree, problem-solving using BST, inorder, preorder, postorder
Write a C++ program that implements a binary search tree (BST) to man.pdfhardjasonoco14599
Write a C++ program that implements a binary search tree (BST) to manage a number of integer
items with different priorities. In order to do so. you will need to maintain a queue as an item in
the tree. Each queue item is an integer. All items in a given queue will have the same priority. As
a new item with a user-specified priority comes in, it should be enqueued in the queue with items
of that priority. A new queue may need to created in case a queue with that priority does not
exist. Use dynamic memory to manage nodes in the queue and BST. Implement the following:
Constructor. Destructor. Overloaded copy constructor. Overloaded assignment operator.
Overloaded \"==\" operator to check if two trees are equal. A tree t1 is equal to t2 if for every
queue in t1, a corresponding queue with the same priority exists in t2 with the same set of
elements (the order of the elements in the queue docs not matter). Overloaded \"
Solution
program using namespace std;
#define width_unit 5
class BStree
{
private:
class Node
{
public:
int data;
Node *left, *right;
Node(int d=0)
:data(d), left(NULL), right(NULL) {}
};
Node *root;
Node * trav(int, Node * &);
void chop(Node * N);
void copy(Node * N);
void print(ostream &, Node *, int) const;
void print(Node *, int) const;
public:
BStree(void);
~BStree(void);
bool find(int);
void insert(int);
void remove(int);
bool empty(void) const;
Tree(const BStree &);
const BStree & operator=(const BStree &);
friend ostream & operator<<(ostream &, const BStree &);
};
BStree::BStree(void)
{
root=NULL;
}
bool Tree::empty(void) const
{
return !root;
}
BStree::Node * BStree::trav(int foo, Node * & par)
{
Node * curr=root;
par=NULL;
while(curr && curr->data != foo)
{
par=curr;
if(foo < curr->data)
curr=curr->left;
else
curr=curr->right;
}
return curr;
}
bool BStree::find(int foo)
{
Node * par=NULL;
Node * curr=trav(foo, par);
return curr;
}
void BStree::insert(int foo)
{
Node * par=NULL;
Node * curr=trav(foo,par);
if(!curr)
{
curr= new Node(foo);
if(!par)
root=curr;
else if(foo < par->data)
par->left=curr;
else
par->right=curr;
}
}
void BStree::remove(const int foo)
{
Node * par=NULL;
Node * curr=trav(foo,par);
if(curr)
{
if(curr->left && curr->right)
{
Node * tmp=curr;
par=curr;
curr=curr->left;
while(curr->right)
{
par=curr;
curr=curr->right;
}
tmp->data=curr->data;
}
Node *tmp=(curr->left ? curr->left : curr->right);
if(!par)
root=tmp;
else if(par->data < curr->data)
par->right=tmp;
else
par->left=tmp;
delete curr;
}
}
void BStree::chop(Node *N)
{
if(N)
{
chop(N->left);
chop(N->right);
delete N;
}
}
BStree::~BStree(void)
{
chop(root);
}
BStree::BStree(const BStree & T)
{
root=NULL;
copy(T.root);
}
void BStree::copy(Node * N)
{
if(N)
{
insert(N->data);
copy(N->left);
copy(N->right);
}
}
const BStree & BStree::operator=(const BStree & T)
{
if(this != &T)
{
chop(root);
root=NULL;
copy(T.root);
}
return *this;
}
void BStree::print(ostream & ost, Node * curr, int level) const
{
if(curr)
{
print(ost,curr->right,level+1);
ost<data<le.
Given a newly created Binary Search Tree with the following numerica.pdfhadpadrrajeshh
Given a newly created Binary Search Tree with the following numerical key input sequence
(from left to right): 9, 4, 12, 7, 5, 2, 20, 14, 11, 13, 19, 16, 17, 42, 24
a.) We are asked to add a function Position lowestKey(const Position& v) const to the class
SearchTree.
Function prototype: Position lowestKey(const Position& v);
input argument: position of the starting node in the binary search tree to calculate from. For the
whole tree this would be the position for the root of the tree.
return value: position of the node having the lowest key value.
i. Describe strategy with reasonings and examples to convince that it will work.
ii. Implement the function using C++ and show the source code listing.
b) We are also asked to add a function Position highestKey(const Position& v) const to the class
SearchTree.
Function prototype: Position highestKey(const Position& v) const;
input argument: position of the node in the binary search tree to calculate from. For the whole
tree this would be the position for the root of the tree.
return value: position of the node having the highest key value.
i. Describe strategy with reasonings and examples to convince that it will work.
ii. Implement the function using C++ and show the source code listing.
Solution
#include
#include
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(data));
else
{
/* 2. Otherwise, recur down the tree */
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
/* return the (unchanged) node pointer */
return node;
}
}
/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node) {
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}
/* Driver program to test sameTree function*/
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
printf(\"\ Minimum value in BST is %d\", minValue(root));
getchar();
return 0;
}
#include
#include
/* A binary tree node has data, pointer to left child
.
Given a newly created Binary Search Tree with the following numerica.pdf
Binary searchtrees
1. CSCE 3110
Data Structures &
Algorithm Analysis
Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110
Binary Search Trees
Reading: Chap. 4 (4.3) Weiss
2. A Taxonomy of Trees
General Trees – any number of children / node
Binary Trees – max 2 children / node
Heaps – parent < (>) children
Binary Search Trees
3. Binary Trees
Binary search tree
Every element has a unique key.
The keys in a nonempty left subtree (right subtree)
are smaller (larger) than the key in the root of
subtree.
The left and right subtrees are also binary search
trees.
4. Binary Search Trees
Binary Search Trees (BST) are a type of
Binary Trees with a special organization of
data.
This data organization leads to O(log n)
complexity for searches, insertions and
deletions in certain types of the BST
(balanced trees).
O(h) in general
5. Binary Search Algorithm
Binary Search algorithm of an array of sorted items
reduces the search space by one half after each comparison
34 41 56 63 72 89 95
0 1 2 3 4 5 6
34 41 56 72 89 95
0 1 2 4 5 6
72 95
34 56
4 6
0 2
6. Organization Rule for BST
• the values in all nodes in the left subtree of a node are less than
the node value
• the values in all nodes in the right subtree of a node are greater
63
than the node values
41 89
72 95
34 56
8. BST Operations: Search
Searching in the BST
method search(key)
• implements the binary search based on comparison of the items
in the tree
• the items in the BST must be comparable (e.g integers, string,
etc.)
The search starts at the root. It probes down, comparing the
values in each node with the target, till it finds the first item equal
to the target. Returns this item or null if there is none.
9. Search in BST - Pseudocode
if the tree is empty
return NULL
else if the item in the node equals the target
return the node value
else if the item in the node is greater than the target
return the result of searching the left subtree
else if the item in the node is smaller than the target
return the result of searching the right subtree
10. Search in a BST: C code
Ptnode search(ptnode root,
int key)
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
if (!root) return NULL;
if (key == root->key) return root;
if (key < root->key)
return search(root->left,key);
return search(root->right,key);
}
11. BST Operations: Insertion
method insert(key)
places a new item near the frontier of the BST while retaining its
organization of data:
starting at the root it probes down the tree till it finds a
node whose left or right pointer is empty and is a logical place
for the new value
uses a binary search to locate the insertion point
is based on comparisons of the new item and values of nodes
in the BST
Elements in nodes must be comparable!
12. Case 1: The Tree is Empty
Set the root to a new node containing the item
Case 2: The Tree is Not Empty
Call a recursive helper method to insert the
item 10 > 7
10
7
5 9 10 > 9
4 6 8 10
13. Insertion in BST - Pseudocode
if tree is empty
create a root node with the new key
else
compare key with the top node
if key = node key
replace the node with the new value
else if key > node key
compare key with the right subtree:
if subtree is empty create a leaf node
else add key in right subtree
else key < node key
compare key with the left subtree:
if the subtree is empty create a leaf node
else add key to the left subtree
14. Insertion into a BST: C code
void insert (ptnode *node, int key)
{
ptnode ptr,
temp = search(*node, key);
if (temp || !(*node)) {
ptr = (ptnode) malloc(sizeof(tnode));
if (IS_FULL(ptr)) {
fprintf(stderr, “The memory is fulln”);
exit(1);
}
ptr->key = key;
ptr->left = ptr->right = NULL;
if (*node)
if (key<temp->key) temp->left=ptr;
else temp->right = ptr;
else *node = ptr;
}
}
15. BST Shapes
The order of supplying the data determines where it is
placed in the BST , which determines the shape of the BST
Create BSTs from the same set of data presented each time
in a different order:
a) 17 4 14 19 15 7 9 3 16 10
b) 9 10 17 4 3 7 14 16 15 19
c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?
16. BST Operations: Removal
removes a specified item from the BST and adjusts the tree
uses a binary search to locate the target item:
starting at the root it probes down the tree till it finds the
target or reaches a leaf node (target not in the tree)
removal of a node must not leave a ‘gap’ in the tree,
17. Removal in BST - Pseudocode
method remove (key)
I if the tree is empty return false
II Attempt to locate the node containing the target using the
binary search algorithm
if the target is not found return false
else the target is found, so remove its node:
Case 1: if the node has 2 empty subtrees
replace the link in the parent with null
Case 2: if the node has a left and a right subtree
- replace the node's value with the max value in the
left subtree
- delete the max node in the left subtree
18. Removal in BST - Pseudocode
Case 3: if the node has no left child
- link the parent of the node
- to the right (non-empty) subtree
Case 4: if the node has no right child
- link the parent of the target
- to the left (non-empty) subtree
19. Removal in BST: Example
Case 1: removing a node with 2 EMPTY SUBTREES
parent 7
cursor 5 9
4 6 8 10
7
Removing 4
replace the link in the
5 9
parent with null
6 8 10
20. Removal in BST: Example
Case 2: removing a node with 2 SUBTREES
- replace the node's value with the max value in the left subtree
- delete the max node in the left subtree
What other element
Removing 7 can be used as
cursor replacement?
cursor
7 6
5 9 5 9
4 6 8 10 4 8 10
21. Removal in BST: Example
Case 3: removing a node with 1 EMPTY SUBTREE
the node has no left child:
link the parent of the node to the right (non-empty) subtree
parent
parent
7 7
cursor
5 9 cursor 5 9
6 8 10 6 8 10
22. Removal in BST: Example
Case 4: removing a node with 1 EMPTY SUBTREE
the node has no right child:
link the parent of the node to the left (non-empty) subtree
Removing 5
parent
parent
7 cursor 7
cursor
5 9 5 9
4 8 10 4 8 10
23. Analysis of BST Operations
The complexity of operations get, insert and
remove in BST is O(h) , where h is the height.
O(log n) when the tree is balanced. The updating
operations cause the tree to become unbalanced.
The tree can degenerate to a linear shape and the
operations will become O (n)
24. Best Case
BST tree = new BST();
tree.insert ("E");
tree.insert ("C");
tree.insert ("D");
tree.insert ("A");
tree.insert ("H");
tree.insert ("F");
tree.insert ("K");
>>>> Items in advantageous order:
Output: K
H
F
E
D
C
A
25. Worst Case
BST tree = new BST();
for (int i = 1; i <= 8; i++)
tree.insert (i);
Output: >>>> Items in worst order:
8
7
6
5
4
3
2
1
26. Random Case
tree = new BST ();
for (int i = 1; i <= 8; i++)
tree.insert(random());
Output: >>>> Items in random order:
X
U
P
O
H
F
B
27. Applications for BST
Sorting with binary search trees
Input: unsorted array
Output: sorted array
Algorithm ?
Running time ?
28. Better Search Trees
Prevent the degeneration of the BST :
A BST can be set up to maintain balance during
updating operations (insertions and removals)
Types of ST which maintain the optimal performance:
splay trees
AVL trees
2-4 Trees
Red-Black trees
B-trees