Download presentation

Presentation is loading. Please wait.

Published byQuentin Egerton Modified over 2 years ago

1
Algorithms and Data Structures Lecture 4

2
Agenda: Trees – fundamental notions, variations Binary search tree

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

4
Data Structures: Trees

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

7
Data Structures: binary tree

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

12
Data Structures: arbitrary tree

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]

16
Data Structures: Binary search tree

17
Operations: Enumerate Search Insert Remove Predecessor Successor Minimum Maximum

18
Data Structures: Binary search tree -sample

20
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

21
Data Structures: Binary search tree -sample

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

24
Data Structures: Binary search tree -sample

25
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

26
Data Structures: Binary search tree -sample

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

29
Data Structures: Binary search tree -sample

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

33
Data Structures: Binary search tree -sample CASE 2: being removed node has one child node

35
Data Structures: Binary search tree -sample Being removed node has two children

37
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

38
Data Structures: Binary search tree -sample

39
Q&A

Similar presentations

Presentation is loading. Please wait....

OK

Binary Trees Chapter 6.

Binary Trees Chapter 6.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google