將BST轉換為有序的雙向鍊錶

2021-06-19 09:31:45 字數 1523 閱讀 4839

在二叉樹中,每個結點都有兩個指向子結點的指標. 在雙向鍊錶中, 每個結點也有兩個指標,它們分別指向前乙個結點和後乙個結點.由於這兩種結構的相似性, 同時二叉搜尋樹也是一種排過序的資料結構, 因此在理論上有可能實現二叉搜尋樹和排序的雙向鍊錶之間的轉換.

下面的檔案bst_to_dl.cpp將bst轉換為排序過的雙向鍊錶,請參加**:

#include "binary_tree.h"

void convertnode(binarytreenode* pnode, binarytreenode** plastnodeinlist)

binarytreenode* convert(binarytreenode* proot)

//********************====測試** *************************====

void printdoublelinkedlist(binarytreenode* pheadoflist)

printf("\nthe nodes from right to left are:\n");

while(pnode != null)

printf("\n");

}void destroylist(binarytreenode* pheadoflist)

}void test(const char* testname, binarytreenode* proot)

// 10

// / \

// 6 14

// /\ /\

// 4 8 12 16

void test1()

// 5

// /

// 4

// /

// 3

// /

// 2

// /

// 1

void test2()

// 1

// \

// 2

// \

// 3

// \

// 4

// \

// 5

void test3()

//樹中只有1個結點

void test4()

//樹中沒有結點

void test5()

int main(int argc, char** argv)

將單向有序鍊錶轉化為BST

原題目為 leetcode的 convert sorted list to binary search tree 樹和鍊錶的定義 definition for singly linked list.public class listnode definition for binary tree pu...

二叉搜尋樹轉換為有序雙向鍊錶

一 問題描述 輸入一棵二叉搜尋樹,現在要將該二叉搜尋樹轉換成乙個排序的雙向鍊錶。而且在轉換的過程中,不能建立任何新的結點,只能調整樹中的結點指標的指向來實現。二 實現思路 在二叉搜尋樹中,每個結點都有兩個分別指向其左 右子樹的指標,左子樹結點的值總是小於父結點的值,右子樹結點的值總是大於父結點的值。...

二叉搜尋樹轉換為有序雙向鍊錶

一 問題描述 輸入一棵二叉搜尋樹,現在要將該二叉搜尋樹轉換成乙個排序的雙向鍊錶。而且在轉換的過程中,不能建立任何新的結點,只能調整樹中的結點指標的指向來實現。二 實現思路 在二叉搜尋樹中,每個結點都有兩個分別指向其左 右子樹的指標,左子樹結點的值總是小於父結點的值,右子樹結點的值總是大於父結點的值。...