tree, binary tree

Tree

Binary search tree definition:

A binary search tree is also called a binary search tree, in which each node has a key that identifies the node as unique, and each key is greater than the key of any node on the left subtree and smaller than the key of any node on the right subtree.

Features:

●If the left subtree of any node is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node .
●If the right subtree of any node is not empty, the values ​​of all nodes on the right subtree are greater than the value of its root node .
●The left and right subtrees of any node are also binary search trees.
There are no nodes with equal key values ​​(no duplicate nodes).

create binary tree

main.h//(main function)

//主函数
#include"Tree.h"
int main(){
    
    
vector<int> vec = {
    
    7,5,4,6,11,9,10};
Tree t(vec);
t.preOrder();//先序遍历
t.LevelOrder();//中序遍历
t.postOrder();//后序遍历
return 0;
}

//Tree.h

#pragma once
#include<iosteam>
#include<vector>//动态数组头文件
#include<queue>//队列头文件
#include<stack>//栈头文件
using namespace std;
struct Node{
    
    //创建节点
int key;
Node* left = nullptr;
Node* right = nullptr;
Node(int key){
    
    
this-> key = key;
}
};

class Tree{
    
    
Node* root = nullptr;
public:
Tree(vector<int>vec);
Node* getRoot(){
    
    return root;}
void LevelOrder();//先序遍历
void PreOrder();//中序遍历
void PostOrder();//后序遍历

void In_Order(Node* root){
    
    
if(root == nullptr) return;
In_Order(root -> nullptr) return;
cout << root->key<<" ";
In_Order(root->right);
}
}

Tree.cpp

Tree::Tree(vector<int> vec){
    
    }//创建树函数
void Tree::LevelOrder(){
    
    
if(root == nullptr) return;
queue<Node*> que;
que.push(root);
while(!que.empty()){
    
    
int n = que.size();
for(int i = 0;i < n;i++){
    
    
Node* fornt = que.front();
cout<<front->key<<" ";
que.pop();
if(front->left)que.push(front->left);
if(front->right)que.push(front->right);
}
cout<<endl;
}
}

Binary tree traversal

To traverse a binary tree is to "visit" each node of the binary tree one by one in some way. "Visiting" means doing some kind of operation on a node, such as outputting a node's direct. According to the order of visiting the root of the tree, we have three ways to traverse the binary tree:
1. Preorder traversal: root -> left subtree -> right subtree
2. Inorder traversal: left subtree -> root -> right subtree
3. Post-order traversal: left subtree -> right subtree -> tree root
These three traversals are defined according to the access order of the root node

1. Preorder traversal

Visit the root node first, then traverse the left subtree, and finally traverse the right subtree; and when traversing the left and right subtrees, you still need to visit the root node first, then traverse the left subtree, and finally traverse the right subtree.
Using a stack to create an animation for preorder traversal

preorder traversal function
void Tree::PreOrder(){
    
    //先序遍历函数,根左右
if(root == nullptr){
    
    
stack.push(root);
while(!stk.empty()){
    
             //判断栈是否为空,不为空进行以下操作
Node* top = stk.top();
stk.pop();
cout<<top->key<<" ";
if(top->right) stk.push(top->right);    //栈先存该节点的右子节点,因为栈是先进后出
if(top->left) stk.push(top->left);
}
}
}

2. Inorder traversal

First traverse the left subtree, then visit the root node, and finally traverse the right subtree; and when traversing the left and right subtrees. Still traverse the left subtree first, then visit the root node, and finally traverse the right subtree.

inorder traversal function
void Tree::InOrder(){
    
    //中序遍历函数,左根右
if( root == nullptr) return;  //判断树是否为空
stack<Node*> stk;
while(!stk.empty() || cur){
    
      //判断条件为栈不为空或cur不为空,
while(cur){
    
                     //cur存在则进行该循环,不存在则跳过,作用为一直向下记录左节点
stk.push(cur);
cur = cur->left;
}
Node* top = stk.top;     //创建真正之变量来记录弹出节点的地址
stk.pop();
cout<<top->key<<" ";
cur = top->right;      //每次输出一个节点必须判断一下是否存在右子节点
} 
}

3. Post-order traversal

First traverse the left subtree, then traverse the right subtree, and finally visit the root node; similarly, when traversing the left and right subtrees, you must first traverse the left subtree, then traverse the right subtree, and finally visit the root node. The results of the preorder traversal of the previous graph are as follows.

postorder traversal function
//
void Tree::PostOrder(){
    
    //后序遍历函数,左右根
if(root == nullptr) return;
stack<Node*>s1,s2;          //创建两个栈
s1.push(root);             
while(!s1.empty()){
    
               //该循环的作用为记录按照根左右的顺序来向s1记录树里面的元素
Node* top = s1.top();
s1.pop();
s2.push(top);                //s2的作用的是按照根右左的顺序向栈里记录树的元素
if(top->left) s1.push(top->left);   //top的左节点若存在则向S1栈里记录元素
if(top->right) s1.push(top->right);   //top的右节点若存在则向S1栈里记录元素
}
while(s2.size()){
    
    
cout<<s2.top()->key<<" ";
s2.pop();
}
}

do the question

1. Determine the only binary tree

Among the three traversal methods of the binary tree,
1. If there are inorder and preorder traversal results
2. Or inorder and postorder traversal results,
3. Inorder and levels can get the only binary tree from these results.

Ways to determine a unique binary tree:

1. First, determine the root node in the in-order traversal according to the first element of the pre-order traversal or the tail element of the post-order traversal.
2. Then determine the left and right subtrees in the in-order traversal according to the root node.

2. The way to do the question of the binary search tree

1. Select the recursive method
2. Recursive exit (end) condition: In general, the node is empty or meets the meaning of the question
3. What each node does
4. Return value
depth: the longest path from a node to the root node, use Pre-order traversal
Height: the longest path from a node to a leaf node,
except for post-order traversal, which is a post-order traversal for depth

Example: The kth largest node of a binary search tree

Given a binary search tree, find the value of the kth largest node in it.

insert image description here
insert image description here
Limit: 1 ≤ k ≤ number of binary search tree elements

class Solution {
    
    
public:
int res = 0;
int a = 0;
    int kthLargest(TreeNode* root, int k) {
    
      //二叉搜索树的中序遍历是从小到大的遍历(左根右),这里通过递归的方法用右根左的遍历可得到由大到小的遍历
        if(root == NULL)return res;
        kthLargest(root->right,k);
        a++;
        //k--;
        if(k == a){
    
    
            res = root->val;
            return res;
        }
        kthLargest(root->left,k);
        return res;
    }
};

Guess you like

Origin blog.csdn.net/m0_66656626/article/details/129107713