Algorithms and Data Structures Lecture 4
Agenda: Trees – fundamental notions, variations Binary search tree
Data Structures: Trees Tree is a popular data structure; information is organized in form of trees Tree consists of nodes that may have child nodes Node that has no child nodes is a leaf Each tree has unique root node
Data Structures: Trees
Trees are divided into two main groups: Trees with limited number of child nodes, k- tree Particular case if k=2, binary trees Trees with arbitrary number of child nodes, arbitrary tree
Data Structures: binary tree Each node may have two children Each node includes: key of some type, pointer to a left (first) child node, pointer to a right (second) node and, optionally, pointer to a parent node If some node (child or parent) is absent then pointer is set to NULL
Data Structures: binary tree
Data Structures: arbitrary tree Each node may have any number of child nodes
Data Structures: arbitrary tree Number of children is not known Number of children may vary from node to node Direct representation is not available Arbitrary tree is usually represented by “left-child, right- sibling” principle
Data Structures: arbitrary tree Each node includes: key of some type, pointer to a left child (first), pointer to a next sibling of the same level and, optionally, pointer to a parent node If some node (child, sibling or parent) is absent then pointer is set to NULL
Data Structures: arbitrary tree
Data Structures: Binary search tree Binary search tree is a binary tree which nodes are arranged by keys If x is an arbitrary node of a binary tree and y is arbitrary node of a left sub-tree with root x and z is an arbitrary node of a right sub-tree with root x, then key[y]<=key[x]<=key[z]
Data Structures: Binary search tree
Operations: Enumerate Search Insert Remove Predecessor Successor Minimum Maximum
Data Structures: Binary search tree -sample
Minimum operation – returns node with minimal key value Procedure goes through the tree (from top to bottom) keeping the left side; returns leftmost and bottommost node Maximum operation – returns node with maximal key value Procedure goes through the tree (from top to bottom) keeping the right side; returns rightmost and bottommost node
Data Structures: Binary search tree -sample
Search operation – returns node with the specified key value, if any; otherwise NULL Procedure goes through the tree (from top to bottom) Key value of each visited node is compared against the interested key value Direction of movement depends on result of the comparison; if given key value is greater – procedure continues with right sub-tree, otherwise with left one
Data Structures: Binary search tree -sample
Successor operation – returns next node in terms of key values; NULL if node is not found Predecessor operation – returns previous node in terms of key values ; NULL if node is not found
Data Structures: Binary search tree -sample
Insert operation – adds the specified node to the tree, preserving the ascending order of nodes (by value of key) During the procedure algorithm goes through the tree (from the top to the bottom) Key value of each visited node is compared against the key value of the new node Direction of the movement depends on the result of comparison; if key value of inserted node is greater – procedure continues with right sub-tree, otherwise with left one
Data Structures: Binary search tree -sample
Remove operation – removes the specified node from the tree, preserving the ascending order of nodes (by value of key) We have to consider three cases: Being removed node has no children at all Being removed node has one child node Being removed node has two children
Data Structures: Binary search tree -sample CASE 1: being removed node has no children at all
Data Structures: Binary search tree -sample CASE 2: being removed node has one child node
Data Structures: Binary search tree -sample Being removed node has two children
Data Structures: Binary search tree -sample Enumerate is Θ(n) Search, Minimum, Maximum, successor, Predecessor, Insert and Remove are O(h), h – is a height of a tree
Data Structures: Binary search tree -sample
Q&A