SlideShare a Scribd company logo
1 of 41
Download to read offline
1
csci 210: Data Structures
Trees
Summary
 Topics
• general trees, definitions and properties
• interface and implementation
• tree traversal algorithms
• depth and height
• pre-order traversal
• post-order traversal
• binary trees
• properties
• interface
• implementation
• binary search trees
• definition
• h-n relationship
• search, insert, delete
• performance
 READING:
• GT textbook chapter 7 and 10.1
2
Trees
 So far we have seen linear structures
• linear: before and after relationship
• lists, vectors, arrays, stacks, queues, etc
 Non-linear structure: trees
• probably the most fundamental structure in computing
• hierarchical structure
• Terminology: from family trees (genealogy)
3
Trees
 store elements hierarchically
 the top element: root
 except the root, each element has a parent
 each element has 0 or more children
root
4
Trees
 Definition
• A tree T is a set of nodes storing elements such that the nodes have a parent-child
relationship that satisfies the following
• if T is not empty, T has a special tree called the root that has no parent
• each node v of T different than the root has a unique parent node w; each node with parent w is a child of w
 Recursive definition
• T is either empty
• or consists of a node r (the root) and a possibly empty set of trees whose roots are the
children of r
 Terminology
• siblings: two nodes that have the same parent are called siblings
• internal nodes
• nodes that have children
• external nodes or leaves
• nodes that don’t have children
• ancestors
• descendants
5
Trees
root
internal nodes
leaves
6
Trees
ancestors of u
u
7
Trees
u
descendants of u
8
Application of trees
 Applications of trees
• class hierarchy in Java
• file system
• storing hierarchies in organizations
9
Tree ADT
 Whatever the implementation of a tree is, its interface is the following
• root()
• size()
• isEmpty()
• parent(v)
• children(v)
• isInternal(v)
• isExternal(v)
• isRoot()
10
Tree Implementation
class Tree {
TreeNode root;
//tree ADT methods..
}
class TreeNode<Type> {
Type data;
int size;
TreeNode parent;
TreeNode firstChild;
TreeNode nextSibling;
getParent();
getChild();
getNextSibling();
}
11
 Depth:
• depth(T, v) is the number of ancestors of v, excluding v itself
 Recursive formulation
• if v == root, then depth(v) = 0
• else, depth(v) is 1 + depth (parent(v))
 Compute the depth of a node v in tree T: int depth(T, v)
 Algorithm:
int depth(T,v) {
if T.isRoot(v) return 0
return 1 + depth(T, T.parent(v))
}
 Analysis:
• O(number of ancestors) = O(depth_v)
• in the worst case the path is a linked-list and v is the leaf
• ==> O(n), where n is the number of nodes in the tree
Algorithms on trees: Depth
12
 Height:
• height of a node v in T is the length of the longest path from v to any leaf
 Recursive formulation:
• if v is leaf, then its height is 0
• else height(v) = 1 + maximum height of a child of v
 Definition: the height of a tree is the height of its root
 Compute the height of tree T: int height(T,v)
 Height and depth are “symmetrical”
 Proposition: the height of a tree T is the maximum depth of one of its leaves.
Algorithms on trees: Height
13
Height
 Algorithm:
int height(T,v) {
if T.isExternal(v) return 0;
int h = 0;
for each child w of v in T do
h = max(h, height(T, w))
return h+1;
}
 Analysis:
• total time: the sum of times spent at all nodes in all recursive calls
• the recursion:
• v calls height(w) recursively on all children w of v
• height() will eventually be called on every descendant of v
• overall: height() is called on each node precisely once, because each node has one parent
• aside from recursion
• for each node v: go through all children of v
– O(1 + c_v) where c_v is the number of children of v
• over all nodes: O(n) + SUM (c_v)
– each node is child of only one node, so its processed once as a child
– SUM(c_v) = n - 1
• total: O(n), where n is the number of nodes in the tree
14
Tree traversals
 A traversal is a systematic way to visit all nodes of T.
 pre-order: root, children
• parent comes before children; overall root first
 post-order: children, root
