Download presentation
Presentation is loading. Please wait.
Published byMoris Palmer Modified over 9 years ago
1
Topics Definition and Application of Binary Trees Binary Search Tree Operations
2
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 node N points to are the children of N NULL
3
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
4
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
5
Binary Tree Terminology Leaf nodes: nodes that have no children The nodes containing 7 and 43 are leaf nodes NULL 7 19 31 43 59 NULL
6
Binary Tree Terminology Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59 NULL 7 19 31 43 59 NULL
7
Binary Tree Terminology The parent of the node containing 43 is the node containing 59 NULL 7 19 31 43 59 NULL
8
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 node includes the node itself, its children, the children of the children, etc.
9
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
10
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
11
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 7 19 31 43 59 NULL
12
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 assist in creation of nodes
13
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
14
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
15
Inserting an item into a Binary Tree NULL 7 87 65 43 59 root Since the right subtree is NULL, insert 23 here NULL
16
Traversing a Binary Tree Three traversal methods: 1)Inorder: a)Traverse left subtree of node b)Process data in node c)Traverse right subtree of node 2)Preorder: a)Process data in node b)Traverse left subtree of node c)Traverse right subtree of node 3)Postorder: a)Traverse left subtree of node b)Traverse right subtree of node c)Process data in node
17
Traversing a Binary Tree NULL 7 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
18
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 7 19 31 43 59 NULL
19
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 NULL Bird Does it fly? Is it an animal? Flower NULL Dog NULL
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.