3
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

5
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

6
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

9
Data Structures: arbitrary tree Each node may have any number of child nodes

10
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

11
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

15
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]

23
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

28
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

30
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

31
Data Structures: Binary search tree -sample CASE 1: being removed node has no children at all