• parent comes after children; overall root last
void preorder(T, v)
visit v
for each child w of v in T do
preorder(w)
void postorder(T, v)
for each child w of v in T do
postorder(w)
visit v
Analysis: O(n) [same arguments as before]
15
Examples
 Tree associated with a document
 In what order do you read the document?
Pape
r
Title Abstract Ch1 Ch2 Ch3 Refs
1.1 1.2 3.1 3.2
16
Example
 Tree associated with an arithmetical expression
 Write method that evaluates the expression. In what order do you traverse the tree?
+
3 *
-
12 5
+
1 7
17
Binary trees
18
Binary trees
 Definition: A binary tree is a tree such that
• every node has at most 2 children
• each node is labeled as being either a left chilld or a right child
 Recursive definition:
• a binary tree is empty;
• or it consists of
• a node (the root) that stores an element
• a binary tree, called the left subtree of T
• a binary tree, called the right subtree of T
 Binary tree interface
• left(v)
• right(v)
• hasLeft(v)
• hasRight(v)
• + isInternal(v), is External(v), isRoot(v), size(), isEmpty()
19
 In a binary tree
• level 0 has <= 1 node
• level 1 has <= 2 nodes
• level 2 has <= 4 nodes
• ...
• level i has <= 2^i nodes
 Proposition: Let T be a binary tree with n nodes and height h. Then
• h+1 <= n <= 2 h+1 -1
• lg(n+1) - 1 <= h <= n-1
Properties of binary trees
d=0
d=1
d=2
d=3
20
Binary tree implementation
 use a linked-list structure; each node points to its left and right children ; the tree
class stores the root node and the size of the tree
 implement the following functions:
• left(v)
• right(v)
• hasLeft(v)
• hasRight(v)
• isInternal(v)
• is External(v)
• isRoot(v)
• size()
• isEmpty()
• also
• insertLeft(v,e)
• insertRight(v,e)
• remove(e)
• addRoot(e)
data
left right
parentBTreeNode:
21
Binary tree operations
 insertLeft(v,e):
• create and return a new node w storing element e, add w as the left child of v
• an error occurs if v already has a left child
 insertRight(v,e)
 remove(v):
• remove node v, replace it with its child, if any, and return the element stored at v
• an error occurs if v has 2 children
 addRoot(e):
• create and return a new node r storing element e and make r the root of the tree;
• an error occurs if the tree is not empty
 attach(v,T1, T2):
• attach T1 and T2 respectively as the left and right subtrees of the external node v
• an error occurs if v is not external
22
Performance
 all O(1)
• left(v)
• right(v)
• hasLeft(v)
• hasRight(v)
• isInternal(v)
• is External(v)
• isRoot(v)
• size()
• isEmpty()
• addRoot(e)
• insertLeft(v,e)
• insertRight(v,e)
• remove(e)
23
Binary tree traversals
 Binary tree computations often involve traversals
• pre-order: root left right
• post-order: left right root
 additional traversal for binary trees
• in-order: left root right
• visit the nodes from left to right
 Exercise:
• write methods to implement each traversal on binary trees
24
Application: Tree drawing
 Come up with a solution to “draw” a binary tree in the following way. Essentially, we
need to assign coordinate x and y to each node.
• node v in the tree
• x(v) = ?
• y(v) = ?
0 1 2 3
0
1
2
3
4
4 5 6 7
25
Application: Tree drawing
 We can use an in-order traversal and assign coordinate x and y of each node in the
following way:
• x(v) is the number of nodes visited before v in the in-order traversal of v
• y(v) is the depth of v
0 1 2 3
0
1
2
3
4
4 5 6 7
26
Binary tree searching
 write search(v, k)
• search for element k in the subtree rooted at v
• return the node that contains k
• return null if not found
 performance
• ?
27
Binary Search Trees (BST)
 Motivation:
• want a structure that can search fast
• arrays: search fast, updates slow
• linked lists: search slow, updates fast
 Intuition:
• tree combines the advantages of arrays and linked lists
 Definition:
• a BST is a binary tree with the following “search” property
– for any node v allows to search efficientlyv
T1 T2
k
all nodes in T1<= k all node in T2 > k 28
BST
 Example
