Trees Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected] Overview • Why trees? • Terminology • Operations • Implementation Why trees? • Structural relationships • Hierarchies • Efficient insertion and search • Flexibility (moving tree around) Why trees? • Examples • • • • • • Navigating (robot) Files systems Family trees (animals, relationships) Assembles Hierarchies Decision making Terminology Leaves Terminology Level 1 Position Level 2 Terminology Height h(0) d(0) depth h(1) d(1) level (x) = depth(x) + 1 Terminology recursive • Finite number of nodes • Homogeneous data type Terminology • Ordered • Sibling linearly ordered • Siblings in a list • Unordered • Disorganised • No significance between siblings 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 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 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 Terminology 20 • Height of a binary tree 21 1 2 2 What is k? 22 𝒏 3 = 1, where n = number of nodes 2 𝟐𝒌 4 at the bottom level 2k = n, therefore k = log2n 2 25 2k 4 4 8 16 32 4 4 Operations • Navigate • Balancing • Enumerate • Search • Insert • Delete • Pruning • Grafting Operations • Navigate 1 • Travers • Breadth first • Depth first 2 4 3 5 Operations 1 • 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 2 4 3 5 Operations BreadthFirst(T, n) create Q create set N add(N, n) enqueue(Q, n) while Q not empty t ← Q.dequeue() if t = what we want return for all child nodes(T) u ← n.GetChildNode(t) if u not in N add(N, u) enqueue(Q, u) return Operations 1 • Depth first • Walking the tree • Walk 2 3 • 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 4 5 Operations DepthFirst(n) if node == NULL return visit(n) DepthFirst(n.left) DepthFirst(n.right) (pre-ordered) Operations DepthFirst(n) if n not = NULL return DepthFirst(n.left) DepthFirst(n.right) visit(n) return (post-order) Operations DepthFirst(n) (post-order) create stack(S) n.Last ← null while not S.empty or n not = null if n not = null S.push(n) n ← n.left else n.peek ← S.peek if n.peek.right not = null and n.last not = n.peek n ← n.peek else visit(n) n.last ← S.pop() return Operations DepthFirst(n) create stack(S) while not S.isEmpty or n not = NULL if n not = null then do S.push(n) n ← n.left else n ← S.pop() visit(n) n ← n.right Return (in-order) Operations • Balancing a tree • Adding sorted data to a tree • Tree becomes like a list • Rotate left • Red-black trees Operations 1 Rotate left(n) n.tmp ← n.right n.right ← n.tmp.left n.tmp.left ← n n ← n.tmp return n 2 3 tmp 4 3 n=3 4 5 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 NULL NULL NULL NULL 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) Operations 1 • Delete 1. Deleting a leaf 2. Deleting a node with one child 3. Deleting a node with two children 42 • Case 2 • Remove node and replace with child 4 3 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 1 6 3 3 2 4 n = 2 and r = 3 5 6 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! Operations 1. 2. 3. 4. n is the root node n parent is black n parent and uncle is red 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 NULL NULL NULL NULL Operations • To fix • Colour changes • Rotations 1. 2. 3. 4. 5. Colour change red to black OK Colour parent and uncle red, repeat as needed Left rotation Right rotation Operations B A A 3 1 B Right 1 2 2 Left 3 Operations • Time complexity • Worse case: • Search ever branch and every node • O(|V| + |E|) • Space complexity • O(|V|) Implementation • Linked list • Label • Pointer to parent • Array of pointers to children struct { void* pParent; void* pChildren[]; char* strLabel[]; } Implementation pLabel pParent pLabel pChildren pParent pLabel NULL pChildren pParent pLabel pChildren pParent pChildren pLabel pParent pChildren Implementation N1 • As an array • Label • Parent Label Parent N2 N1 N5 N2 N3 N1 N1 N0 N4 N2 N2 N4 N3 N5 Implementation 1 • Binary tree as an array • Parent 1 0 2 1 3 1 4 2 5 2 6 5 2 3 4 5 6 Implementation • Binary tree as an array • index 1 1 1 2 2 3 3 4 4 5 5 6 2 3 4 5 7 8 9 10 6 6 Questions?
© Copyright 2026 Paperzz