Lecture 7 Data Structures
Lecture 7 Data Structures
Contrary to arrays, stacks, queues and sequences all of which are onedimensional data structures, trees are two-dimensional data structures with hierarchical relationship between data items. Definition 1 A tree is a non-empty collection of vertices (nodes) and edges that satisfy certain requirements. Definition 2 A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree. One node in the tree is designated as the root. Each tree has exactly one path between the root and each of the other nodes. If there is more than one path between the root and some node, or no path at all, we have a graph.
Example of a tree:
root siblings
subtree
More definitions
Definition 5 An ordered tree is a tree in which the order of children is specified. Definition 6 A level (depth) of a node in the number of nodes on the path from that node to the root. Definition 7 The height (maximum distance) of a tree is the maximum level among all of the nodes in the tree. Definition 8 The path length of a tree is the sum of the levels of all the nodes in the tree. Definition 9 A tree where each node has a specific number of children appearing in a specific order is call a multiway tree. The simplest type of a multiway tree is the binary tree. Each node in a binary tree has exactly two children one of which is designated as a left child, and the other is designated as a right child. Definition 10 (Recursive definition) A binary tree is either an external node, or an internal node and two binary trees.
left child
right child
one or both children might be external nodes
special external nodes with no name and no data associated with them
+ A B C E
*
/ F
Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right subtree next, and finally the root) returns the postfix form of the arithmetic expression, while the pre-order traversal (root is visited first, then the left subtree, then the right subtree) returns the prefix form of the arithmetic expression.
2. Binary tree with a heap property. The underlying hierarchical relationship suggests that the datum in each node is greater than or equal to the data in its left and right subtrees.
87
84 68 32 67 79 6 8 63 12 10 9
3. Binary tree with an ordering property. The underlying hierarchical relationship suggests that the datum in each node is greater than the data in its left subtree, and less than or equal to the data in its right subtrees.
87
84 68 32 70 74 80 86 103 90 88 97 109
4. Decision trees. The underlying hierarchical relationship depends on the nature of the domain represented by the binary tree. For example, consider a domain that consists of the following statements (from J.Ignizio Intro to ES):
If the planes engine is propeller, then the plane is C130. If the planes engine is jet and the wing position is low, then the plane is B747. If the planes engine is jet and the wing position is high and no bulges are seen, then the plane is C5A If the planes engine is jet and the wing position is high and bulges are aft of wing, then plane is C141 .
Engine type
Jet Propeller
Wing Position
Low High
C130 Bulges
B747
None
Aft Wing
C5A
C141
4
8 9 10
5
11 12
6
13 14
7
15
A complete binary tree is a full binary tree where the internal nodes on the bottom level all appear to the left of the external nodes on that level. Here is an example of a complete binary tree: 1 2 3
4 5 6
A B C
*
/ E F
level 1 (d = 1)
level 2 (d = 2) level 3 (d = 3)
To represent this tree, we need an array of size 23+1 - 1 = 15 The tree is represented as follows: 1. The root is stored in BinaryTree[1]. 2. For node BinaryTree[n], the left child is stored in BinaryTree[2*n], and the right child is stored in BinaryTree[2*n+1]
i: BinaryTree[i]: 1 + 2 3 * 4 A 5 B 6 C 7 / 8 9 10 11 12 13 14 E 15 F
2 3
A
5 4
B
6
C
7
/
8
E
9
F
Nodes in this tree can be viewed as positions in a sequence (numbered 1 through 9).
class BTLRPS implements PSDLL { private BTNode header; private BTNode trailer; private int size; int position;
Example Consider a tree with an ordering property, where nodes are inserted in the following order b i n a r y t r e e, i.e. b a i e n e r y t r The preorder traversal is: b a i e e n r y t r
The nodes in the example tree are traversed in post-order as follows: a e e r t y r n i b In-order traversal
public void inOrder (BTNode localRoot) { if (localRoot != null) { inOrder(localRoot.leftChild); localRoot.displayBTNode(); inOrder(localRoot.rightChild); } }
3 15 11
search stops here 7 13 Step 2: insert 9 at the point where the search terminates unsuccessfully 3 2 15 1 7 9 11 13 That is, new nodes are always inserted at the leaf level.
empty(): returns true if the container is empty. node(position): returns the node in position. elements(): returns an enumeration of all data stored at nodes of the tree. positions(): returns an enumeration of all the positions (nodes) of the tree. size(): returns the size of the container. replace (position, item): replaces the data at position with item. swap (position1, position2): swaps data in position1 and position2.
getRoot(): returns the root node of the tree isRoot(position): returns true if the node in position is the root note. isInternal(position): returns true if the node in that position is an internal node. isExternal(position): returns true if the node in that position is an external node. parent(position): returns the parent of the node in position. children(position): returns a set of children of the node in position. siblings(position): returns a set of siblings of the node in position.
We can represent it in the following binary tree format: 1 Jim 2 Bill 8 Katy 10 Mike 14 Tom
3 Dave
5 Lary
4 Mary
6 Paul
9 Leo
7
11 Bety
Peny
13 Rog
12 Don
class TNode { private String data; private TNode children, sibling; int position; ... class methods follow ... }
Tree traversals
Consider our example tree Jim Bill Dave Lary Mary Paul Katy Leo Peny Mike Bety Don Rog Tom
Preorder traversal is: Jim Bill Dave Don Rog Tom Postorder traversal is: Dave Lary Paul Peny Mary Bill Leo Katy Don Bety Rog Mike Tom Jim Mary Lary Paul Peny Katy Leo Mike Bety
Or, in JAVA:
public void preOrder (TNode localRoot) { localRoot.displayTNode(); Enumeration localRootChildren = localRoot.children(localRoot.getPosition()); while (localRootChildren.hasMoreElements()) { TNode nextNode = localRootChildren.nextElement(); preOrder (nextNode); } }
Note: If a general tree is represented as a binary tree, a preorder traversal of the general tree and the corresponding binary tree, produces the same result.
Or, in JAVA:
public void postOrder (TNode localRoot) { Enumeration localRootChildren = localRoot.children(localRoot.getPosition()); while (localRootChildren.hasMoreElements()) { TNode nextNode = localRootChildren.nextElement(); postOrder (nextNode); } localRoot.displayTNode(); }
Note: If a general tree is represented as a binary tree, a postorder traversal of the general tree and the corresponding binary tree, do not generate the same result. However, the inorder traversal of the corresponding binary tree generates the same result as the postorder traversal of the general tree.
left sibling, which is either null or points to a node whose data precedes that of a given node at the same level; data stored in the node; children, a pointer to the ordered list of children of that node, or null if the node has no children; right sibling, which is either null or points to a node whose data equals or follow that of a given node at the same level.
Represented as a ternary tree, it looks like as follows: Jim Bill Dave Lary Paul Mary Peny Katy Leo Mike Bety Don Rog Tom