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
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
Presentation On Binary Search Tree using Linked List Concept which includes Traversing the tree in Inorder, Preorder and Postorder Methods and also searching the element in the Tree
Here are my slides for my preparation class for possible students for the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
Presentation On Binary Search Tree using Linked List Concept which includes Traversing the tree in Inorder, Preorder and Postorder Methods and also searching the element in the Tree
Here are my slides for my preparation class for possible students for the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
1) Tree
2) General Tree
3) Binary Tree
4) Full Binay Tree, Complete Binay Tree
5) Binary Tree Traversal (DFS & BFS)
6) Binary Search Tree
7) Reconstruction of Binay Tree
8) Expression Tree
9) Evaluation of postfix expression
10) Infix to Prefix using stack
11) Infix to Postfix using stack
12) Threaded Binary Tree
13) AVL-Tree
14) AVL-Tree Rotation
In this chapter we will discuss tree data structures, like trees and graphs. The abilities of these data structures are really important for the modern programming. Each of this data structures is used for building a model of real life problems, which are efficiently solved using this model. We will explain what tree data structures are and will review their main advantages and disadvantages. We will present example implementations and problems showing their practical usage. We will focus on binary trees, binary search trees and self-balancing binary search tree. We will explain what graph is, the types of graphs, how to represent a graph in the memory (graph implementation) and where graphs are used in our life and in the computer technologies. We will see where in .NET Framework self-balancing binary search trees are implemented and how to use them.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
17. Java data structures trees representation and traversalIntro C# Book
In this chapter we will discuss tree data structures, like trees and graphs. The abilities of these data structures are really important for the modern programming. Each of this data structures is used for building a model of real life problems, which are efficiently solved using this model.
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
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
1) Tree
2) General Tree
3) Binary Tree
4) Full Binay Tree, Complete Binay Tree
5) Binary Tree Traversal (DFS & BFS)
6) Binary Search Tree
7) Reconstruction of Binay Tree
8) Expression Tree
9) Evaluation of postfix expression
10) Infix to Prefix using stack
11) Infix to Postfix using stack
12) Threaded Binary Tree
13) AVL-Tree
14) AVL-Tree Rotation
In this chapter we will discuss tree data structures, like trees and graphs. The abilities of these data structures are really important for the modern programming. Each of this data structures is used for building a model of real life problems, which are efficiently solved using this model. We will explain what tree data structures are and will review their main advantages and disadvantages. We will present example implementations and problems showing their practical usage. We will focus on binary trees, binary search trees and self-balancing binary search tree. We will explain what graph is, the types of graphs, how to represent a graph in the memory (graph implementation) and where graphs are used in our life and in the computer technologies. We will see where in .NET Framework self-balancing binary search trees are implemented and how to use them.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
17. Java data structures trees representation and traversalIntro C# Book
In this chapter we will discuss tree data structures, like trees and graphs. The abilities of these data structures are really important for the modern programming. Each of this data structures is used for building a model of real life problems, which are efficiently solved using this model.
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
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.
spell checker with python 3
How it works ?
Main.py is the main file , Spell checker checks the entered word or phrase and if it's wrong it suggests alternative words .
The project on GitHub:
https://github.com/amrelarabi/spell-checker-with-python-3/
For more information you can visit :
http://www.motwr.com/2017/03/python3-spell-checker.html
On the need for applications aware adaptive middleware in real-time RDF data ...Zia Ush Shamszaman
Introduction
Problems
Analysis
Evaluation
Adaptive approach
Conclusion
Streams are originated from a variety of sources (physical or virtual sensors)
Data is produced continuously (usually at short intervals) with a time stamp.
Queries over RDF streams are executed once but continuously monitored to report any change.
Different RSP Engines
CQELS, C-SPARQL, SPARQLstream, EPSPARQL, ETALIS, SPARKWAVE, etc.
Various features of RSP engines
Query
Input Data Model
Execution Strategy
Output Data Model
Input rate
Memory consumptions
RSP Engines Characteristics Categorization
Design Time includes aspects such as input data model, language to define processing rules, operational semantics, and supported streaming operators, etc.
Run Time includes aspects such as Memory, Latency, processing & optimization techniques, quality of service (QoS), load balancing, etc.
Is there any single best RSP engine that can adapt to the diverse application requirements?
•There is no single best system:
•according to the evaluation results and
•few RSP
benchmarks.
•The different features of RSP affects:
•user satisfaction, and
•RSP engines performance
•We need an adaptive middleware which can:
•bridge the gap between applications and RSP engines
•can satisfy diverse user requirements
On the need for applications aware adaptive middleware in real-time RDF data ...Zia Ush Shamszaman
RSP Engines e.g. CQELS, C-SPARQL, SPARQLstream, EPSPARQL, ETALIS, SPARKWAVE differ in various aspects,
Query language
Input data model
Execution strategy
Output data Model, and more.
The performance of existing RSP engines are affected by the different query sets and data sets in different benchmarks.
An adaptive approach to RSP can improve efficiency and correctness by serving a broader category of application requirements.
The adaptive approach is capable to adapt to dynamic application requirements and properties of data streams at run-time with improved scalability.
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, init
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.
linear search and binary search, 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
Opendatabay - Open Data Marketplace.pptxOpendatabay
Opendatabay.com unlocks the power of data for everyone. Open Data Marketplace fosters a collaborative hub for data enthusiasts to explore, share, and contribute to a vast collection of datasets.
First ever open hub for data enthusiasts to collaborate and innovate. A platform to explore, share, and contribute to a vast collection of datasets. Through robust quality control and innovative technologies like blockchain verification, opendatabay ensures the authenticity and reliability of datasets, empowering users to make data-driven decisions with confidence. Leverage cutting-edge AI technologies to enhance the data exploration, analysis, and discovery experience.
From intelligent search and recommendations to automated data productisation and quotation, Opendatabay AI-driven features streamline the data workflow. Finding the data you need shouldn't be a complex. Opendatabay simplifies the data acquisition process with an intuitive interface and robust search tools. Effortlessly explore, discover, and access the data you need, allowing you to focus on extracting valuable insights. Opendatabay breaks new ground with a dedicated, AI-generated, synthetic datasets.
Leverage these privacy-preserving datasets for training and testing AI models without compromising sensitive information. Opendatabay prioritizes transparency by providing detailed metadata, provenance information, and usage guidelines for each dataset, ensuring users have a comprehensive understanding of the data they're working with. By leveraging a powerful combination of distributed ledger technology and rigorous third-party audits Opendatabay ensures the authenticity and reliability of every dataset. Security is at the core of Opendatabay. Marketplace implements stringent security measures, including encryption, access controls, and regular vulnerability assessments, to safeguard your data and protect your privacy.
StarCompliance is a leading firm specializing in the recovery of stolen cryptocurrency. Our comprehensive services are designed to assist individuals and organizations in navigating the complex process of fraud reporting, investigation, and fund recovery. We combine cutting-edge technology with expert legal support to provide a robust solution for victims of crypto theft.
Our Services Include:
Reporting to Tracking Authorities:
We immediately notify all relevant centralized exchanges (CEX), decentralized exchanges (DEX), and wallet providers about the stolen cryptocurrency. This ensures that the stolen assets are flagged as scam transactions, making it impossible for the thief to use them.
Assistance with Filing Police Reports:
We guide you through the process of filing a valid police report. Our support team provides detailed instructions on which police department to contact and helps you complete the necessary paperwork within the critical 72-hour window.
Launching the Refund Process:
Our team of experienced lawyers can initiate lawsuits on your behalf and represent you in various jurisdictions around the world. They work diligently to recover your stolen funds and ensure that justice is served.
At StarCompliance, we understand the urgency and stress involved in dealing with cryptocurrency theft. Our dedicated team works quickly and efficiently to provide you with the support and expertise needed to recover your assets. Trust us to be your partner in navigating the complexities of the crypto world and safeguarding your investments.
Techniques to optimize the pagerank algorithm usually fall in two categories. One is to try reducing the work per iteration, and the other is to try reducing the number of iterations. These goals are often at odds with one another. Skipping computation on vertices which have already converged has the potential to save iteration time. Skipping in-identical vertices, with the same in-links, helps reduce duplicate computations and thus could help reduce iteration time. Road networks often have chains which can be short-circuited before pagerank computation to improve performance. Final ranks of chain nodes can be easily calculated. This could reduce both the iteration time, and the number of iterations. If a graph has no dangling nodes, pagerank of each strongly connected component can be computed in topological order. This could help reduce the iteration time, no. of iterations, and also enable multi-iteration concurrency in pagerank computation. The combination of all of the above methods is the STICD algorithm. [sticd] For dynamic graphs, unchanged components whose ranks are unaffected can be skipped altogether.
1. First Year BA (IT)
CT1120 Algorithms
Lecture 19
Dr. Zia Ush Shamszaman
z.shamszaman1@nuigalway.ie
1
School of Computer Science, College of Science and Engineering
28-02-2020
2. Overview
• Binary Search Tree (BST)
– BST Algorithms
– Python code for BST
– AVL
– Red-Black Tree
– Splay Tree
– Problem Solving using BST in Python
• Feedback and Assessment
228-02-2020
3. Binary Search
Tree (BST)
• Binary Search Tree is a node-based binary
tree data structure which has the following
properties
– The left subtree of a node contains only nodes
with keys lesser than the node’s key.
– The right subtree of a node contains only nodes
with keys greater than the node’s key.
– The left and right subtree each must also be a
binary search tree.
28-02-2020 3
11. BT vs BST
• A binary tree is simply a tree in which each node can have at
most two children.
• A binary search tree is a binary tree in which the nodes are
assigned values, with the following restrictions :
– No duplicate values.
– The left subtree of a node can only have values less than the node
– The right subtree of a node can only have values greater than the
node and recursively defined
– The left subtree of a node is a binary search tree.
– The right subtree of a node is a binary search tree.
28-02-2020 11
14. Algorithm of BST
28-02-2020 14
A) compare ITEM with the root node N of the tree
i) if ITEM < N, proceed to the left child of N.
ii) if ITEM > N, proceed to the right child of N.
B) repeat step (A) until one of the following occurs
i) we meet a node N such that ITEM=N, i.e.
search is successful.
ii) we meet an empty sub tree, i.e. the search is
unsuccessful.
16. Adelson-Velskii and Landis (AVL) Tree
28-02-2020 16
• An AVL tree is a binary search tree which has the
following properties:
• The sub-trees of every node differ in height by
at most one.
• Every sub-tree is an AVL tree.
Balance requirement for an AVL
tree: the left and right sub-trees
differ by at most 1 in height.
18. AVL Tree?
28-02-2020 18
YES
Examination shows that each left sub-tree has a height
1 greater than each right sub-tree.
NO
Sub-tree with root 8 has height 4 and sub-
tree with root 18 has height 2
19. Red-Black Tree
28-02-2020 19
• Red-Black Tree is a self-balancing Binary Search
Tree (BST) where every node follows following rules:
• Every node has a color either red or black.
• Root of tree is always black.
• There are no two adjacent red nodes (A red node
cannot have a red parent or red child).
• Every path from a node (including root) to any of
its descendant NIL node has the same number of
black nodes.
20. Splay Tree
28-02-2020 20
• Splay tree provides performance better by
optimizing the placement of nodes.
• It makes the recently accessed nodes in the top of
the tree and hence providing better performance.
• It’s suitable for cases where there are large
number of nodes but only few of them are accessed
frequently.
https://www.cs.usfca.edu/~galles/visualization/SplayTree.html
Browse the link below for visulalization of Splay tree
21. 28-02-2020 21
# A O(n^2) Python3 program for construction of BST from preorder
traversal
# A binary tree node
class Node():
# A constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# constructTreeUtil.preIndex is a static variable of
# function constructTreeUtil
# Function to get the value of static variable
# constructTreeUtil.preIndex
def getPreIndex():
return constructTreeUtil.preIndex
# Function to increment the value of static variable
# constructTreeUtil.preIndex
def incrementPreIndex():
constructTreeUtil.preIndex += 1
# A recurseive function to construct Full from pre[].
# preIndex is used to keep track of index in pre[[].
def constructTreeUtil(pre, low, high, size):
# Base Case
if( getPreIndex() >= size or low > high):
return None
# The first node in preorder traversal is root. So take
# the node at preIndex from pre[] and make it root,
# and increment preIndex
root = Node(pre[getPreIndex()])
incrementPreIndex()
# If the current subarray has onlye one element,
# no need to recur
if low == high :
return root
# Search for the first element greater than root
for i in range(low, high+1):
if (pre[i] > root.data):
break
# Use the index of element found in preorder to divide
# preorder array in two parts. Left subtree and right
# subtree
root.left = constructTreeUtil(pre, getPreIndex(), i-1 , size)
root.right = constructTreeUtil(pre, i, high, size)
return root
# The main function to construct BST from given preorder
# traversal. This function mailny uses constructTreeUtil()
def constructTree(pre):
size = len(pre)
constructTreeUtil.preIndex = 0
return constructTreeUtil(pre, 0, size-1, size)
def printInorder(root):
if root is None:
return
printInorder(root.left)
print root.data,
printInorder(root.right)
# Driver program to test above function
pre = [10, 5, 1, 7, 40, 50]
root = constructTree(pre)
print "Inorder traversal of the constructed tree:"
printInorder(root)
Python code for Preorder BST
22. Problem-1
• Write a Python program to create a Balanced
Binary Search Tree (BST) using an array (given)
elements where array elements are sorted in
ascending order.
28-02-2020 22
Online Python Compilter:
http://pythontutor.com/live.html#mode=edit
https://www.onlinegdb.com/online_python_debugger
23. 28-02-2020 23
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def sorted_array_to_bst(nums):
if not nums:
return None
mid_val = len(nums)//2
node = TreeNode(nums[mid_val])
node.left =
sorted_array_to_bst(nums[:mid_val
])
node.right =
sorted_array_to_bst(nums[mid_val+
1:])
return node
def preOrder(node):
if not node:
return
print(node.val)
preOrder(node.left)
preOrder(node.right)
result = sorted_array_to_bst([1, 2,
3, 4, 5, 6, 7])
preOrder(result)
Solution of the Problem 1
24. Problem 2
• Write a Python program to find the closest
value of a given target value in a given non-
empty Binary Search Tree (BST) of unique
values.
28-02-2020 24
Online Python Compilter:
http://pythontutor.com/live.html#mode=edit
https://www.onlinegdb.com/online_python_debugger
25. Solution of the Problem 2
28-02-2020 25
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def closest_value(root, target):
a = root.val
kid = root.left if target < a else
root.right
if not kid:
return a
b = closest_value(kid, target)
return min((a,b), key=lambda x:
abs(target-x))
root = TreeNode(8)
root.left = TreeNode(5)
root.right = TreeNode(14)
root.left.left = TreeNode(4)
root.left.right = TreeNode(6)
root.left.right.left = TreeNode(8)
root.left.right.right = TreeNode(7)
root.right.right = TreeNode(24)
root.right.right.left = TreeNode(22)
result = closest_value(root, 19)
print(result)
26. Problem 3
• Write a Python program to check whether a given a binary tree is a valid
binary search tree (BST) or not
28-02-2020 26
Let a binary search tree (BST) is defined as follows:
The left subtree of a node contains only nodes with keys less than the
node's key.
The right subtree of a node contains only nodes with keys greater than the
node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:
2
/
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/
2 3
Binary tree [1,2,3], return false.
27. Problem 4
28-02-2020 27
• Write a Python program to delete a node with
the given key in a given Binary search tree
(BST).
Note: Search for a node to remove. If the node is found,
delete the node.
28. Solution to the Problem 4
28-02-2020 28
# Definition: Binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def delete_Node(root, key):
# if root doesn't exist, just return it
if not root:
return root
# Find the node in the left subtree if key value is less than root value
if root.val > key:
root.left = delete_Node(root.left, key)
# Find the node in right subtree if key value is greater than root value,
elif root.val < key:
root.right= delete_Node(root.right, key)
# Delete the node if root.value == key
else:
# If there is no right children delete the node and new
root would be root.left
if not root.right:
return root.left
# If there is no left children delete the node and new
root would be root.right
if not root.left:
return root.right
# If both left and right children exist in the node replace its value with
# the minmimum value in the right subtree. Now delete that minimum
node
# in the right subtree
temp_val = root.right
mini_val = temp_val.val
while temp_val.left:
temp_val = temp_val.left
mini_val = temp_val.val
# Replace value
root.val = mini
# Delete the minimum node in right subtree
root.right = deleteNode(root.right,root.val)
return root
def preOrder(node):
if not node:
return
print(node.val)
preOrder(node.left)
preOrder(node.right)
root = TreeNode(5)
root.left = TreeNode(3)
root.right = TreeNode(6)
root.left.left = TreeNode(2)
root.left.right = TreeNode(4)
root.left.right.left = TreeNode(7)
print("Original node:")
print(preOrder(root))
result = delete_Node(root, 4)
print("After deleting specified node:")
print(preOrder(result))
34. • Given the following sequence of stack
operations, what is the top item on the stack
when the sequence is complete?
28-02-2020 34
m = Stack()
m.push('x')
m.push('y')
m.pop()
m.push('z')
m.peek()
35. • Given the following sequence of stack
operations, what is the top item on the stack
when the sequence is complete?
28-02-2020 35
m = Stack()
m.push('x')
m.push('y')
m.pop()
m.push('z')
m.peek()
36. What this code is doing?
28-02-2020 36
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
print('nElements poped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('nStack after elements are poped:')
print(stack)
37. Stack implementation code
28-02-2020 37
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
print('nElements poped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('nStack after elements are poped:')
print(stack)
OUTPUT
Initial stack
['a', 'b', 'c']
Elements poped from stack:
c
b
a
Stack after elements are poped:
[]
38. What this code is doing?
28-02-2020 38
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
print("nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("nQueue after removing elements")
print(queue)
39. What this code is doing?
28-02-2020 39
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
print("nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("nQueue after removing elements")
print(queue)
Output
Initial queue
['a', 'b', 'c']
Elements dequeued from queue
a
b
c
Queue after removing elements
[]
40. Tree
• A Tree is a collection of elements called nodes.
• One of the node is distinguished as a root, along with a
relation (“parenthood”) that places a hierarchical
structure on the nodes.
28-02-2020 40
41. Applications of Tree
28-02-2020 41
Main applications of trees include:
1. Manipulate hierarchical data.
2. Make information easy to search (see tree
traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for
visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see
business chess).
42. Binary Tree
28-02-2020 42
• A tree whose elements have at most 2 children is called a
binary tree.
• Since each element in a binary tree can have only 2
children, we typically name the left child and right child.
43. Binary Tree
28-02-2020 43
• A tree whose elements have at most 2 children is called a
binary tree.
• Since each element in a binary tree can have only 2
children, we typically name the left child and right child.
((7+3)∗(5−2))
44. Binary Tree
28-02-2020 44
• A tree whose elements have at most 2 children is called a
binary tree.
• Since each element in a binary tree can have only 2
children, we typically name the left child and right child.
• A Binary Tree node contains following parts.
• Data
• Pointer to left child
• Pointer to right child
45. Python code of
Binary Tree
28-02-2020 45
# A class that represents an individual node in a
# Binary Tree
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
# create root
root = Node(1)
''' following is the tree after above statement
1
/
None None'''
root.left = Node(2);
root.right = Node(3);
''' 2 and 3 become left and right children of 1
1
/
2 3
/ /
None None None None''’
root.left.left = Node(4);
'''4 becomes left child of 2
1
/
2 3
/ /
4 None None None
/
None None'''
"__init__" is a reseved method in python classes. It is
called as a constructor in object oriented terminology. This
method is called when an object is created from a class
and it allows the class to initialize the attributes of the
class.
46. Tree traversal
28-02-2020 46
Following are the generally used ways for traversing trees:
• Inorder: We recursively do an inorder traversal on the
left subtree, visit the root node, and finally do a recursive
inorder traversal of the right subtree.
• Preorder: We visit the root node first, then recursively
do a preorder traversal of the left subtree, followed by a
recursive preorder traversal of the right subtree.
• Postorder: We recursively do a postorder traversal of
the left subtree and the right subtree followed by a visit
to the root node.
49. Tree traversal code
28-02-2020 49
# A class that represents an individual node in a
Binary Tree
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
# A function to do inorder tree traversal
def printInorder(root):
if root:
# First recur on left child
printInorder(root.left)
# then print the data of node
print(root.val),
# now recur on right child
printInorder(root.right)
# A function to do postorder tree traversal
def printPostorder(root):
if root:
# First recur on left child
printPostorder(root.left)
# the recur on right child
printPostorder(root.right)
# now print the data of node
print(root.val),
# A function to do preorder tree traversal
def printPreorder(root):
if root:
# First print the data of node
print(root.val),
# Then recur on left child
printPreorder(root.left)
# Finally recur on right child
printPreorder(root.right)
# Driver code
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print ("nPreorder traversal of binary tree is")
printPreorder(root)
print ("nInorder traversal of binary tree is")
printInorder(root)
print ("nPostorder traversal of binary tree is")
printPostorder(root)
50. Inorder traversal without recursion
28-02-2020 50
# A binary tree node
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Iterative function for inorder tree traversal
def inOrder(root):
# Set current to root of binary tree
current = root
stack = [] # initialize stack
done = 0
while True:
# Reach the left most Node of the current Node
if current is not None:
# Place pointer to a tree node on the stack
# before traversing the node's left subtree
stack.append(current)
current = current.left
# BackTrack from the empty subtree and visit the Nod
# at the top of the stack; however, if the stack is
# empty you are done
elif(stack):
current = stack.pop()
print(current.data, end=" ")
# Python 3 printing
# We have visited the node and its left
# subtree. Now, it's right subtree's turn
current = current.right
else:
break
print()
# Driver program to test above function
""" Constructed binary tree is
1
/
2 3
/
4 5 """
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
inOrder(root)
51. Priority Queue
28-02-2020 51
• A priority queue acts like a queue in that you dequeue an item by
removing it from the front.
• In a priority queue the logical order of items inside a queue is
determined by their priority.
• The highest priority items are at the front of the queue and the lowest
priority items are at the back.
• When you enqueue an item on a priority queue, the new item may move
all the way to the front.
52. Binary Heaps
28-02-2020 52
• A Heap is a special Tree-based data structure in
which the tree is a complete binary tree. Generally,
Heaps can be of two types:
• Max-Heap: The key present at the root node must be
greatest among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in
that Binary Tree.
• Min-Heap: The key present at the root node must be
minimum among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in
that Binary Tree.
53. Binary Heap Operation
28-02-2020 53
The basic operations we will implement for our
binary heap are as follows:
• BinaryHeap() creates a new, empty, binary heap.
• insert(k) adds a new item to the heap.
• findMin() returns the item with the minimum key value,
leaving item in the heap.
• delMin() returns the item with the minimum key value,
removing the item from the heap.
• isEmpty() returns true if the heap is empty, false
otherwise.
• size() returns the number of items in the heap.
• buildHeap(list) builds a new heap from a list of keys.