Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees Dr. Andrew Wallace PhD BEng(hons) EurIng

Similar presentations


Presentation on theme: "Trees Dr. Andrew Wallace PhD BEng(hons) EurIng"— Presentation transcript:

1 Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

2 Overview Why trees? Terminology Operations Implementation

3 Why trees? Structural relationships Hierarchies Efficient insertion and search Flexibility (moving tree around)

4 Why trees? Examples Navigating (robot) Files systems Family trees (animals, relationships) Assembles Hierarchies Decision making

5

6 Terminology Root (1) Node Leaves Branches Node

7 Terminology Level 2 Level 1 Position Label Parent (1) child sibling

8 Terminology Height depth h(0) h(1)d(0) d(1) level (x) = depth(x) + 1

9 Terminology Finite number of nodes Homogeneous data type Unique path Sub tree is a tree recursive

10 Terminology Ordered Sibling linearly ordered Siblings in a list Unordered Disorganised No significance between siblings

11 Terminology Directed Trees Paths in only one direction Down directed trees lack parents Up directed trees lack children Binary Trees Has (max) two children per node Sorted Relationship / values

12 Terminology Binary tree Left child Right child Number of nodes in a binary tree At least n = 2h – 1 (h = height) Number of leaves in a full binary tree l = (n + 1)/2

13 Terminology Full binary tree Each node has zero or two children Complete binary tree Full at each level with possible exception of last level Balanced binary tree Left and right sub trees have same number of nodes

14 Terminology Height of a binary tree 1 22 4 44 4 8 16 32 2020 2121 2 2323 2424 2525 2k2k What is k? 2 k = n, therefore k = log 2 n

15 Operations Navigate Balancing Enumerate Search Insert Delete Pruning Grafting

16 Operations Navigate Travers Breadth first Depth first 1 23 45

17 Operations Breadth first Create a queue Save all out going nodes to the queue Work through the queue and check each node End when queue is empty 1 23 45

18 Operations

19 Depth first Walking the tree Walk Pre-order Parent traversed before child 1, 2, 4, 5, 3 Post-order Child traversed before parent 4, 5, 2, 3, 1 In-order Left tree, then node, then right tree 4, 2, 5, 1, 3 1 23 45

20 Operations DepthFirst(n)(pre-ordered) if node == NULL return visit(n) DepthFirst(n.left) DepthFirst(n.right)

21 Operations DepthFirst(n)(post-order) if n not = NULL return DepthFirst(n.left) DepthFirst(n.right) visit(n) return

22 Operations

23

24 Balancing a tree Adding sorted data to a tree Tree becomes like a list Rotate left Red-black trees

25 Operations 1 23 4 5 tmp 4 3 n = 3

26 Operations Red-black tree Colours for nodes All leaves coloured black Root is black If a node is red the both children are black Root from any given node to a leaf contains the same number of black nodes NULL

27 Operations Insert(n, data) if n = null then n = new(data) else if n.data < data insert(n.left, data) else if n-data > data insert(n.root, data)

28 Operations Delete 1.Deleting a leaf 2.Deleting a node with one child 3.Deleting a node with two children Case 2 Remove node and replace with child 1 23 4 4

29 Operations Select either in order predecessor or successor, r, of n Copy r to n Recursively call delete on r until case 1 or 2 n = 2 and r = 3 1 23 45 6 3 6

30 Operations Insert in a red-back tree Add a red node in place of a black leaf Then what happens? The insert can violate the rules!

31 Operations 1.n is the root node 2.n parent is black 3.n parent and uncle is red 4.n is added left/right or right/left so parent is red and uncle black 5.n is added left/left or right/right so parent is red and uncle black NULL

32 Operations To fix Colour changes Rotations 1.Colour change red to black 2.OK 3.Colour parent and uncle red, repeat as needed 4.Left rotation 5.Right rotation

33 Operations B A3 1 2 A 1B 2 3 Right Left

34 Operations Time complexity Worse case: Search ever branch and every node O(|V| + |E|) Space complexity O(|V|)

35 Implementation Linked list Label Pointer to parent Array of pointers to children struct { void* pParent; void*pChildren[]; char*strLabel[]; }

36 Implementation pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel NULL

37 Implementation As an array Label Parent N 1 N 2N 3 N 4N 5 LabelParent N 2 N 1 N 5 N 2 N 3 N 1 N 0 N 4N 2

38 Implementation Binary tree as an array Parent 1 23 45 0 1 1 2 2 1 2 3 4 5 6 5 6

39 Implementation Binary tree as an array index 1 23 45 1 2 3 4 5 1 2 3 4 5 6 6 7 8 9 10 6

40 Questions?


Download ppt "Trees Dr. Andrew Wallace PhD BEng(hons) EurIng"

Similar presentations


Ads by Google