DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree...

75
DATA STRUCTURES AND ALGORITHMS LECTURE 12 Lect. PhD. Onet-Marian Zsuzsanna Babe¸ s - Bolyai University Computer Science and Mathematics Faculty 2019 - 2020 Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Transcript of DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree...

Page 1: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

DATA STRUCTURES AND ALGORITHMSLECTURE 12

Lect. PhD. Onet-Marian Zsuzsanna

Babes - Bolyai UniversityComputer Science and Mathematics Faculty

2019 - 2020

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 2: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

In Lecture 11

Hash tables

Collision resolution with open addressing

Trees

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 3: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Today

Binary trees

Binary search trees

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 4: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary trees

An ordered tree in which each node has at most two childrenis called binary tree.

In a binary tree we call the children of a node the left childand right child.

Even if a node has only one child, we still have to knowwhether that is the left or the right one.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 5: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - example

A is the leftchild of B

Y is the rightchild of M

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 6: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - Terminology I

A binary tree is called full if every internal node has exactlytwo children.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 7: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - Terminology II

A binary tree is called complete if all leaves are one the samelevel and all internal nodes have exactly 2 children.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 8: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - Terminology III

A binary tree is called almost complete if it is a completebinary tree except for the last level, where nodes arecompleted from left to right (binary heap - structure).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 9: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - Terminology IV

A binary tree is called degenerate if every internal node hasexactly one child (it is actually a chain of nodes).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 10: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - Terminology V

A binary tree is called balanced if the difference between theheight of the left and right subtrees is at most 1 (for everynode from the tree).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 11: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - Terminology VI

Obviously, there are many binary trees that are none of theabove categories, for example:

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 12: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree - properties

A binary tree with n nodes has exactly n − 1 edges (this istrue for every tree, not just binary trees)

The number of nodes in a complete binary tree of height N is2N+1 − 1 (it is 1 + 2 + 4 + 8 + ... + 2N )

The maximum number of nodes in a binary tree of height N is2N+1 − 1 - if the tree is complete.

The minimum number of nodes in a binary tree of height N isN + 1 - if the tree is degenerate.

A binary tree with N nodes has a height between log2N andN − 1.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 13: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree I

Domain of ADT Binary Tree:

BT = {bt | bt binary tree with nodes containing information

of type TElem}

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 14: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree II

init(bt)

descr: creates a new, empty binary tree

pre: true

post: bt ∈ BT , bt is an empty binary tree

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 15: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree III

initLeaf(bt, e)

descr: creates a new binary tree, having only the root with agiven value

pre: e ∈ TElem

post: bt ∈ BT , bt is a binary tree with only one node (itsroot) which contains the value e

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 16: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree IV

initTree(bt, left, e, right)

descr: creates a new binary tree, having a given information inthe root and two given binary trees as children

pre: left, right ∈ BT , e ∈ TElem

post: bt ∈ BT , bt is a binary tree with left child equal to left,right child equal to right and the information from the root is e

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 17: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree V

insertLeftSubtree(bt, left)

descr: sets the left subtree of a binary tree to a given value (ifthe tree had a left subtree, it will be changed)

pre: bt, left ∈ BTpost: bt ′ ∈ BT , the left subtree of bt ′ is equal to left

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 18: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree VI

insertRightSubtree(bt, right)

descr: sets the right subtree of a binary tree to a given value(if the tree had a right subtree, it will be changed)

pre: bt, right ∈ BTpost: bt ′ ∈ BT , the right subtree of bt ′ is equal to right

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 19: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree VII

root(bt)

descr: returns the information from the root of a binary tree

pre: bt ∈ BT , bt 6= Φ

post: root ← e, e ∈ TElem, e is the information from theroot of bt

throws: an exception if bt is empty

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 20: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree VIII

left(bt)

descr: returns the left subtree of a binary tree

pre: bt ∈ BT , bt 6= Φ

post: left ←, l , l ∈ BT , l is the left subtree of bt

throws: an exception if bt is empty

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 21: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree IX

