Chapter 3 Part Three Tree
Chapter 3 Part Three Tree
Trees
Trees
⚫ A tree is a collection of nodes.
zero or more nonempty sub-trees T1,T2, ...., Tk, each of whose roots
are connected by an edge from r
2
Some Terminologies
3
Some Terminologies…
Path
is a sequence of nodes from root to a node (arbitrary node in
the tree).
Length
Number of edges on the path from node x to node y
Depth of a node
Number of edges from the root to that node (Depth of C =1)
The depth of a tree is equal to the depth of the deepest
leaf (=3)
4
Some Terminologies…
Height of a node
The ancestors of a node are all the nodes along the path from the root to the
node.
5 Descendant node reachable by repeated proceeding from parent to child.
A Tree Representation
⚫ A node is represented by an object
storing
⚫⚫Element
Parent node B
⚫ Sequence of children nodes
A D F
C E
6
Binary Tree
⚫ A binary tree is a tree with the following
properties: Applications:
⚫ Each internal node has at most two arithmetic
children (degree of two) expressions
⚫ The children of a node are an ordered pair decision processes
searching
⚫ We call the children of an internal node left
A
child and right child
⚫ Alternative recursive definition: a binary
B C
tree is either
⚫ a tree consisting of a single node, OR D E F G
⚫position leftChild(p)
⚫position rightChild(p)
⚫position sibling(p)
8
Data Structure for Binary Trees
⚫ A node is represented
B
by an object storing
⚫Element A D
⚫Parent node
⚫Left child node C E
9
Examp
le
Struct Node{
Int data;
Node *
parent; Node
*Lchiled;
Node *
Rchiled;
}Node
*root=NULL;
10
Example: Expression Trees
⚫ One of the application of binary is representing arthematic exression
⚫ Pre-order traversal
12
Preorder, Postorder and Inorder
⚫ Preorder traversal
⚫node, left, right
⚫prefix expression
⚫ ++a*bc*+*defg
13
Preorder, Postorder and
Inorder
⚫ Postorder traversal
⚫left, right, node
⚫postfix expression
⚫abc*+de*f+g*
+
⚫ Inorder traversal
⚫left, node, right.
⚫infix expression
⚫a+b*c+d*e+f*
14
g
Preorder, Postorder and
Inorder
18
Binary Search
Trees
⚫ Stores keys in the nodes in a way so that searching, insertion and deletion
can be done efficiently.
Binary search tree property
⚫ For every node X, all the keys in its left subtree are smaller than the key
value in X, and all the keys in its right subtree are larger than the key
value in X
16
Binary Search
Trees…
17
Binary search
trees…
Two binary search trees representing the same set:
18
Implementation of
BSTStruc node
{
Int num;
Node *
parent
Node*left;
Node *
right;
}
Node
19 *root=NULL;
Inserting node in
BST
⚫ When a new node is inserted the definition of BST should
be preserved.
⚫ There are two cases to consider
⚫There is no data in the tree (root=null)
⚫Root=newnode;
⚫There is data
⚫Search the appropriate position
⚫Insert the node in that position.
20
Example- insert
node
Proceed down the tree as you would with a find
If newnode is found, do nothing (or update something)
Otherwise, insert newnode at the last spot on the path
traversed
22
23
Searching
(Find)
Find X: return a pointer to the node that has key X, or NULL if there is no such
node
Time complexity
O(height of the tree)
24
findMi
⚫nReturn the node containing the smallest element in the tree
⚫ Start at the root and go left as long as there is a left child. The stopping
point is
the smallest element
Node*findMin(node*root)
{
If(root==NULL)
Return Null;
Ellse if(root->left==Null)
Return root
Else
Return(findMin(root->left)
}
Node*findMin(node*root)
{
If(root==NULL
) Return
Null;
Ellse if(root-
>right==Null)
Return root
Else
Return(findMin(
26
root->right)
delet
eWhen we delete a node, we need to consider how we take
⚫
care of the children of the deleted node.
⚫When a node is deleted the definition of a BST should
be maintained.
⚫ When a node is deleted four cases should be considered
⚫Case1: Deleting a leaf node (a node with no chiled )
⚫Case2: Deleting a node having only one child
⚫Case3: Deleting a node having two child
⚫Case4: Deleting a root node
27
delet
eThree cases:
(1) the node is a leaf
⚫ Delete it immediately
(2) the node has one
child
⚫ Adjust a pointer
from the parent to
bypass that node
⚫ Example delete
node 4, make
node 2 pointer
point to node 3
28
delet
e(3) the node has 2 children
Copy the node containing the largest element in the left( or the smallest element
in
the right)to the node to be deleted
Delete the copied node
The picture below shows deleting node2
30