v
T1 T2
k
<= k > k
29
Sorting a BST
 Print the elements in the BST in sorted order
30
Sorting a BST
 Print the elements in the BST in sorted order.
 in-order traversal: left -node-right
 Analysis: O(n)
//print the elements in tree of v in order
sort(BSTNode v)
if (v == null) return;
sort(v.left());
print v.getData();
sort(v.right());
31
Searching in a BST
32
Searching in a BST
//return the node w such that w.getData() == k or null if such a node
//does not exist
BSTNode search (v, k) {
if (v == null) return null;
if (v.getData() == k) return v;
if (k < v.getData()) return search(v.left(), k);
else return search(v.right(), k)
}
 Analysis:
• search traverses (only) a path down from the root
• does NOT traverse the entire tree
• O(depth of result node) = O(h), where h is the height of the tree
33
Inserting in a BST
 insert 25
34
Inserting in a BST
 insert 25
• There is only one place where 25 can go
 //create and insert node with key k in the right place
 void insert (v, k) {
//this can only happen if inserting in an empty tree
if (v == null) return new BSTNode(k);
if (k <= v.getData()) {
if (v.left() == null) {
//insert node as left child of v
u = new BSTNode(k);
v.setLeft(u);
} else {
return insert(v.left(), k);
}
} else //if (v.getData() > k) {
...
}
}
25
35
Inserting in a BST
 Analysis:
• similar with searching
• traverses a path from the root to the inserted node
• O(depth of inserted node)
• this is O(h), where h is the height of the tree
36
Deleting in a BST
 delete 87
 delete 21
 delete 90
 case 1: delete a leaf x
• if x is left of its parent, set parent(x).left = null
• else set parent(x).right = null
 case 2: delete a node with one child
• link parent(x) to the child of x
 case 2: delete a node with 2 children
• ?? 37
Deleting in a BST
 delete 90
 copy in u 94 and delete 94
• the left-most child of right(x)
 or
 copy in u 87 and delete 87
• the right-most child of left(x)
u
node has <=1 child
node has <=1 child
38
Deleting in a BST
 Analysis:
• traverses a path from the root to the deleted node
• and sometimes from the deleted node to its left-most child
• this is O(h), where h is the height of the tree
39
BST performance
 Because of search property, all operations follow one root-leaf path
• insert: O(h)
• delete: O(h)
• search: O(h)
 We know that in a tree of n nodes
• h >= lg (n+1) - 1
• h <= n-1
 So in the worst case h is O(n)
• BST insert, search, delete: O(n)
• just like linked lists/arrays
40
BST performance
 worst-case scenario
• start with an empty tree
• insert 1
• insert 2
• insert 3
• insert 4
• ...
• insert n
 it is possible to maintain that the height of the tree is Theta(lg n) at all times
• by adding additional constraints
• perform rotations during insert and delete to maintain these constraints
 Balanced BSTs: h is Theta(lg n)
• Red-Black trees
• AVL trees
• 2-3-4 trees
• B-trees
 to find out more.... take csci231 (Algorithms)
41

More Related Content

What's hot

2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
NTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_posterNTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_posterNidhin Pattaniyil
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2jomerson remorosa
 
Binary search trees
Binary search treesBinary search trees
Binary search treesDwight Sabio
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandasPiyush rai
 
Preparation Data Structures 10 trees
Preparation Data Structures 10 treesPreparation Data Structures 10 trees
Preparation Data Structures 10 treesAndres Mendez-Vazquez
 
Funddamentals of data structures
Funddamentals of data structuresFunddamentals of data structures
Funddamentals of data structuresGlobalidiots
 
lecture 13
lecture 13lecture 13
lecture 13sajinsc
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
 
Search data structures
Search data structuresSearch data structures
Search data structuresRavi Pathak
 

What's hot (20)

2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Array
ArrayArray
Array
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
NTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_posterNTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_poster
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Preparation Data Structures 10 trees
Preparation Data Structures 10 treesPreparation Data Structures 10 trees
Preparation Data Structures 10 trees
 
binary tree
binary treebinary tree
binary tree
 