right(bt)

descr: returns the right subtree of a binary tree

pre: bt ∈ BT , bt 6= Φ

post: right ← r , r ∈ BT , r is the right subtree of bt

throws: an exception if bt is empty

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 22: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree X

isEmpty(bt)

descr: checks if a binary tree is empty

pre: bt ∈ BTpost:

empty ←

{True, if bt = Φ

False, otherwise

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 23: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree XI

iterator (bt, traversal, i)

descr: returns an iterator for a binary tree

pre: bt ∈ BT , traversal represents the order in which the treehas to be traversed

post: i ∈ I, i is an iterator over bt that iterates in the ordergiven by traversal

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 24: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree XII

destroy(bt)

descr: destorys a binary tree

pre: bt ∈ BTpost: bt was destroyed

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 25: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

ADT Binary Tree XIII

Other possible operations:

change the information from the root of a binary tree

remove a subtree (left or right) of a binary tree

search for an element in a binary tree

return the number of elements from a binary tree

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 26: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Possible representations

If we want to implement a binary tree, what representationcan we use?

We have several options:

Representation using an array (similar to a binary heap)

Linked representation

with dynamic allocation

on an array

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 27: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Possible representations I

Representation using an array

Store the elements in an array

First position from the array is the root of the tree

Left child of node from position i is at position 2 ∗ i , rightchild is at position 2 ∗ i + 1.

Some special value is needed to denote the place where thereis no element.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 28: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Possible representations II

Disadvantage: depending on the form of the tree, we mightwaste a lot of space.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 29: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Possible representations I

Linked representation with dynamic allocation

We have a structure to represent a node, containing theinformation, the address of the left child and the address ofthe right child (possibly the address of the parent as well).

An empty tree is denoted by the value NIL for the root.

We have one node for every element of the tree.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 30: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Possible representations II

Linked representation on an array

Information from the nodes is placed in an array. The addressof the left and right child is the index where the correspondingelements can be found in the array.

We can have a separate array for the parent as well.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 31: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Possible representations III

We need to know that the root is at position 1 (could be anyposition).

If the array is full, we can allocate a larger one.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 32: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Tree Traversal

A node of a (binary) tree is visited when the program controlarrives at the node, usually with the purpose of performingsome operation on the node (printing it, checking the valuefrom the node, etc.).

Traversing a (binary) tree means visiting all of its nodes.

For a binary tree there are 4 possible traversals:

Preorder

Inorder

Postorder

Level order (breadth first) - the same as in case of a(non-binary) tree

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 33: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Tree Traversal

The preorder, inorder and postorder traversals have recursiveand non-recursive implementations. We are only going todiscuss the recursive implementations.

When we work non-recursively, we need an auxiliary containerin which we can keep some nodes while traversing. This isusually a queue (when we have level-order traversal) or astack (when we have depth first traversal).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 34: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree representation

In the following, for the implementation of the traversalalgorithms, we are going to use the following representationfor a binary tree (this is the linked representation withdynamic allocation):

BTNode:info: TElemleft: ↑ BTNoderight: ↑ BTNode

BinaryTree:root: ↑ BTNode

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 35: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal

In case of a preorder traversal:

Visit the root of the tree

Traverse the left subtree - if exists

Traverse the right subtree - if exists

When traversing the subtrees (left or right) the same preordertraversal is applied (so, from the left subtree we visit the rootfirst and then traverse the left subtree and then the rightsubtree).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 36: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal example

Preorder traversal: A, G, Q, X, Y, J, K, T, N, O

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 37: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal example

Preorder traversal: A, G, Q, X, Y, J, K, T, N, O

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 38: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal - recursive implementation

The simplest implementation for preorder traversal is with arecursive algorithm.

subalgorithm preorder recursive(node) is://pre: node is a ↑ BTNode

if node 6= NIL then@visit [node].infopreorder recursive([node].left)preorder recursive([node].right)

end-ifend-subalgorithm

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 39: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal - recursive implementation

The preorder recursive subalgorithm receives as parameter apointer to a node, so we need a wrapper subalgorithm, onethat receives a BinaryTree and calls the function for the rootof the tree.

subalgorithm preorderRec(tree) is://pre: tree is a BinaryTree

preorder recursive(tree.root)end-subalgorithm

Assuming that visiting a node takes constant time (print theinfo from the node, for example), the whole traversal takesΘ(n) time for a tree with n nodes.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 40: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal - non-recursive implementation

We can implement the preorder traversal algorithm withoutrecursion, using an auxiliary stack to store the nodes.

We start with an empty stack

Push the root of the tree to the stack

While the stack is not empty:

Pop a node and visit it

Push the node’s right child to the stack

Push the node’s left child to the stack

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 41: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal - non-recursive implementation example

Stack: A

Visit A, push children (Stack: T G)

Visit G, push children (Stack: T X Q)

Visit Q, push nothing (Stack: T X)

Visit X, push children (Stack: T J Y)

Visit Y, push nothing (Stack: T J)

Visit J, push child (Stack: T K)

Visit K, push nothing (Stack: T)

Visit T, push children (Stack: O N)

Visit N, push nothing (Stack: O)

Visit O, push nothing (Stack: )

Stack is empty, traversal is complete

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 42: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal - non-recursive implementation

subalgorithm preorder(tree) is://pre: tree is a binary tree

s: Stack //s is an auxiliary stackif tree.root 6= NIL then

push(s, tree.root)end-ifwhile not isEmpty(s) execute

currentNode ← pop(s)@visit currentNodeif [currentNode].right 6= NIL then

push(s, [currentNode].right)end-ifif [currentNode].left 6= NIL then

push(s, [currentNode].left)end-if

end-whileend-subalgorithm

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 43: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder traversal - non - recursive implementation

Time complexity of the non-recursive traversal is Θ(n), andwe also need O(n) extra space (the stack)

Preorder traversal is exactly the same as the depth firsttraversal for a k-ary tree.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 44: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Inorder traversal

In case of inorder traversal:

Traverse the left subtree - if exists

Visit the root of the tree

Traverse the right subtree - if exists

When traversing the subtrees (left or right) the same inordertraversal is applied (so, from the left subtree we traverse theleft subtree, then we visit the root and then traverse the rightsubtree).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 45: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Inorder traversal example

Inorder traversal: Q, G, Y, X, K, J, A, N, T, O

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 46: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Inorder traversal example

Inorder traversal: Q, G, Y, X, K, J, A, N, T, O

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 47: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Inorder traversal - recursive implementation

The simplest implementation for inorder traversal is with arecursive algorithm.

subalgorithm inorder recursive(node) is://pre: node is a ↑ BTNode

if node 6= NIL theninorder recursive([node].left)@visit [node].infoinorder recursive([node].right)

end-ifend-subalgorithm

We need again a wrapper subalgorithm to perform the firstcall to inorder recursive with the root of the tree as parameter.

The traversal takes Θ(n) time for a tree with n nodes.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 48: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Postorder traversal

In case of postorder traversal:

Traverse the left subtree - if exists

Traverse the right subtree - if exists

Visit the root of the tree

When traversing the subtrees (left or right) the samepostorder traversal is applied (so, from the left subtree wetraverse the left subtree, then traverse the right subtree andthen visit the root).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 49: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Postorder traversal example

Postorder traversal: Q, Y, K, J, X, G, N, O, T, A

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 50: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Postorder traversal example

Postorder traversal: Q, Y, K, J, X, G, N, O, T, A

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 51: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Postorder traversal - recursive implementation

The simplest implementation for postorder traversal is with arecursive algorithm.

subalgorithm postorder recursive(node) is://pre: node is a ↑ BTNode

if node 6= NIL thenpostorder recursive([node].left)postorder recursive([node].right)@visit [node].info

end-ifend-subalgorithm

We need again a wrapper subalgorithm to perform the firstcall to postorder recursive with the root of the tree asparameter.

The traversal takes Θ(n) time for a tree with n nodes.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 52: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Level order traversal

In case of level order traversal we first visit the root, then thechildren of the root, then the children of the children, etc.

Level order traversal: A, G, T, Q, X, N, O, Y, J, K

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 53: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary tree iterator

The interface of the binary tree contains the iteratoroperation, which should return an iterator.

This operation receives a parameter that specifies what kindof traversal we want to do with the iterator (preorder, inorder,postorder, level order)

The recursive traversal algorithms discussed so far traverse allthe elements of the binary tree at once, but an iterator has todo element-by-element traversal.

For defining an iterator, we need a non-recursive traversalalgorithm and we have to divide the code into the functions ofan iterator: init, getCurrent, next, valid.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 54: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder, Inorder, Postorder

How to remember the difference between traversals?

Left subtree is always traversed before the right subtree.

The visiting of the root is what changes:

PREorder - visit the root before the left and right

INorder - visit the root between the left and right

POSTorder - visit the root after the left and right

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 55: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Preorder, Inorder, Postorder

How to remember the difference between traversals?

Left subtree is always traversed before the right subtree.

The visiting of the root is what changes:

PREorder - visit the root before the left and right

INorder - visit the root between the left and right

POSTorder - visit the root after the left and right

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 56: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Think about it

Assume you have a binary tree, but you do not know how itlooks like, but you have the preorder and inorder traversal ofthe tree. Give an algorithm for building the tree based onthese two traversals.

For example:

Preorder: A B F G H E L M

Inorder: B G F H A L E M

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 57: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Think about it

Can you rebuild the tree if you have the postorder and theinorder traversal?

Can you rebuild the tree if you have the preorder and thepostorder traversal?

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 58: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Huffman coding

The Huffman coding can be used to encode characters (froman alphabet) using variable length codes.

In order to reduce the total number of bits needed to encodea message, characters that appear more frequently haveshorter codes.

Since we use variable length code for each character, no codecan be the prefix of any other code (if we encode letter E with01 and letter X with 010011, during deconding, when we finda 01, we will not know whether it is E or the beginning of X).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 59: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Huffman coding

When building the Huffman encoding for a message, we firsthave to compute the frequency of every character from themessage, because we are going to define the codes based onthe frequencies.

Assume that we have a message with the following letters andfrequencies

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 60: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Huffman coding

For defining the Huffman code a binary tree is build in thefollowing way:

Start with trees containing only a root node, one for everycharacter. Each tree has a weight, which is frequency of thecharacter.

Put these trees into a Priority Queue

Get the two trees with the least weight - pop two elementsfrom the Priority Queue - (if there is a tie, choose randomly),combine them into one tree which has as weight the sum of thetwo weights and add the resulting tree in the Priority Queue.

Repeat until we get have only one tree.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 61: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Huffman coding

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 62: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Huffman coding

Code for each character can be read from the tree in thefollowing way: start from the root and go towards thecorresponding leaf node. Every time we go left add the bit 0to encoding and when we go right add bit 1.

Code for the characters:

NL - 00000S - 00001T - 0001A - 001E - 01I - 10SP - 11

In order to encode a message, just replace each character withthe corresponding code

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 63: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Huffman coding

Assume we have the following code and we want to decode it:011011000100010011100100000

We do not know where the code of each character ends, butwe can use the previously built tree to decode it.

Start parsing the code and iterate through the tree in thefollowing way:

Start from the root

If the current bit from the code is 0 go to the left child,otherwise go to the right child

If we are at a leaf node we have decoded a character and haveto start over from the root

The decoded message: E I SP T T A SP I E NL

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 64: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary search trees

A Binary Search Tree is a binary tree that satisfies thefollowing property:

if x is a node of the binary search tree then:

For every node y from the left subtree of x, the informationfrom y is less than or equal to the information from x

For every node y from the right subtree of x, the informationfrom y is greater than or equal to the information from x

Obviously, the relation used to order the nodes can beconsidered in an abstract way (instead of having ”≤” as inthe definition).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 65: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Search Tree Example

If we do an inorder traversal of a binary search tree, we willget the elements in increasing order (according to the relationused).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 66: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Search Tree

The terminology and many properties discussed for binary treeis valid for binary search trees as well:

We can have a binary search tree that is full, complete, almostcomplete, degenerate or balanced

The maximum number of nodes in a binary search tree ofheight N is 2N+1 − 1 - if the tree is complete.

The minimum number of nodes in a binary search tree ofheight N is N - if the tree is degenerate.

A binary search tree with N nodes has a height between log2Nand N (we will denote the height of the tree by h).

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 67: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Search Tree

Binary search trees can be used as representation for sortedcontainers: sorted maps, sorted multimaps, priority queues,sorted sets, etc.

In order to implement these containers on a binary searchtree, we need to define the following basic operations:

search for an element

insert an element

remove an element

Other operations that can be implemented for binary searchtrees (and can be used by the containers): getminimum/maximum element, find the successor/predecessorof an element.

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 68: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Search Tree - Representation

We will use a linked representation with dynamic allocation(similar to what we used for binary trees)

BSTNode:info: TElemleft: ↑ BSTNoderight: ↑ BSTNode

BinarySearchTree:root: ↑ BSTNode

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 69: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

Binary Search Tree - search operation

How can we search for an element in a binary search tree?

How can we search for element 15? And for element 14?

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 70: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

BST - search operation - recursive implementation

How can we implement the search algorithm recursively?

function search rec (node, elem) is://pre: node is a BSTNode and elem is the TElem we are searching for

if node = NIL thensearch rec ← false

elseif [node].info = elem then

search rec ← trueelse if [node].info < elem then

search rec ← search rec([node].right, elem)else

search rec ← search rec([node].left, elem)end-if

end-function

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 71: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

BST - search operation - recursive implementation

How can we implement the search algorithm recursively?

function search rec (node, elem) is://pre: node is a BSTNode and elem is the TElem we are searching for

if node = NIL thensearch rec ← false

elseif [node].info = elem then

search rec ← trueelse if [node].info < elem then

search rec ← search rec([node].right, elem)else

search rec ← search rec([node].left, elem)end-if

end-function

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 72: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

BST - search operation - recursive implementation

Complexity of the search algorithm:

O(h) (which is O(n))

Since the search algorithm takes as parameter a node, weneed a wrapper function to call it with the root of the tree.

function search (tree, e) is://pre: tree is a BinarySearchTree, e is the elem we are looking for

search ← search rec(tree.root, e)end-function

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 73: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

BST - search operation - recursive implementation

Complexity of the search algorithm: O(h) (which is O(n))

Since the search algorithm takes as parameter a node, weneed a wrapper function to call it with the root of the tree.

function search (tree, e) is://pre: tree is a BinarySearchTree, e is the elem we are looking for

search ← search rec(tree.root, e)end-function

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 74: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

BST - search operation - non-recursive implementation

How can we define the search operation non-recursively?

function search (tree, elem) is://pre: tree is a BinarySearchTree and elem is the TElem we are searching for

currentNode ← tree.rootfound ← falsewhile currentNode 6= NIL and not found execute

if [currentNode].info = elem thenfound ← true

else if [currentNode].info < elem thencurrentNode ← [currentNode].right

elsecurrentNode ← [currentNode].left

end-ifend-whilesearch ← found

end-function

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS

Page 75: DATA STRUCTURES AND ALGORITHMSmarianzsu/DSA_MI/Lectures/Lecture12.pdfdescr: sets the right subtree of a binary tree to a given value (if the tree had a right subtree, it will be changed)

BST - search operation - non-recursive implementation

How can we define the search operation non-recursively?

function search (tree, elem) is://pre: tree is a BinarySearchTree and elem is the TElem we are searching for

currentNode ← tree.rootfound ← falsewhile currentNode 6= NIL and not found execute

if [currentNode].info = elem thenfound ← true

else if [currentNode].info < elem thencurrentNode ← [currentNode].right

elsecurrentNode ← [currentNode].left

end-ifend-whilesearch ← found

end-function

Lect. PhD. Onet-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS