Professional Documents
Culture Documents
Non-Linear Data Structures
Non-Linear Data Structures
We will have a look at the following; Trees Tree terminology Binary Search Trees Tree Traversal
A Tree
In terms of data structures, what do we mean by a tree?
Definition: - a set of related interconnected nodes in a hierarchical structure. - a non-empty collection of vertices and edges that satisfies certain requirements. - structure resembles branches of a tree, hence the name. Different types of trees : - Rooted tree - Ordered tree - M-ary tree and binary tree
a b d i e f g c h
A tree
Trees
Where have you seen a tree structure before? Examples of trees: - Directory tree - Family tree - Company organization chart - Table of contents - etc.
root edges
a b d i e f g c
Nodes/Vertices
leaves
Rooted Tree
A Rooted Tree is one where we designate one node as the root (i.e. the tree examples we have been looking at so far are all rooted trees). In computer science we normally reserve the term tree to refer to rooted trees. The more general structure is a free tree In a rooted tree, any node is the root of a subtree consisting of it and the nodes below it. There is exactly one path between the root and each of the other nodes in the tree Each node except the root has exactly one node above it in the tree, (i.e. its parent), and we extend the family analogy talking of children, siblings, or grandparents
Programming and Data Structures 6
Ordered Tree
An ordered tree is a rooted tree in which the order of the children at every node is specified. If each node must have a specific number of children appearing in a specific order, then we have an M-ary tree The simplest type of M-ary tree is the binary tree
Binary Trees
A binary tree is a tree where each node has exactly zero, one or two children. i.e. each parent can have no more than 2 children. As with any Abstract Data Structure we can implement a binary tree in a number of ways, using arrays, strings, or structures and pointers
a
left child
b e f
right child
d h
Examples of Trees
A free tree A binary tree A rooted binary tree
A forest
null
null
We may then build our binary tree using this node structure.
Programming and Data Structures 10
11
12
Examples
12 5 4 2
BST for numbers
John
16 7 13 21
Jack
James Jesus
Joseph
13
14
15
use recursive approach. What are the base and recursive cases? : Base case: if tree is empty, create new node for item Recursive case: if key < root's value, add to the left subtree, otherwise to the right subtree
16
17
Tree Traversal
As with lists, we would like to be able to traverse through all nodes in our tree. Problem: what order should the nodes be visited in? - Top-down? - Left-to-right? - Bottom-up? We may traverse a binary tree in 3 ways : - preorder - postorder - inorder
Programming and Data Structures 18
Types of Traversal
Preorder traversal: - visit the node first and process. - do preorder traversal of left subtree. - do preorder traversal of right subtree. - i.e. visits and processes each node in a tree BEFORE visiting and processing its children. Postorder traversal: - do postorder traversal of left subtree. - do postorder traversal of right subtree. - visit the node last and process. - i.e. visits and processes each node in the tree AFTER visiting and processing its children. Inorder traversal: - do inorder traversal of left subtree. - visit the node and process. - do inorder traversal of right subtree. - i.e. processes nodes in the tree in an ascending sorted order.
Programming and Data Structures 19
a c
20
Traversal Methods
void inorder(BTreeNode the_node) { if (the_node == null) ; // do nothing else { inorder(the_node.left); System.out.print(the_node.data); inorder(the_node.right); } } void preorder(BTreeNode the_node) { if (the_node == null) ; // do nothing else { System.out.print(the_node.data); preorder(the_node.left); preorder(the_node.right); } }
Inorder
void postorder(BTreeNode the_node) { if (the_node == null) ; // do nothing else { postorder(the_node.left); postorder(the_node.right); System.out.print(the_node.data); } } Programming and Data Structures
Preorder
Postorder
21
22
expression
statement ...
LHS =
expression var y
23
const var 1 x
24
25