Funddamentals of data structures
Funddamentals of data structuresFunddamentals of data structures
Funddamentals of data structures
 
lecture 13
lecture 13lecture 13
lecture 13
 
Language R
Language RLanguage R
Language R
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
Search data structures
Search data structuresSearch data structures
Search data structures
 

Similar to CSCI 210 Binary Search Tree Guide

part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.pptSuneel61
 
Hub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureHub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureTiểu Hổ
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2Rajendran
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxAhmedEldesoky24
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
CS1027-Trees-2020 lecture one .pdf
CS1027-Trees-2020 lecture one       .pdfCS1027-Trees-2020 lecture one       .pdf
CS1027-Trees-2020 lecture one .pdfAliaaTarek5
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.pptMouDhara1
 
chap11.ppt
chap11.pptchap11.ppt
chap11.pptAswiniJ6
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaVladimir Kostyukov
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptssuser5c874e
 

Similar to CSCI 210 Binary Search Tree Guide (20)

part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Hub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureHub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data Structure
 
s11_bin_search_trees.ppt
s11_bin_search_trees.ppts11_bin_search_trees.ppt
s11_bin_search_trees.ppt
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
 
7.tree
7.tree7.tree
7.tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
CS1027-Trees-2020 lecture one .pdf
CS1027-Trees-2020 lecture one       .pdfCS1027-Trees-2020 lecture one       .pdf
CS1027-Trees-2020 lecture one .pdf
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.ppt
 
chap11.ppt
chap11.pptchap11.ppt
chap11.ppt
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 

Recently uploaded

Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 

Recently uploaded (20)

Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 

