Topics
-
Upload
serina-adkins -
Category
Documents
-
view
17 -
download
1
description
Transcript of Topics
Topics
Definition and Application of Binary Trees
Binary Search Tree Operations
Definition and Application of Binary Trees
• Binary tree: a nonlinear data structure in which each node may point to 0, 1, or two other nodes
• The nodes that a nodeN points to are
the childrenof N NULL NULL
NULL NULL NULL NULL
Terminology
• If a node N is a child of another node P, then P is called the parent of N
• A node that has no children is called a leaf
• In a binary tree there is a unique node with no parent, it is called the root of the tree
Binary Tree Terminology
• Tree pointer: like a head pointer for a linked list, it points to the first node in the binary tree
• Root node: the node with no parent NULL NULL
NULL NULL NULL NULL
Binary Tree Terminology
• Leaf nodes: nodes that have no children
The nodes containing 7 and 43 are leaf nodes
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
Binary Tree Terminology
• Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
Binary Tree Terminology
The parent of the node containing 43 is the node containing 59
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
Binary Tree Terminology
• A descendant of a node is defined as follows:
(1) A node is considered its own descendant
(2) A child of a descendant of a node N is a descendant of N
So the set of all descendants of a nodeincludes the node itself, its children, thechildren of the children, etc.
Binary Tree Terminology
• A subtree of a binary tree is a part of the tree consisting of all descendants of a given node N
• Such a subtree is said to be rooted at N, and N is called the root of the subtree
Subtrees of Binary Trees
• A subtree of a binary tree is itself a binary tree
• A nonempty binary tree consists of a root node, with the rest of its nodes forming two subtrees, called the left and right subtree
Binary Tree Terminology
• The node containing 31 is the root
• The nodes containing 19 and 7 form the left subtree
• The nodes containing 59 and 43 form the right subtree
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
Binary Tree Node• A node in a binary tree is like a node in a
linked list, except it has two node pointer fields:class TreeNode{ public: int value;
TreeNode *left; TreeNode *right;
};It is convenient to use a constructor to assistin creation of nodes
Binary Tree Operations
• Create a binary tree
• Insert a node into a binary tree at some position
• Delete a node from a binary tree – remove a node and adjust links to preserve the binary tree
• Update a node in a binary tree
• Retrieve value of a node in a binary tree
Inserting an item into a Binary Tree
1) If tree is empty, create a new node as root, with empty left and right subtrees
2) Otherwise, insert the item as a leaf (as either the right or left child), update the links
Inserting an item into a Binary Tree
NULL NULL7
87
65
43
59
root
Since the right subtree is NULL, insert 23 here
NULL NULL NULL NULL
Traversing a Binary Tree
Three traversal methods:1) Inorder:
a) Traverse left subtree of nodeb) Process data in nodec) Traverse right subtree of node
2) Preorder: a) Process data in nodeb) Traverse left subtree of nodec) Traverse right subtree of node
3) Postorder: a) Traverse left subtree of nodeb) Traverse right subtree of nodec) Process data in node
Traversing a Binary Tree
NULL NULL7
87
65
43
59
TRAVERSAL METHOD
NODES VISITED IN ORDER
Inorder 7, 87, 65, 43, 59
Preorder 65, 87, 7, 59, 43
Postorder 7, 87, 43, 59, 65
NULL NULL NULL NULL
Uses of Binary Trees
• Binary search tree: a binary tree whose data is organized to simplify searches
• Left subtree at each node contains data values less than the data in the node
• Right subtree at each node contains values greater than the data in the node
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
Uses of Binary Trees
• Decision Tree: a binary tree whose data is organized to answer yes/no question
• Left subtree at each node contains yes responses
• Right subtree at each node contains no responses
NULLBird
Does it fly?
Is it an animal?
Flower
NULL NULL NULL
Dog
NULL NULL
NULL