Download presentation
Presentation is loading. Please wait.
1
Data Structures: Trees and Binary Trees
By: A. H. Abdul Hafez DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
2
Outlines Definition of a tree Terminology Binary tree
Properties of binary trees Array representation of binary tree Linked list representation of binary tree End DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
3
Definition of a Tree DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU
December 8, 2018
4
Terminology The root contains A (we usually say node A), and we normally draw trees with their root is at the top. The number of sub-trees of a node is called its degree. The degree of A is 3, of C is 1, and of F is 0. Nodes that have degree zero are called leaf or terminal nodes. The set {K. L. F, G, M, I, J} is the set of leaf nodes of figure. The other nodes are referred to as non terminals. The roots of the sub-trees of a node X are the children of X. The node X is the parent of its children. Thus the children of D are H, I, and J, and the parent of D is A. Children of the same parent arc said to be siblings. For example H, I, and J are siblings. DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
5
Terminology cont. The degree of a tree is the maximum degree of the nodes in the tree. The ancestors of a node are all the nodes along the path from the root to that node. The ancestors of M are A, D, and H. The level of a node is defined by initially letting the root be at level one. If a node is at level p, then its children are at level p + 1. The figure shows the levels of all nodes in that tree. The height or depth of a tree is defined to be the maximum level of any node in the tree. A forest is a set of n>0 disjoint trees. The notion of a forest is very close to that of a tree because if we remove the root of a tree, we get a forest. For example, if we removed A , we get a forest with three trees. DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
6
Binary tree The terms such as degree, level, height, leaf, parent of a child, all apply to binary trees as well. Skewed tree Complete binary tree DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
7
Properties of binary trees
The binary tree of depth k that has exactly 2k - 1 nodes is called a full binary tree of depth k. full binary tree with depth =4. DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
8
Properties of binary trees
It is useful to represent the nodes by numbering them, starting with the node on level one, then going to those on level two, and so on. Nodes on any level are numbered from left to right. A binary tree with n nodes is complete if and only if its nodes correspond to the nodes that are numbered from 1 to n in the full binary tree of depth k. Leaf nodes occur on at most two adjacent level. Using an array Tree[1:n] the nodes number i is stored in the ith element. But it is wasteful for many binary trees. DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
9
Properties of binary trees
DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
10
Linked list representation of a binary tree
Each node has three fields lchild, data, rchild. In many applications a forth field parent is added. DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
11
The end of the Lecture Thanks for your time Questions are welcome
DS, by Dr. A.H. Abdul Hafez, CE Dept. HKU December 8, 2018
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.