CSCI 210 Binary Search Tree Guide

  • 1. 1 csci 210: Data Structures Trees
  • 2. Summary  Topics • general trees, definitions and properties • interface and implementation • tree traversal algorithms • depth and height • pre-order traversal • post-order traversal • binary trees • properties • interface • implementation • binary search trees • definition • h-n relationship • search, insert, delete • performance  READING: • GT textbook chapter 7 and 10.1 2
  • 3. Trees  So far we have seen linear structures • linear: before and after relationship • lists, vectors, arrays, stacks, queues, etc  Non-linear structure: trees • probably the most fundamental structure in computing • hierarchical structure • Terminology: from family trees (genealogy) 3
  • 4. Trees  store elements hierarchically  the top element: root  except the root, each element has a parent  each element has 0 or more children root 4
  • 5. Trees  Definition • A tree T is a set of nodes storing elements such that the nodes have a parent-child relationship that satisfies the following • if T is not empty, T has a special tree called the root that has no parent • each node v of T different than the root has a unique parent node w; each node with parent w is a child of w  Recursive definition • T is either empty • or consists of a node r (the root) and a possibly empty set of trees whose roots are the children of r  Terminology • siblings: two nodes that have the same parent are called siblings • internal nodes • nodes that have children • external nodes or leaves • nodes that don’t have children • ancestors • descendants 5
  • 9. Application of trees  Applications of trees • class hierarchy in Java • file system • storing hierarchies in organizations 9
  • 10. Tree ADT  Whatever the implementation of a tree is, its interface is the following • root() • size() • isEmpty() • parent(v) • children(v) • isInternal(v) • isExternal(v) • isRoot() 10
  • 11. Tree Implementation class Tree { TreeNode root; //tree ADT methods.. } class TreeNode<Type> { Type data; int size; TreeNode parent; TreeNode firstChild; TreeNode nextSibling; getParent(); getChild(); getNextSibling(); } 11
  • 12.  Depth: • depth(T, v) is the number of ancestors of v, excluding v itself  Recursive formulation • if v == root, then depth(v) = 0 • else, depth(v) is 1 + depth (parent(v))  Compute the depth of a node v in tree T: int depth(T, v)  Algorithm: int depth(T,v) { if T.isRoot(v) return 0 return 1 + depth(T, T.parent(v)) }  Analysis: • O(number of ancestors) = O(depth_v) • in the worst case the path is a linked-list and v is the leaf • ==> O(n), where n is the number of nodes in the tree Algorithms on trees: Depth 12
  • 13.  Height: • height of a node v in T is the length of the longest path from v to any leaf  Recursive formulation: • if v is leaf, then its height is 0 • else height(v) = 1 + maximum height of a child of v  Definition: the height of a tree is the height of its root  Compute the height of tree T: int height(T,v)  Height and depth are “symmetrical”  Proposition: the height of a tree T is the maximum depth of one of its leaves. Algorithms on trees: Height 13
  • 14. Height  Algorithm: int height(T,v) { if T.isExternal(v) return 0; int h = 0; for each child w of v in T do h = max(h, height(T, w)) return h+1; }  Analysis: • total time: the sum of times spent at all nodes in all recursive calls • the recursion: • v calls height(w) recursively on all children w of v • height() will eventually be called on every descendant of v • overall: height() is called on each node precisely once, because each node has one parent • aside from recursion • for each node v: go through all children of v – O(1 + c_v) where c_v is the number of children of v • over all nodes: O(n) + SUM (c_v) – each node is child of only one node, so its processed once as a child – SUM(c_v) = n - 1 • total: O(n), where n is the number of nodes in the tree 14
  • 15. Tree traversals  A traversal is a systematic way to visit all nodes of T.  pre-order: root, children • parent comes before children; overall root first  post-order: children, root • parent comes after children; overall root last void preorder(T, v) visit v for each child w of v in T do preorder(w) void postorder(T, v) for each child w of v in T do postorder(w) visit v Analysis: O(n) [same arguments as before] 15
  • 16. Examples  Tree associated with a document  In what order do you read the document? Pape r Title Abstract Ch1 Ch2 Ch3 Refs 1.1 1.2 3.1 3.2 16
  • 17. Example  Tree associated with an arithmetical expression  Write method that evaluates the expression. In what order do you traverse the tree? + 3 * - 12 5 + 1 7 17
  • 19. Binary trees  Definition: A binary tree is a tree such that • every node has at most 2 children • each node is labeled as being either a left chilld or a right child  Recursive definition: • a binary tree is empty; • or it consists of • a node (the root) that stores an element • a binary tree, called the left subtree of T • a binary tree, called the right subtree of T  Binary tree interface • left(v) • right(v) • hasLeft(v) • hasRight(v) • + isInternal(v), is External(v), isRoot(v), size(), isEmpty() 19
  • 20.  In a binary tree • level 0 has <= 1 node • level 1 has <= 2 nodes • level 2 has <= 4 nodes • ... • level i has <= 2^i nodes  Proposition: Let T be a binary tree with n nodes and height h. Then • h+1 <= n <= 2 h+1 -1 • lg(n+1) - 1 <= h <= n-1 Properties of binary trees d=0 d=1 d=2 d=3 20
  • 21. Binary tree implementation  use a linked-list structure; each node points to its left and right children ; the tree class stores the root node and the size of the tree  implement the following functions: • left(v) • right(v) • hasLeft(v) • hasRight(v) • isInternal(v) • is External(v) • isRoot(v) • size() • isEmpty() • also • insertLeft(v,e) • insertRight(v,e) • remove(e) • addRoot(e) data left right parentBTreeNode: 21
  • 22. Binary tree operations  insertLeft(v,e): • create and return a new node w storing element e, add w as the left child of v • an error occurs if v already has a left child  insertRight(v,e)  remove(v): • remove node v, replace it with its child, if any, and return the element stored at v • an error occurs if v has 2 children  addRoot(e): • create and return a new node r storing element e and make r the root of the tree; • an error occurs if the tree is not empty  attach(v,T1, T2): • attach T1 and T2 respectively as the left and right subtrees of the external node v • an error occurs if v is not external 22
  • 23. Performance  all O(1) • left(v) • right(v) • hasLeft(v) • hasRight(v) • isInternal(v) • is External(v) • isRoot(v) • size() • isEmpty() • addRoot(e) • insertLeft(v,e) • insertRight(v,e) • remove(e) 23
  • 24. Binary tree traversals  Binary tree computations often involve traversals • pre-order: root left right • post-order: left right root  additional traversal for binary trees • in-order: left root right • visit the nodes from left to right  Exercise: • write methods to implement each traversal on binary trees 24
  • 25. Application: Tree drawing  Come up with a solution to “draw” a binary tree in the following way. Essentially, we need to assign coordinate x and y to each node. • node v in the tree • x(v) = ? • y(v) = ? 0 1 2 3 0 1 2 3 4 4 5 6 7 25
  • 26. Application: Tree drawing  We can use an in-order traversal and assign coordinate x and y of each node in the following way: • x(v) is the number of nodes visited before v in the in-order traversal of v • y(v) is the depth of v 0 1 2 3 0 1 2 3 4 4 5 6 7 26
  • 27. Binary tree searching  write search(v, k) • search for element k in the subtree rooted at v • return the node that contains k • return null if not found  performance • ? 27
  • 28. Binary Search Trees (BST)  Motivation: • want a structure that can search fast • arrays: search fast, updates slow • linked lists: search slow, updates fast  Intuition: • tree combines the advantages of arrays and linked lists  Definition: • a BST is a binary tree with the following “search” property – for any node v allows to search efficientlyv T1 T2 k all nodes in T1<= k all node in T2 > k 28
  • 30. Sorting a BST  Print the elements in the BST in sorted order 30
  • 31. Sorting a BST  Print the elements in the BST in sorted order.  in-order traversal: left -node-right  Analysis: O(n) //print the elements in tree of v in order sort(BSTNode v) if (v == null) return; sort(v.left()); print v.getData(); sort(v.right()); 31
  • 32. Searching in a BST 32
  • 33. Searching in a BST //return the node w such that w.getData() == k or null if such a node //does not exist BSTNode search (v, k) { if (v == null) return null; if (v.getData() == k) return v; if (k < v.getData()) return search(v.left(), k); else return search(v.right(), k) }  Analysis: • search traverses (only) a path down from the root • does NOT traverse the entire tree • O(depth of result node) = O(h), where h is the height of the tree 33
  • 34. Inserting in a BST  insert 25 34
  • 35. Inserting in a BST  insert 25 • There is only one place where 25 can go  //create and insert node with key k in the right place  void insert (v, k) { //this can only happen if inserting in an empty tree if (v == null) return new BSTNode(k); if (k <= v.getData()) { if (v.left() == null) { //insert node as left child of v u = new BSTNode(k); v.setLeft(u); } else { return insert(v.left(), k); } } else //if (v.getData() > k) { ... } } 25 35
  • 36. Inserting in a BST  Analysis: • similar with searching • traverses a path from the root to the inserted node • O(depth of inserted node) • this is O(h), where h is the height of the tree 36
  • 37. Deleting in a BST  delete 87  delete 21  delete 90  case 1: delete a leaf x • if x is left of its parent, set parent(x).left = null • else set parent(x).right = null  case 2: delete a node with one child • link parent(x) to the child of x  case 2: delete a node with 2 children • ?? 37
  • 38. Deleting in a BST  delete 90  copy in u 94 and delete 94 • the left-most child of right(x)  or  copy in u 87 and delete 87 • the right-most child of left(x) u node has <=1 child node has <=1 child 38
  • 39. Deleting in a BST  Analysis: • traverses a path from the root to the deleted node • and sometimes from the deleted node to its left-most child • this is O(h), where h is the height of the tree 39
  • 40. BST performance  Because of search property, all operations follow one root-leaf path • insert: O(h) • delete: O(h) • search: O(h)  We know that in a tree of n nodes • h >= lg (n+1) - 1 • h <= n-1  So in the worst case h is O(n) • BST insert, search, delete: O(n) • just like linked lists/arrays 40
  • 41. BST performance  worst-case scenario • start with an empty tree • insert 1 • insert 2 • insert 3 • insert 4 • ... • insert n  it is possible to maintain that the height of the tree is Theta(lg n) at all times • by adding additional constraints • perform rotations during insert and delete to maintain these constraints  Balanced BSTs: h is Theta(lg n) • Red-Black trees • AVL trees • 2-3-4 trees • B-trees  to find out more.... take csci231 (Algorithms) 41