Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
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 Root (1) Node Leaves Branches Node
Terminology Level 2 Level 1 Position Label Parent (1) child sibling
Terminology Height depth h(0) h(1)d(0) d(1) level (x) = depth(x) + 1
Terminology Finite number of nodes Homogeneous data type Unique path Sub tree is a tree recursive
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 Height of a binary tree k2k What is k? 2 k = n, therefore k = log 2 n
Operations Navigate Balancing Enumerate Search Insert Delete Pruning Grafting
Operations Navigate Travers Breadth first Depth first
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
Operations
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,
Operations DepthFirst(n)(pre-ordered) if node == NULL return visit(n) DepthFirst(n.left) DepthFirst(n.right)
Operations DepthFirst(n)(post-order) if n not = NULL return DepthFirst(n.left) DepthFirst(n.right) visit(n) return
Operations
Balancing a tree Adding sorted data to a tree Tree becomes like a list Rotate left Red-black trees
Operations tmp 4 3 n = 3
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
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 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
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 =
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.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
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
Operations B A3 1 2 A 1B 2 3 Right Left
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 pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel NULL
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
Implementation Binary tree as an array Parent
Implementation Binary tree as an array index
Questions?