1 / 34

Algorithms and data structures

Learn about various tree data structures like rooted trees, binary trees, k-ary trees and search algorithms like BFS and DFS. Understand different search techniques like pre-order, in-order, and post-order traversal. Also, learn about binary search trees and their operations like search, insertion, finding minimum/maximum, and successor/predecessor.

Download Presentation

Algorithms and data structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithmsand data structures Rooted trees, Search trees Augmenting BST, TRIE

  2. Rooted tree • Tree – each node can have more than one child (successor) nodes, no circles are possible • Rooted tree = tree with a defined root (starting node) • Binary tree – node can have at most two children (two pointers) • For easier implementation usualy a pointer to parent is defined class Node: key = Nonedata = Noneparent = Noneleft = Noneright = None

  3. Binare tree root

  4. Breadth First Search def BFS(node, key):„function traverses tree level by level" toTraverse = Put(None,node) while toTraverse != None :toTraverse, node = Get(toTraverse) ifnode == None : continue #Process(node) ifnode.key==key: return nodetoTraverse = Put(toTraverse,node.left) toTraverse = Put(toTraverse,node.right) return None • Shall we check if node already visited?

  5. Depth First Search def DFS(node, key):"function traverses tree in depth" toTraverse = Push(None, node) while toTraverse != None : toTraverse, node = Pop(toTraverse) if node == None : continue #Process(node); if node.key==key: return node toTraverse = Push(toTraverse, node.left) toTraverse = Push(toTraverse, node.right) return None • What exaclty is the order of visited nodes? • Can it be changed?

  6. Depth First Search - recursive def DFSRec (node, key):"""function traverses tree in depth recursive version""" if node == None: return None #Process(node) if node.key==key: return noderet = DFSRec (node.left, key)if ret!=None : return retelse : return DFSRec (node.right, key)

  7. K-ary tree • K-ary (K-way) tree: a node has at most K children (in implementation it has k pointers) • A general tree could be represented in notion „Left-child right-sibling”

  8. Traversing K-ary trees class NODE: data = Nonesons = None # = [None, None, None ....] #code of funtion traversing the tree #the old code toTraverse = Put(toTraverse, node.left) toTraverse = Put(toTraverse, node.right) #the new code for son in node.sons: toTraverse = Put(toTraverse, son)

  9. The general tree

  10. Breadth First Search def BFSBrotherSon (node, key): """function traverses Left-child right-sibling tree level by level"" toTraverse = Put(None, node) while toTraverse != None: toTraverse, node = Get(toTraverse)while node != None:#Process(node) if node.key==key: return node toTraverse = Put(toTraverse,node.left) node = node.right return node

  11. Depth First Search def DFSBrotherSon (node, key): """function traverses Left-child right-sibling tree in depth"" toTraverse = Push(None, node) while toTraverse != None: toTraverse, node = Pop(toTraverse)while node != None: #Process(node) if node.key==key: return nodetoTraverse = Push(toTraverse,node.right) node = node.left return None • Order of nodes?

  12. Depth First Search - recursive def DFSBrotherSonRec(node, key): """function traverses Left-child right-sibling tree in depth"" ifnode==None: return None#Process(node)ifnode.key==key: return node ret = DFSBrotherSonRec(node.left, key) ifret !=None : return retelse: return DFSBrotherSonRec(node.right,key) • Can BFSBrotherSonRec be implemented in recursive way?

  13. Binary Search Tree 2 5 3 3 7 7 6 2 8 6 8 h=2 5 h=4 y=Left(x)  y.key<x.key y=Right(x)  y.key≥ x. key

  14. BST: Print # Function prints content of BST # in pre-order way def BSTPreorderPrint(node): if node != None : printnode.key," ",node.dataBSTPreorderPrint(node.left) BSTPreorderPrint(node. right) # Function prints content of BST # inin-order way def BSTInorderPrint(node):if node != None: BSTInorderPrint(node.left) printnode.key," ",node.data BSTInorderPrint(node.right)

  15. 16 19 10 17 20 5 12 1 6 14 15 13 • h=4 • max=20 • min=1 • 13: 1610 12 14  13

  16. BST: Search def BSTSearchRec(node, key):"""Function search for key in BST. It can return None if no key found Recursive version.""" if node == Noneor node.key ==key: return nodeif key < node.key:return BSTSearch(node.left, key)else:return BSTSearch(node.right, key) O(h(T))

  17. BST: Search def BSTSearch(node, key):"""Function search for key in BST. It can return None if no key found Iterative version.""" while node!=None and node.key!=key: if key<node.key : node = node.left else : node = node.rightreturn node O(h(T))

  18. BST: Min/Max O(h(T)) #recursiveversion def BSTMinimumRec(node):if node.left == None:return node else: return BSTMinimum(node.left) #iterative version def BSTMinimum(node):while node.left != None:node = node.left return node O(h(T))

  19. BST: Successor/Predecessor # F.Returns Noneif node.key is MAX def BSTSuccesor(node):if node.right != None: return BSTMinimum(node.right) tmp = node.parent while tmp != Noneandnode == tmp.right:node = tmp tmp = tmp.parent return tmp O(h(T))

  20. Insertion of the node 16 19 10 17 20 5 12 1 6 14 18 15 13 • Inserted node should be a leaf Proceed like a search case. When a None value found – it is a good place for the inserted node.

  21. BST: node insertion def BSTInsertNode (root, node):if root == None : node.parent = None node.left = None node.right = Nonereturn nodetmp =root# i.e. root != Nonewhile tmp!= None: parent = tmpif node.key < tmp.key: tmp = tmp.leftelse: tmp = tmp.right node.parent = parent if node.key < parent.key: parent.left = nodeelse:parent.right = nodereturn root O(h(T))

  22. Removing of the node - no sons case 16 16 19 19 10 10 12 12 20 20 5 5 17 17 14 14 1 1 6 6 15 13 13 Removed node is a leaf – could be replaced by None

  23. Removing of the node - one son case 16 16 19 19 10 10 12 20 20 5 5 17 17 14 14 1 1 6 6 15 15 13 13 Removed node has exactly one child - it could be removed by compression of the branch

  24. Removing of the node - two sons case x 16 17 19 10 19 10 12 20 5 17 12 20 5 18 1 6 18 11 1 6 11 Removed node has two children – • We remove another node, (easy to remove) i.e. (succesor(x) or predecessor(x) ) • We overwrite the data of x by the content of the removed node

  25. BST:node removing def BSTDeleteNode(root, node):ret = rootif node.left==Noneor node.right==None:todel = nodeelse:todel = BSTSuccesor(node)if todel.left!=None: son = todel.leftelse: son = todel.rightif son!=None: son.parent = todel.parent if todel.parent==None: ret=sonelif todel == todel.parent.left: todel.parent.left = son# todel is lson else: todel.parent.right = son# todel is rson if todel != node: node.key = todel.key node.data = todel.data return ret O(h(T))

  26. Complexity of BST operations • All the key operations in trees have complexity that means height of the tree. • It is important to keep the height as small as possible. O(h(T))

  27. Augmenting data structures • Picking of the basic structure • Determination of the additional information • Verification of the time necessary for handling the additional information • Design of the new operations

  28. Order-statistic tree • Additional information – size of sub-tree Applications: • Search for order statistic time = log(n) • Determination of element position time = log(n)

  29. Order-statistic tree 21 20 26 40 7 15 12 17 41 47 2 25 4 10 7 18 4 14 21 30 47 5 4 11 2 16 2 23 1 27 2 20 1 28 38 10 16 19 23 46 1 4 2 17 1 7 1 29 1 7 12 20 39 12 1 3 1 def GetSize(node): if node==None: return 0 else: return node.size x.size = GetSize(x.left) + GetSize(x.right) +1

  30. Interval tree Tree contains intervals [a, b] • The beggining of interval (a) is a • Additional information – max of all the interval ends (b) in the sub tree (i.e. Max = Max(b)) • Up-date in time log(n) Applications: • Search for interval that contains given point • Search for interval that has a non empty intersection with a given interval

  31. Interval tree def Interval_search(root, a, b ):x = rootwhile (x!=None && ([a,b]  [x.a, x.b] == )): if (x.left && x.left.max >= a): x = x.left else: x = x.right return x

  32. Trie – reTRIEval A I B F A B F I E U Y O R N T S  T S R  N  N R S O R  N S T T E U Y D E E T Y R O S     D E  E T Y R O  S M M The sequences could be represented by : • nodes • edges

  33. Trie: Compression of the nodes S S PLA PLA S T N O P TES N A E L E C N L O S O T H E NER ETS M A S T Ó D R T N O D Y E L E C N S O T H O E S T Ó Compression makes the tree modification harder D R D Y

  34. Mixed Tree: Trie+BST P TRIE A L NE ANETS BST GE STE ANES ATES LE

More Related