Download presentation
Presentation is loading. Please wait.
1
Csc 2720 Instructor: Zhuojun Duan
Data structure: Trees Csc 2720 Instructor: Zhuojun Duan
2
Outline
3
Outline Introduction of trees Implementation Array-based
Reference-based
4
Nature View of a Tree leaves branches root
5
Computer Scientist’s View
root leaves nodes branches
6
Applications Manipulate hierarchical data: Family tree
Manipulate hierarchical data: File system
7
Applications 1. Manipulate hierarchical data. 2. Make information easy to search (see tree traversal). 3. Manipulate sorted lists of data. 4. As a workflow for compositing digital images for visual effects. 5. Router algorithms
8
General tree A tree is a collection of nodes
The collection can be empty (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r
9
General tree Child and parent Leaves Sibling
Every node except the root has one parent A node can have an arbitrary number of children Leaves Nodes with no children Sibling nodes with same parent
10
Binary tree A binary tree is a set T of nodes such that either
T is empty, or T is partitioned into three disjoint subsets: A single node r, the root Two possibly empty sets that are binary trees, called left and right subtrees of r
11
Binary tree Example: Expression Trees
Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators Will not be a binary tree if some operators are not binary
12
Height of trees The height of trees Level of a node n in a tree T
If n is the root of T, it is at level 1 If n is not the root of T, its level is 1 greater than the level of its parent Height of a tree T defined in terms of the levels of its nodes If T is empty, its height is 0 If T is not empty, its height is equal to the maximum level of its nodes Level 1 Level 2 Height of the tree: 4 Level 3 Level 4
13
Height of trees 1 1 1 2 2 2 2 2 3 3 3 Binary trees with the same nodes but different heights 4 4 3 3 3 5 3 5 6 7 Height is 3 Height is 5 Height is 7
14
Full binary tree Recursive definition of a full binary tree
If T is empty, T is a full binary tree of height 0 If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h – 1
15
Complete binary trees Complete binary trees
A binary tree T of height h is complete if All nodes at level h – 2 and above have two children each, and When a node at level h – 1 has children, all nodes to its left at the same level have two children each, and When a node at level h – 1 has one child, it is a left child Full binary trees are complete tree
16
Summary of tree terminology (1)
General tree A set of one or more nodes, partitioned into a root node and subsets that are general subtrees of the root Parent of node n The node directly above node n in the tree Child of node n A node directly below node n in the tree Root The only node in the tree with no parent
17
Summary of tree terminology (2)
Leaf A node with no children Siblings Nodes with a common parent Ancestor of node n A node on the path from the root to n Descendant of node n A node on a path from n to a leaf Subtree of node n A tree that consists of a child (if any) of n and the child’s descendants
18
Summary of tree terminology (3)
Height The number of nodes on the longest path from the root to a leaf Binary tree A set of nodes that is either empty or partitioned into a root node and one or two subsets that are binary subtrees of the root Each node has at most two children, the left child and the right child Left (right) child of node n A node directly below and to the left (right) of node n in a binary tree Left (right) subtree of node n In a binary tree, the left (right) child (if any) of node n plus its descendants Binary search tree A binary tree where the value in any node n is greater than the value in every node in n’s left subtree, but less than the value of every node in n’s right subtree Empty binary tree A binary tree with no nodes
19
Summary of tree terminology (4)
Full binary tree A binary tree of height h with no missing nodes All leaves are at level h and all other nodes each have two children Complete binary tree A binary tree of height h that is full to level h – 1 and has level h filled in from left to right
20
Basic Operations of the Binary Tree
The operations available for a particular ADT binary tree depend on the type of binary tree being implemented Basic operations of the ADT binary tree createBinaryTree() createBinaryTree(bTreeNode root) makeEmpty() isEmpty() getRootItem() throws TreeException
21
Gneral Operations of the Binary Tree
General operations of the ADT binary tree createBinaryTree (bTreeNode root, binaryTree leftTree, binaryTree rightTree) setRootItem(bTreeNode treenode) attachLeft(bTreeNode treenode) throws TreeException attachRight(bTreeNode treenode) throws TreeException attachLeftSubtree(binaryTree leftTree) throws TreeException attachRightSubtree(binaryTree rightTree) throws TreeException detachLeftSubtree() throws TreeException detachRightSubtree() throws TreeException
22
Traversals of a Binary Tree
In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. Recursive traversal algorithms Preorder traversal (root leftChild rightChild ) Inorder traversal (leftChild root rightChild ) Postorder traversal (leftChild rightChild root )
23
Preorder Example (Visit = print)
root leftChild rightChild a b c a b c
24
Preorder Example (Visit = print)
root leftChild rightChild a b c d e f g h i j a b d g h e i c f j
25
Preorder Example (Visit = print)
preOrder(bTreeNode root) { if (root != nullNULL) visit(root); preOrder(root.leftChild); preOrder(root.rightChild); }
26
Inorder Example (Visit = print)
leftChild root rightChild a b c b a c
27
Inorder Example (Visit = print)
leftChild root rightChild a b c d e f g h i j g d h b e i a f j c
28
Inorder Example (Visit = print)
inOrder(bTreeNode root) { if (root != NULL) inOrder(root.leftChild); visit(root); inOrder(root.rightChild); }
29
Inorder Of Expression Tree
+ a b - c d e f * / e a + b * c d / f - Gives infix form of expression (sans parentheses)!
30
Postorder Example (Visit = print)
leftChild rightChild root a b c b c a
31
Postorder Example (Visit = print)
b c d e f g h i j leftChild rightChild root g h d i e b j f c a
32
Postorder Example (Visit = print)
postOrder(bTreeNode root) { if (root != NULL) postOrder(root.leftChild); postOrder(root.rightChild); visit(root); }
33
Outline Introduction of trees Implementation Array-based
Reference-based
34
Binary tree implementation array-based
An array-based implementation A Java class is used to define a node in the binary tree A binary tree is represented by using an array of tree nodes Each tree node contains a data portion and two indexes (one for each of the node’s children) Requires the creation of a free list which keeps track of available nodes
35
Binary tree implementation array-based
a) A binary tree of names b) its array-based implementations
36
Binary tree implementation array-based
Complete binary tree is easily represented as an array (either static or dynamic) root is tree[0] for any node at tree[n], the parent node will be found at index (n-1)/2 for any node at tree[n], the children of n (if any) will be found at tree[2n + 1] and tree[2n + 2] If the binary tree is complete and remains complete: A memory-efficient array-based implementation can be used 1 2 Jane Bob Tom Alan Ellen Nancy null . … 3 4 5
37
Outline Introduction of trees Implementation Array-based
Reference-based
38
Binary tree implementation reference-based
A binary tree can be represented by its individual nodes. Each node will contain references to its left child and right child. The node also has at least one instance variable to hold some data. An entire tree is represented as a reference to the root node
39
Binary tree implementation reference-based
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.